Terraform vs Ansible: what's the difference, and when to use each
Terraform provisions infrastructure: you declare the cloud resources you want in HCL, and Terraform plans and applies the changes, tracking what it manages in a state file. Ansible configures systems: it runs ordered tasks from YAML playbooks over SSH to install packages, edit files and start services. Most teams use both, Terraform to create the servers and Ansible to set them up.

The short answer: provisioning vs configuration management
Both tools are open about what they're best at, and the split is fairly clean:
- Terraform is an infrastructure-as-code tool for provisioning. It talks to cloud and SaaS APIs through providers and manages the lifecycle of resources: VPCs, subnets, instances, buckets, DNS records, IAM roles, Kubernetes clusters. It creates them, updates them, and destroys them when you remove them from the code.
- Ansible is an automation tool best known for configuration management. It connects to machines (usually over SSH) and brings them to a described state: packages installed, config files templated, users created, services running. It is also used for application deployment and one-off operational jobs such as rolling restarts.
There is overlap. Ansible has cloud modules that can create an EC2 instance, and Terraform can run a boot script on a new server. But each tool is designed around a different job, and that shows in how they track state, how they handle removal, and how their code reads.
Ownership is now shared at the top: Ansible is sponsored by Red Hat, part of IBM since 2019, and HashiCorp, which makes Terraform, became an IBM company in February 2025. The two remain separate products with separate licences: Ansible is GPL v3, and Terraform has used the Business Source License (BUSL 1.1) since version 1.6.
Terraform vs Ansible side by side
| Terraform | Ansible | |
|---|---|---|
| Main job | Provisioning cloud infrastructure | Configuring servers, deploying apps, running operational tasks |
| Language | HCL (.tf files) | YAML playbooks, with Jinja2 templating |
| Style | Declarative: describe the end result | Tasks run in order; most modules are idempotent and declare a target state |
| State | State file records every managed object | No state file; checks the target on each run |
| Preview | terraform plan | --check --diff (check mode), where modules support it |
| Removing something | Delete the block; the next plan destroys it | Write a task with state: absent |
| Talks to | Cloud and SaaS APIs via providers | Hosts over SSH (WinRM for Windows), plus API modules |
| Agents | None; runs where you run the CLI | None on managed nodes; runs from a control node |
| Licence | BUSL 1.1 (1.6 and later) | GPL v3 |
| Company | HashiCorp, an IBM company | Red Hat, an IBM company |
Declarative vs ordered tasks
A Terraform configuration is a description of what should exist. The order of blocks in your files doesn't matter: Terraform builds a dependency graph from the references between resources (a subnet refers to a VPC, so the VPC comes first) and works out the order itself, running independent changes in parallel.
An Ansible playbook is a list of plays, and each play is a list of tasks that run top to bottom on each host. The order is yours to choose, which suits configuration work: install the package, then write its config file, then restart the service. Each task calls a module, and good modules are idempotent: ansible.builtin.package with state: present installs nginx only if it's missing, so running the playbook twice makes no changes the second time. Ansible's documentation calls this out directly: a playbook run multiple times changes nothing once the system is in the described state.
So Ansible is often described as procedural and Terraform as declarative. The more precise version: both describe desired state, but Terraform describes a whole set of resources and plans the path, while Ansible describes a sequence of steps, each of which is usually declarative on its own.
State: Terraform remembers, Ansible checks
The biggest practical difference is state.
Terraform keeps a state file that maps each resource in your code to a real object, with its ID and attributes. On every plan it refreshes that state from the provider, compares it with the configuration, and proposes the difference. Because Terraform knows what it created, removing a resource block from the code means the next plan destroys that object. The cost is that the state file needs care: a remote backend, locking, and no hand editing.
Ansible has no state file. Each run, every task inspects the target (is the package installed? does the file have this content?) and acts only if needed. Nothing is remembered between runs, so there's nothing to store or lock. The flip side: Ansible only manages what the current playbook mentions. Delete the task that installed nginx and nginx stays installed; you have to write a task that removes it. The same goes for cloud resources created with Ansible's cloud modules.
Mutable vs immutable infrastructure
The tools also lean towards different patterns.
Ansible fits a mutable model: a server lives for a long time and you change it in place, running playbooks to patch packages or update configuration. It works well for fleets of long-lived machines, network devices and on-premises hosts.
Terraform fits an immutable model: when something fundamental changes, replace the resource rather than modify it. A new machine image means a new instance, and Terraform's plan shows it as -/+ (destroy and recreate) or, with create_before_destroy, +/-. Teams that bake images with a tool like Packer often need little or no configuration management after boot.
Neither pattern is required by either tool, but knowing which one your team follows usually settles the Terraform vs Ansible question.
The same task in both: an EC2 instance running nginx
Terraform
Terraform creates the instance and can pass a boot script as user_data, which cloud-init runs on first boot:
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
subnet_id = var.subnet_id
user_data = <<-EOF
#!/bin/bash
dnf install -y nginx
systemctl enable --now nginx
EOF
tags = {
Name = "web-1"
}
}
output "public_ip" {
value = aws_instance.web.public_ip
}terraform init
terraform plan
terraform applyThe script only runs on first boot. Editing it later doesn't re-run it on the existing instance unless you set user_data_replace_on_change = true, in which case Terraform replaces the instance. That is the immutable pattern: fine for a simple bootstrap, awkward for ongoing configuration.
Ansible
Ansible can launch the instance with the amazon.aws.ec2_instance module, run from the control node against the AWS API:
- name: Launch the web server
hosts: localhost
gather_facts: false
tasks:
- name: Launch an EC2 instance
amazon.aws.ec2_instance:
name: web-1
image_id: "{{ ami_id }}"
instance_type: t3.micro
vpc_subnet_id: "{{ subnet_id }}"
key_name: deploy-key
region: eu-west-2
state: running
wait: trueOn a second run the module finds the existing instance (by default it matches on the Name tag, AMI, state and subnet) and changes nothing. To remove it you change state to absent; deleting the task does nothing.
Installing nginx is where Ansible is at home:
- name: Configure web servers
hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Start nginx and enable it at boot
ansible.builtin.service:
name: nginx
state: started
enabled: trueansible-playbook -i inventory.ini web.yml --check --diff
ansible-playbook -i inventory.ini web.ymlChange the playbook and run it again, and Ansible updates the running server in place. Most teams would write the instance in Terraform and the nginx play in Ansible, which leads to the next section.
Using Terraform and Ansible together
The common split: Terraform provisions the network, instances, load balancers and DNS; Ansible configures what runs on the instances. The join between them is the inventory, the list of hosts Ansible connects to. There are a few ways to build it from Terraform:
- Outputs. Export the addresses with an
outputblock and read them withterraform output -jsonin a script that writes the inventory file. - Dynamic inventory from the cloud. Ansible's
amazon.aws.aws_ec2inventory plugin queries AWS directly and can group hosts by the tags Terraform set. Terraform doesn't need to know about Ansible at all. - Inventory from Terraform state. The Ansible-maintained
ansible/ansibleTerraform provider has anansible_hostresource, and thecloud.terraform.terraform_providerinventory plugin builds an inventory from those resources in state:
resource "ansible_host" "web" {
name = aws_instance.web.public_ip
groups = ["web"]
}# inventory.yml
plugin: cloud.terraform.terraform_provider
project_path: ../infraThe cloud.terraform collection also has a terraform module, so an Ansible playbook can run a Terraform plan and apply as one of its steps. Whichever way you join them, keep the boundary clear: one tool owns each resource. If Ansible changes something Terraform manages, the next terraform plan shows it as drift and proposes putting it back.
When to choose Terraform, Ansible, or both
- Choose Terraform to create and change cloud infrastructure across AWS, Azure, Google Cloud and SaaS services, when you want a reviewed plan before every change, and when removing code should remove the resource.
- Choose Ansible to configure operating systems and applications on existing machines, to manage on-premises servers and network devices, and for ordered operational runbooks (patching, rolling restarts, deployments).
- Use both when you run long-lived servers in the cloud: Terraform for the infrastructure, Ansible for what runs on it.
- You may need neither for configuration if you run containers or serverless functions, or bake images: Terraform plus a container platform often covers it.
If you're learning for a cloud infrastructure role, the concepts in Terraform (plans, state, providers, modules) carry over to most infrastructure-as-code tools, and it has its own certification, the Terraform Associate. Start with what Terraform is and the Terraform tutorial.
Try the Terraform side in a Lab
Planfile teaches the Terraform half of this page on your iPhone. Its Labs run a simulated AWS provider (EC2 instances, S3 buckets and more), so you can write resources, read plans and inspect state without an AWS account; the first two chapters, including Providers and resources, are free. It covers Terraform only, not Ansible.
Questions
What is the difference between Terraform and Ansible?
Terraform provisions infrastructure declaratively and tracks it in a state file; Ansible configures systems by running ordered, idempotent tasks from YAML playbooks over SSH and keeps no state between runs.
Ansible vs Terraform: which is better?
Neither is better overall. Terraform is better at creating and changing cloud resources; Ansible is better at configuring the software on servers. Many teams use both.
Can Ansible replace Terraform?
Partly. Ansible's cloud modules can create resources, but without a state file it won't remove resources you delete from a playbook, and it has no whole-infrastructure plan like terraform plan.
Can Terraform replace Ansible?
For simple bootstrapping, yes: user_data or a baked machine image can install software on first boot. For ongoing configuration of long-lived servers, Ansible is the better fit.
Is Ansible agentless? Is Terraform?
Both are. Ansible connects to managed nodes over SSH or WinRM with no agent installed, and Terraform calls cloud APIs from wherever you run the CLI.
Are Terraform and Ansible owned by the same company?
Both are now part of IBM: Ansible through Red Hat (acquired in 2019) and Terraform through HashiCorp (acquisition completed in February 2025). They remain separate products.