I’ve tried them all: Puppet, Chef, SaltStack. They all work. But Ansible is the one I keep coming back to. Here’s why.
The Pitch
Ansible is a configuration management tool that lets you define your infrastructure as code. But unlike its competitors, it:
- Requires no agents — just SSH
- Uses YAML — readable by humans
- Is idempotent — run it 100 times, same result
- Starts simple — but scales to thousands of servers
Your First Playbook
Let’s install Nginx on a server. Create nginx.yml:
| |
Run it:
| |
That’s it. Nginx is installed, running, and configured on every server in your webservers group.
The Inventory
Ansible needs to know what servers to target. Create inventory.ini:
| |
Groups let you organize servers logically. Run a playbook against webservers, databases, or production (which includes both).
Variables and Templates
Real power comes from variables. Define them in group_vars/webservers.yml:
| |
Then use Jinja2 templates. Create templates/nginx.conf.j2:
| |
Same playbook, different configs per environment. Magic.
Roles: Reusable Components
Once your playbooks grow, organize them into roles:
Then your playbook becomes:
| |
You can share roles via Ansible Galaxy, or keep them in your own git repo.
Real-World Patterns
Check Mode (Dry Run)
See what would change without changing anything:
| |
Limit to Specific Hosts
Test on one server before rolling out:
| |
Vault for Secrets
Encrypt sensitive data:
| |
Dynamic Inventory
For cloud environments, generate inventory from AWS, GCP, etc.:
| |
Idempotence: Why It Matters
Every Ansible task describes the desired state, not a sequence of commands:
| |
Run idempotent playbooks repeatedly. First run makes changes, subsequent runs verify everything is correct. This makes Ansible perfect for:
- Continuous enforcement — cron job that ensures config stays correct
- Disaster recovery — rebuild servers from scratch
- Onboarding — new servers get full config automatically
When Not to Use Ansible
Ansible isn’t always the answer:
- Container orchestration — use Kubernetes
- Immutable infrastructure — use Packer to build images
- Application deployment — consider dedicated CD tools
- Real-time config — Ansible is push-based, not reactive
But for traditional server management? It’s hard to beat.
Getting Started
| |
The Bottom Line
Ansible hits a sweet spot: powerful enough for complex infrastructure, simple enough to start using today. No agents to install, no master server to maintain, no new language to learn.
Write YAML. Describe your infrastructure. Let Ansible make it so.
Using Ansible in production? Have questions? Find me on Twitter.