What is the difference between Terraform and Ansible?
BeginnerConceptInfrastructure & cloud basics
Answer
In one line: Terraform is mainly for provisioning infrastructure (creating cloud resources like networks, VMs and databases), while Ansible is mainly for configuring systems (installing software and managing settings inside servers) — and they're often used together.
Terraform
- Purpose: create, change and delete infrastructure across clouds and SaaS (AWS, Azure, GCP, Kubernetes, Cloudflare, GitHub, Datadog…).
- Style: declarative (HCL).
- State: keeps a state file to track what it manages.
- Strength: dependency graph, plan before apply, full lifecycle of resources.
Ansible
- Purpose: configure servers, deploy apps, run operational tasks, manage network devices.
- Style: procedural playbooks in YAML, with idempotent modules.
- State: no state file; checks the current state of the target each run.
- Agentless: connects over SSH (Linux) or WinRM (Windows).
- Strength: OS-level configuration, ad-hoc tasks across many servers, orchestration steps.
| Terraform | Ansible | |
|---|---|---|
| Main use | Provisioning | Configuration management |
| Language | HCL | YAML |
| Approach | Declarative | Procedural, idempotent modules |
| State | State file | Stateless |
| Example task | Create VPC, EC2, RDS | Install nginx, deploy config, restart service |
Using them together:
- Terraform creates the VPC, security groups and EC2 instances, and outputs their IPs.
- Ansible uses a dynamic inventory (or the Terraform outputs) to install packages and deploy the app on those instances.
Or, with immutable infrastructure: Ansible runs inside Packer to build a golden image, and Terraform launches instances from that image.
Overlap: Ansible can create cloud resources and Terraform can run scripts, but each is clumsy at the other's main job.
Tip for interviews: "Terraform builds the house, Ansible furnishes it" is a memorable one-liner — follow it with a real example.
More on Infrastructure & cloud basics
What is the difference between declarative and imperative infrastructure?BeginnerWhat is idempotency, and why is it important in automation?BeginnerWhat is configuration drift, and how do you prevent it?BeginnerWhat is immutable infrastructure?BeginnerWhat does "pets vs cattle" mean?BeginnerWhat is cloud computing, and what are IaaS, PaaS and SaaS?Beginner