What is the difference between Terraform and configuration tools like Ansible?
Understand the difference between Terraform and Ansible, provisioning versus configuration management, and how the two tools complement each other in DevOps.
Expected Interview Answer
Terraform is a declarative provisioning (infrastructure orchestration) tool that creates and manages the lifecycle of infrastructure resources, while Ansible is primarily a configuration management tool that installs software and configures the state inside existing machines.
Terraform focuses on building the infrastructure itself — VMs, networks, load balancers, databases — using a state file and a declarative model to reconcile desired versus actual resources. Ansible focuses on what runs on that infrastructure, applying packages, files, and services, typically in a more procedural, task-by-task fashion. They overlap at the edges, but in practice teams often use Terraform to provision the servers and Ansible to configure them, so the tools are complementary rather than mutually exclusive.
- Clarifies provisioning versus configuration responsibilities
- Terraform tracks resource lifecycle and drift via state
- Ansible excels at in-machine software configuration
- The two can be combined in one pipeline
- Choosing the right tool reduces accidental complexity
AI Mentor Explanation
Terraform is like building the stadium — laying the pitch, stands, and floodlights — while Ansible is like preparing the players inside it, kitting them out and drilling tactics. Provisioning the ground and configuring the team are different jobs: one creates the venue's lifecycle, the other sets up what happens on the field once the ground exists.
Step-by-Step Explanation
Step 1
Identify the layer
Decide whether the task is creating infrastructure (Terraform) or configuring software on existing machines (Ansible).
Step 2
Provision with Terraform
Use Terraform to declaratively create VMs, networks, and managed services and track them in state.
Step 3
Configure with Ansible
Use Ansible playbooks to install packages, push config files, and manage services on the provisioned hosts.
Step 4
Pass outputs across
Feed Terraform outputs (like IPs or hostnames) into an Ansible inventory to bridge provisioning and configuration.
Step 5
Automate the pipeline
Run Terraform then Ansible in CI so infrastructure is built and configured in one repeatable flow.
What Interviewer Expects
- Distinction between provisioning and configuration management
- Understanding that Terraform is declarative and state-driven
- Awareness that Ansible is largely procedural and agentless
- Recognition that the tools are complementary
- A practical example of using both together
Common Mistakes
- Treating Terraform and Ansible as interchangeable competitors
- Saying Ansible cannot provision infrastructure at all
- Ignoring Terraform's state file in the comparison
- Claiming one tool fully replaces the other in every scenario
Best Answer (HR Friendly)
“Terraform builds the servers and cloud resources themselves, while Ansible sets up the software and settings inside those servers. They are not really rivals — many teams use Terraform to create the environment and Ansible to configure it.”
Code Example
# Terraform: create the server (main.tf)
resource "aws_instance" "app" {
ami = "ami-0abcdef1234567890"
instance_type = "t3.small"
}
output "app_ip" {
value = aws_instance.app.public_ip
}
# Ansible: configure it (playbook.yml)
- hosts: app
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: presentFollow-up Questions
- Can Terraform do configuration management with provisioners?
- Is Ansible declarative or procedural, and why does it matter?
- How do you pass Terraform outputs into an Ansible inventory?
- When would you choose only Ansible and skip Terraform?
- How does state tracking differ between the two tools?
MCQ Practice
1. What is Terraform's primary role?
Terraform's main job is declaratively provisioning and managing the lifecycle of infrastructure resources.
2. What is Ansible primarily designed for?
Ansible is chiefly a configuration management tool that configures the state inside existing hosts.
3. How are Terraform and Ansible best characterized together?
They address different layers and are commonly used together, Terraform to provision and Ansible to configure.
Flash Cards
Terraform's core focus? — Provisioning and managing the lifecycle of infrastructure resources declaratively via state.
Ansible's core focus? — Configuration management — installing software and setting the state inside existing machines.
Are they competitors? — Mostly complementary; teams often provision with Terraform and configure with Ansible.
Key model difference? — Terraform is declarative and state-driven; Ansible is largely procedural and agentless.