What is the difference between Ansible and Terraform?
Compare Ansible vs Terraform: provisioning vs configuration management, state files, declarative models, and why the two tools work best together.
Expected Interview Answer
Ansible is primarily a configuration management tool that installs and configures software on existing servers, while Terraform is an infrastructure provisioning tool that creates and manages cloud resources like servers, networks, and databases.
Terraform is declarative and state-driven: it tracks resources in a state file and computes the changes needed to reach your desired infrastructure, excelling at provisioning. Ansible is procedural-leaning (ordered tasks) though idempotent, and excels at configuring what already exists — packages, files, and services. They are complementary: teams often use Terraform to provision the infrastructure and Ansible to configure the machines on top, rather than treating them as competitors.
- Terraform: strong at provisioning cloud infrastructure
- Terraform: explicit state tracking and change planning
- Ansible: strong at configuring software on existing hosts
- Ansible: agentless, no state file to manage for config
- Both are declarative-friendly and version-controllable
- They combine well: provision with Terraform, configure with Ansible
AI Mentor Explanation
Terraform is like the groundsmen who build the entire stadium — laying the pitch, erecting stands, and installing floodlights — creating the venue from nothing. Ansible is like the team management who then set up the dressing rooms, arrange the kit, and prepare the players inside that finished stadium. One brings the ground into existence; the other configures everything within it, and a full match day needs both roles working together.
Step-by-Step Explanation
Step 1
Identify the goal
Decide whether you are creating infrastructure (provisioning) or setting up software on it (configuration).
Step 2
Provision with Terraform
Declare cloud resources — VMs, networks, load balancers — and let Terraform's state engine create them.
Step 3
Hand off to Ansible
Once servers exist, use Ansible to install packages, deploy apps, and enforce their configuration.
Step 4
Manage state appropriately
Terraform relies on a state file to track resources; Ansible needs no state file for configuration tasks.
Step 5
Combine in a pipeline
Chain Terraform then Ansible in CI/CD so infrastructure and its configuration are both codified.
What Interviewer Expects
- Provisioning versus configuration management distinction
- Terraform's state file and declarative planning model
- Ansible's agentless, task-ordered idempotent model
- Recognition that the tools are complementary, not rivals
- A sensible example of using both together
Common Mistakes
- Saying one tool is strictly better than the other
- Claiming Ansible cannot provision at all (it can, but it is not its strength)
- Forgetting that Terraform maintains an explicit state file
- Treating them as direct competitors instead of complementary tools
Best Answer (HR Friendly)
“Terraform is best for creating infrastructure like servers and networks from scratch, while Ansible is best for setting up and configuring the software on machines that already exist. Many teams use them together: Terraform builds the environment and Ansible configures it.”
Code Example
resource "aws_instance" "web" {
ami = "ami-0abcd1234"
instance_type = "t3.micro"
tags = {
Name = "web-server"
Role = "app"
}
}---
- name: Configure the provisioned web server
hosts: web
become: true
tasks:
- name: Install and start nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Ensure nginx is running
ansible.builtin.service:
name: nginx
state: started
enabled: trueFollow-up Questions
- Why does Terraform keep a state file and Ansible generally does not?
- Can Ansible provision cloud infrastructure? What are the trade-offs?
- What does 'declarative vs procedural' mean for these tools?
- How would you combine Terraform and Ansible in a CI/CD pipeline?
- When would you choose Ansible over Terraform for a task?
MCQ Practice
1. What is Terraform primarily used for?
Terraform's core strength is provisioning and managing infrastructure resources through a declarative, state-driven model.
2. Which tool maintains an explicit state file to track resources?
Terraform records managed resources in a state file to plan and apply changes; Ansible does not use a state file for configuration.
3. How are Ansible and Terraform best described together?
They complement each other: Terraform provisions the infrastructure and Ansible configures the machines running on it.
Flash Cards
Ansible vs Terraform: core focus? — Ansible configures software on existing servers; Terraform provisions infrastructure like servers and networks.
Which tool uses a state file? — Terraform tracks resources in a state file; Ansible generally needs no state file for configuration tasks.
Are Ansible and Terraform competitors? — No — they are complementary. Provision with Terraform, then configure with Ansible.
Declarative vs procedural? — Terraform is declarative and state-driven; Ansible runs ordered, idempotent tasks to reach desired state.