DevOps Tools
Ansible vs Terraform: When to Use Which
Terraform provisions infrastructure -- it creates servers, networks, databases, and cloud resources. Ansible configures infrastructure -- it installs packages, manages files, and controls services on servers that already exist. They solve different problems, and the right answer is usually to learn Terraform first, then add Ansible when your role requires configuration management.
This confusion is one of the most common in DevOps. Both tools are labelled "Infrastructure as Code," both use declarative-ish configuration files, and both automate infrastructure tasks. But their purposes, architectures, and ideal use cases are fundamentally different. This article clears up the distinction.
What Terraform does
Terraform is an infrastructure provisioning tool. It creates and manages cloud resources -- the building blocks of your infrastructure.
When you write Terraform code, you are describing what cloud resources should exist:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.medium"
tags = {
Name = "web-server"
}
}
resource "aws_rds_instance" "database" {
engine = "postgres"
instance_class = "db.t3.medium"
allocated_storage = 20
}
Terraform reads this, compares it to its state file (a record of what already exists), calculates the difference, and makes the minimal changes needed. If the server already exists with the right configuration, Terraform does nothing. If it does not exist, Terraform creates it. If it exists but differs, Terraform updates it.
Key characteristics:
- Declarative -- you describe the desired end state, not the steps to get there
- State-aware -- Terraform tracks every resource it manages in a state file
- Plan before apply --
terraform planshows exactly what will change before you commit - Cloud-native -- designed specifically for provisioning cloud resources across AWS, Azure, GCP, and hundreds of other providers
- Idempotent -- running the same configuration twice produces the same result with no side effects
For a deeper introduction to Terraform, see our Terraform for beginners guide.
What Ansible does
Ansible is a configuration management tool. It manages what happens on servers after they exist -- installing packages, copying files, starting services, and enforcing system state.
When you write Ansible code, you are describing what the state of a server should look like:
- name: Configure web server
hosts: webservers
become: yes
tasks:
name: Install Nginx
apt:
name: nginx
state: present
name: Copy Nginx configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx
name: Ensure Nginx is running
service:
name: nginx
state: started
enabled: yes
handlers:
name: restart nginx
service:
name: nginx
state: restarted
Ansible connects to servers over SSH (no agent required), executes tasks in order, and ensures the server matches the described state.
Key characteristics:
- Procedural with declarative modules -- tasks execute in order, but individual modules are declarative (e.g., "ensure this package is present")
- Agentless -- connects over SSH, no software to install on managed servers
- No state file -- Ansible checks the current state of each server every time it runs
- Playbook-based -- YAML files (playbooks) describe the desired configuration
- Idempotent -- well-written playbooks can run repeatedly without side effects
The full comparison
| Feature | Ansible | Terraform |
|---|---|---|
| Primary purpose | Configure servers (packages, files, services) | Provision cloud infrastructure (servers, networks, databases) |
| Approach | Procedural (tasks run in order) | Declarative (describe end state) |
| State management | No state file checks live state each run | State file tracks all managed resources |
| Language | YAML (playbooks) | HCL (HashiCorp Configuration Language) |
| Agent required | No connects over SSH | No runs locally, calls cloud APIs |
| Change preview | Check mode (--check), limited accuracy | terraform plan exact preview of changes |
| Cloud support | Cloud modules exist but are not the primary use case | Purpose-built for multi-cloud provisioning |
| Server configuration | Purpose-built for server configuration | Limited user_data scripts or provisioners (discouraged) |
| Learning curve | Low YAML is familiar, SSH is straightforward | Low to moderate HCL is simple, but state management requires understanding |
| Rollback capability | No native rollback write a reverse playbook | terraform destroy or revert code and re-apply |
| Community and ecosystem | Large Ansible Galaxy has thousands of roles | Large Terraform Registry has thousands of modules and providers |
| Job market demand (2026) | Mentioned in ~30-40% of DevOps job postings | Mentioned in ~65-70% of DevOps job postings |
The core distinction: Terraform answers "what infrastructure should exist?" Ansible answers "how should that infrastructure be configured?"
When to use Terraform
Use Terraform when you need to create, modify, or destroy cloud resources:
- Provisioning cloud infrastructure -- VPCs, subnets, security groups, EC2 instances, RDS databases, S3 buckets, Lambda functions, IAM roles
- Managing Kubernetes clusters -- creating EKS, AKS, or GKE clusters with node groups, networking, and IAM
- DNS and CDN setup -- Route 53 records, CloudFront distributions, Cloudflare configuration
- Multi-environment infrastructure -- creating identical dev, staging, and production environments from the same code
- Cost management -- Terraform can destroy entire environments when not needed, preventing idle resource costs
The rule of thumb: If you are creating something in a cloud provider's console, that task belongs to Terraform.
When to use Ansible
Use Ansible when you need to configure and manage servers after they exist:
- Installing and configuring software -- Nginx, PostgreSQL, Java, Node.js, monitoring agents
- Managing configuration files -- templating and deploying config files across multiple servers
- User management -- creating users, managing SSH keys, setting permissions
- Security hardening -- applying CIS benchmarks, configuring firewalls, managing certificates
- Application deployment (non-containerised) -- deploying code to servers, running database migrations, restarting services
- Multi-server orchestration -- running tasks across hundreds of servers simultaneously
The rule of thumb: If you would SSH into a server to do it manually, that task belongs to Ansible.
When to use both together
The most powerful pattern combines both tools in a pipeline:
Terraform Ansible
│ │
├── Create VPC ├── Install packages
├── Create security groups ├── Configure Nginx
├── Provision EC2 instances ──> ├── Deploy application
├── Create RDS database ├── Set up monitoring agent
├── Configure load balancer ├── Harden security
│ │
└── Infrastructure exists └── Infrastructure is configured
Step 1: Terraform creates the cloud infrastructure -- networks, servers, databases, load balancers.
Step 2: Terraform outputs the IP addresses and connection details of the newly created resources.
Step 3: Ansible uses those details to connect to the servers and configure them -- install packages, deploy applications, configure services.
This separation is clean: Terraform owns the cloud layer, Ansible owns the server layer. Each tool does what it was designed for.
Example: Terraform output feeding Ansible inventory
# Terraform outputs the server IPs
output "web_server_ips" {
value = aws_instance.web[*].public_ip
}
# Ansible dynamic inventory uses those IPs
# inventory.yml
webservers:
hosts:
web-1:
ansible_host: 52.14.100.10
web-2:
ansible_host: 52.14.100.11
In practice, teams often use dynamic inventory scripts that automatically discover servers created by Terraform, eliminating manual IP management entirely.
When you might not need Ansible at all
Here is the important nuance: if your workloads are fully containerised, you may not need Ansible.
In a containerised workflow:
- Terraform provisions the cloud infrastructure (EKS cluster, networking, IAM)
- Docker packages the application and all its dependencies into a container image
- Kubernetes deploys and manages the containers
There are no servers to SSH into. No packages to install. No configuration files to template. The container image contains everything. Kubernetes handles deployment, scaling, and service management.
This is why Terraform demand has outpaced Ansible in job postings. The industry trend towards containers and Kubernetes has reduced the need for traditional server configuration management. Ansible remains essential for:
- Organisations with legacy (non-containerised) applications
- Environments where servers are managed directly (bare metal, on-premises)
- Security hardening and compliance automation
- Network device configuration
- Hybrid environments mixing containers and traditional servers
If you are starting your DevOps career and need to prioritise, Terraform gives you more value in more job opportunities. Ansible is an excellent second tool to learn once you encounter scenarios that require it.
Common misconceptions
"Ansible is declarative, Terraform is declarative, so they are the same"
They are declarative at different levels. Terraform is declarative at the infrastructure level -- you describe the desired cloud resources and Terraform figures out the steps. Ansible playbooks are procedural -- tasks execute in the order you write them. Individual Ansible modules (like apt or service) are declarative ("ensure this package is present"), but the playbook as a whole is a sequence of steps.
This distinction matters because Terraform can calculate the minimal set of changes to reach the desired state. Ansible runs every task in order, checking conditions as it goes. For infrastructure provisioning, Terraform's approach is more efficient. For server configuration, Ansible's approach is more flexible.
"Terraform can configure servers with provisioners"
Terraform has a provisioner block that can run scripts on servers after creation. HashiCorp explicitly discourages this. The Terraform documentation states that provisioners are a "last resort." They do not integrate with Terraform's state management, they cannot be reliably re-run, and they break the declarative model.
If you need to configure servers, use the right tool -- Ansible, cloud-init, or container images.
"Ansible can create cloud resources, so it replaces Terraform"
Ansible has cloud modules (e.g., amazon.aws.ec2_instance) that can create AWS resources. However, Ansible was not designed for this. It lacks a state file, so it cannot calculate the minimal changes needed. It cannot show you a preview of what will change before applying. It cannot manage dependencies between resources as elegantly as Terraform.
You can use Ansible for basic cloud provisioning, but for anything beyond trivial setups, Terraform is dramatically better at the job.
Job market reality in 2026
The job market data is clear:
| Skill | Approximate % of DevOps job postings (2026) |
|---|---|
| Terraform | 65-70% |
| Ansible | 30-40% |
| Both required | 20-25% |
| CloudFormation | 20-30% (AWS-specific roles) |
| Pulumi | 5-10% (growing) |
Terraform appears in nearly twice as many job postings as Ansible. This does not mean Ansible is irrelevant -- it remains essential in many environments. But if you are choosing where to invest your learning time first, the data favours Terraform.
Roles that emphasise Ansible tend to be in:
- Large enterprises with legacy infrastructure
- Government and regulated industries
- On-premises or hybrid cloud environments
- Network automation
Roles that emphasise Terraform tend to be in:
- Cloud-native organisations
- Startups and scale-ups
- Platform engineering teams
- Multi-cloud environments
Many senior DevOps roles require both. But you reach "employable" faster with Terraform.
Recommended learning path
-
Learn Terraform first (2-3 weeks) -- Provision AWS infrastructure. Understand providers, resources, variables, state, and modules. See our Terraform for beginners guide.
-
Learn Docker and Kubernetes (4-5 weeks) -- Containerisation reduces the need for Ansible. Understand when containers eliminate traditional server management. See Docker vs Kubernetes.
-
Learn Ansible when needed (1-2 weeks) -- If your role involves managing non-containerised servers, add Ansible. Focus on inventory, playbooks, roles, and templates.
-
Learn both together (ongoing) -- In production environments, practice the Terraform-provisions-then-Ansible-configures pattern.
This sequence maximises your employability while building skills in the order they deliver the most value. The full DevOps tools guide maps out how these tools fit alongside CI/CD, monitoring, and cloud platforms.
Frequently Asked Questions
Ola
Founder, CloudPros
Building the most hands-on DevOps bootcamp for the AI era. 16 weeks of real infrastructure, real projects, real career outcomes.
