Ansible
Ansible automates server configuration and application deployment using declarative YAML playbooks. It's agentless — it connects over SSH and needs nothing installed on the target beyond Python — which makes it easy to adopt across existing fleets.
Where Terraform provisions infrastructure (creates the servers), Ansible configures what's on it (installs packages, writes config, deploys apps). The two are complementary, and Ansible's core virtue is idempotency: running a playbook repeatedly converges to the same desired state without breaking things.
TL;DR
- Agentless config management over SSH — declarative YAML playbooks.
- Strive for idempotent playbooks (safe to run repeatedly).
- Organize reusable automation into roles.
- Store secrets in Ansible Vault or an external secret manager.
Quick Example
A task uses a module to declare desired state — installed and running — not imperative commands:
Core Concepts
- Inventory — the hosts (and groups) to manage.
- Playbook — a YAML file of plays/tasks describing desired state.
- Task — a single action, usually invoking a module.
- Module — a reusable building block (install a package, template a file).
- Role — a standard directory structure for reusable, shareable automation.
Best Practices
- Make tasks idempotent — prefer modules (which check state) over raw
shell/command. - Keep playbooks small and composable, organized into roles.
- Store secrets in Ansible Vault or an external secret manager — never in plain repos.
- Run playbooks in CI (with
--check/--diff) to validate changes before applying.
Comparison: Ansible vs Terraform
See Terraform.
Common Mistakes
Non-idempotent shell tasks
Secrets committed to the repo
FAQ
Ansible or Terraform?
They do different jobs and pair well. Terraform provisions infrastructure (declares the servers, networks, and databases you want). Ansible configures systems (installs and sets up software on them). A common pattern is Terraform to create the machines, then Ansible to configure them.
What does "idempotent" mean here?
Running the same playbook multiple times produces the same end state without unintended side effects. Ansible modules check current state and act only if needed — so re-running is safe. Raw shell/command tasks break this unless you guard them.
Why is Ansible agentless an advantage?
You don't install or maintain an agent on every managed host — Ansible connects over SSH and runs modules using the target's Python. That lowers the barrier to adopting it on existing servers and reduces moving parts to keep secure and updated.
How do I manage secrets in Ansible?
Use Ansible Vault to encrypt sensitive variables/files in the repo, or pull secrets at runtime from an external manager (e.g. HashiCorp Vault). Never store credentials in plaintext playbooks or inventories. See Secrets Management.
Related Topics
- Terraform — Provisioning to Ansible's configuring
- Linux — What Ansible configures
- Infrastructure as Code — The broader practice
- Secrets Management — Ansible Vault and beyond
- DevOps — The broader practice