Sometimes Bash scripts are better than Ansible
Comparing Bash and Ansible in DevOps: when to use Bash scripts, when to use Ansible? Code samples for deploy + restart service on both.
Bash vs Ansible — When to choose which?
In the Bash series, we’ve seen how powerful Bash is for automation. But in DevOps, Ansible is also a popular choice for infrastructure automation. So when should you use Bash, and when should you use Ansible?
This isn’t a question of “which is better” but “which is more suitable” for a specific situation. This post will provide an objective comparison with practical code samples to help you make the right choice.
Overview comparison
| Criteria | Bash | Ansible |
|---|---|---|
| Learning curve | Low — everyone knows it | Medium — need to learn YAML, modules |
| Readability | Very easy — plain text | Easy — clear YAML |
| Idempotent | No — need manual implementation | Yes — built-in |
| Reusability | Medium — source/functions | High — roles, modules |
| Inventory | Manual — file or array | Built-in — dynamic inventory |
| Testing | Hard — need mocking | Easy — molecule |
| Debug | Easy — bash -x | Medium — verbose mode |
| Dependency | None | Requires Python, SSH |
| Remote execution | Manual SSH | Built-in |
| Best for | Quick scripts, small tasks | Complex infrastructure, multi-server |
When to use Bash?
Bash is suitable when:
- Simple scripts, running locally — backup, cleanup, health checks
- Quick debugging —
bash -xshows each execution line - No dependencies needed — Bash is available on every Linux server
- Small team, need flexibility — don’t want to set up Ansible infrastructure
- On-call situations — need quick fixes, no time to learn playbooks
Code sample: Deploy with Bash
| |
When to use Ansible?
Ansible is suitable when:
- Complex infrastructure — many servers, many roles
- Need idempotency — run again without unintended changes
- Large team, need conventions — clear YAML, easy to review
- Need audit trail — Ansible logs each task
- Multi-environment — dev, staging, production with separate inventories
Code sample: Deploy with Ansible
| |
| |
Same task comparison
Task: Restart service if config changes
Bash:
| |
Ansible:
| |
Analysis
| Bash | Ansible | |
|---|---|---|
| Lines of code | ~15 lines | ~12 lines |
| Readability | Very easy | Easy |
| Idempotent | No — need hash tracking | Yes — register + when |
| Remote | Need SSH loop | Built-in |
Team maturity is the deciding factor
Ansible needs discipline
Ansible is powerful but requires:
- Clear conventions — consistent task and variable naming
- Serious code review — YAML is easy to write but easy to mess up
- Testing — use molecule or Terratest
- Documentation — every role should have a README
Without these, playbooks become “YAML spaghetti” — harder to debug than Bash.
Bash suits flexible teams
Bash is suitable when:
- Small team (< 5 people) — no need for complex processes
- Need quick fixes — on-call situations
- Short scripts — under 200 lines
- No dependencies wanted — just Bash and SSH
Combining both
In practice, many teams use both:
| Task | Use what |
|---|---|
| Quick fix, on-call | Bash |
| Simple deployment | Bash |
| Infrastructure provisioning | Ansible |
| Configuration management | Ansible |
| Simple cron jobs | Bash |
| Multi-server orchestration | Ansible |
Example:
| |
Implementation notes
- Don’t glorify tools: Both Bash and Ansible are tools. Choose the right tool for the task, not the “trend”.
- Team review: Whether using Bash or Ansible, code review is important. Bash needs security checks, Ansible needs idempotency checks.
- Documentation: Bash scripts should have
--helpand comments. Ansible roles should have README and variable docs. - Testing: Bash is harder to test, but you should still test critical scripts. Ansible has molecule for testing.
- Progressive adoption: Start with Bash when the team is small. When the team grows and needs structure, gradually migrate to Ansible.
Conclusion
Bash and Ansible are not competitors but complementary tools. Bash excels at quick scripts, debugging, and situations requiring flexibility. Ansible excels at complex infrastructure, multi-server management, and teams needing conventions.
Choose the tool that fits the task and your team’s maturity level. Don’t use Ansible for a 5-line Bash task, and don’t use Bash for complex infrastructure requiring idempotency.
The most important thing is to understand the strengths and weaknesses of each tool to make informed decisions in your DevOps workflow.
