Terraform vs Ansible: Provisioning vs Configuration Management
Overview Terraform and Ansible are both infrastructure-as-code tools but solve different problems: Terraform declaratively provisions and tracks cloud infrastructure using a state file, while Ansible procedurally configures and manages software on existing hosts with no persistent state. Many teams use them together — Terraform to stand up infrastructure, Ansible to configure it. Comparison Diagram Terraformdeclarativemain.tf (desired state)state file (source of truth)dependency graphVMNetworkDBconverges infra to match stateAnsibleproceduralplaybook.ymltask 1: install pkgtask 2: configuretask 3: start serviceServer AServer BServer Csequential push over SSH, no state file Comparison Table Aspect Terraform Ansible Primary purpose Provision and tear down cloud/infra resources (VMs, networks, DBs) Configure software and manage state on existing hosts Configuration language HCL (HashiCorp Configuration Language), declarative YAML playbooks, procedural task lists Execution model Builds a dependency graph and applies changes in parallel where possible Executes tasks sequentially, in order, per host State management Maintains a state file mapping config to real resources Stateless — queries live system facts on each run Idempotency approach Diffs desired config against state file before acting Each module checks current condition before making a change Connectivity/agent requirement Agentless — calls cloud/provider APIs directly Agentless — connects over SSH or WinRM to target hosts Failure & recovery handling Partial applies are resolved by re-running against the state file Reruns the playbook from the start; tasks are re-checked, not resumed Key Differences Terraform tracks infrastructure in a persistent state file; Ansible has no state store and reads live system facts each run. Terraform resolves a dependency graph to apply changes in parallel; Ansible runs tasks sequentially. Terraform talks to infrastructure through provider APIs; Ansible connects to hosts via SSH/WinRM. Terraform is built for provisioning infra; Ansible is built for configuration of what already exists. They’re commonly paired: Terraform creates the servers, then Ansible configures them. When to Use Each Terraform ...