Introduction
Ansible vs Terraform is the comparison every DevOps engineer is asked about, and it is mostly not a contest. The two tools answer different questions. Terraform builds infrastructure and remembers what it built. Ansible configures machines and network devices that already exist, and remembers nothing. A team that treats them as rivals will pick one and then fight its limitations; a team that treats them as complementary will use both, each for the job it is good at, and hand off between them cleanly.
This guide compares the two tools as they stand on 30 November 2026: ansible-core 2.22.0, released 2 November 2026, with the Ansible 14.6.0 community package on ansible-core 2.21 and Ansible 15 in pre-release; and Terraform 1.17.2, released 11 November 2026. It covers what each tool actually does, how they differ on language, state, workflow and licensing, when to use each one, and how they work together in a real pipeline — including the handoff pattern and the tooling that connects them. If you are new to either tool, our Ansible Tutorial for Beginners and our Terraform Tutorial for Beginners are the starting points; if you are choosing between infrastructure-as-code tools more broadly, our Terraform vs Pulumi vs OpenTofu covers the other side of the decision.
The short answer: Terraform provisions cloud infrastructure declaratively, with state. Ansible configures what runs inside it, procedurally, over SSH, with no state. Learn Terraform first for cloud infrastructure, Ansible first for managing servers, and both if you want to be hireable — the job that asks for one usually expects the other as well.
What Each Tool Actually Does
Terraform is a provisioning tool. You declare the end state of cloud objects in HCL — a VPC, a subnet, a database, a load balancer — and Terraform compares that desired state with the state file it keeps, works out the difference, and creates, changes or destroys whatever is needed to close the gap. Run it twice with no code change and the second run does nothing, because the desired state and the real state already agree. That property, convergence against recorded state, is the whole value of the tool. Terraform talks to provider APIs: it does not log into machines, it calls the AWS, Azure or Google Cloud API and asks for an object to exist.
Ansible is a configuration and orchestration tool. It connects to machines that already exist — over SSH for Linux, WinRM for Windows, or vendor APIs for network gear — with no agent installed on the target, runs tasks in order, and each module is written to be safe to rerun. A playbook is a list of plays, each mapping a group of hosts to a list of tasks, and each task calls a module: install a package, write a configuration file from a template, restart a service, create a user. Ansible's control node pushes small Python modules to the target, runs them, and collects the results as JSON. The target needs Python for Linux hosts, and nothing else — there is no agent to install, upgrade or secure.
The overlap that confuses people is that Ansible ships cloud modules, so you can create an EC2 instance from a playbook. You can. But Ansible keeps no state file, so it has no memory of what it built, and deletion, drift detection and dependency ordering all become your problem. Creating an instance is easy; knowing it exists, knowing what has changed since, and deleting it cleanly a month later are the hard parts, and they are the parts Terraform's state file exists to solve. The same is true in reverse: Terraform has provisioners that can run commands on a machine after creating it, but they are a last resort, not a configuration management system, and HashiCorp's own guidance is to use a dedicated tool for configuration instead.
The sentence that settles most arguments is this: Terraform answers "what infrastructure should exist?", and Ansible answers "what state should this machine be in?". They are different questions, and a team that asks both needs both tools.
Language — Declarative HCL vs Procedural YAML
The language difference is the first thing a new user feels, and it follows from the job difference. Terraform's language is HCL, the HashiCorp Configuration Language: declarative, describing an intended goal rather than the steps to reach it. A resource block says what you want — a virtual machine of a given size in a given subnet — and Terraform works out the order, the dependencies and the API calls. HCL is a small language by design: it describes infrastructure and little else, so a configuration file is readable by anyone on the team, including people who do not program. Its limits are a feature: there is no way to write a loop that nobody understands, because the language does not offer one in the general-purpose sense. Repetition is handled by for_each and count, and by modules, which are folders of configuration with inputs and outputs.
Ansible's language is YAML plus Jinja2 templating. A playbook is a list of tasks that run in the order you wrote them, top to bottom, and each task is a module call with arguments. That makes Ansible procedural in feel even though its modules are idempotent — each module checks the current state and changes it only if needed, so running the playbook twice is safe, but the order of operations is yours to decide. The procedural style is a strength for orchestration: a rolling deploy that updates one server, waits for the health check, updates the next, and rolls back if the check fails is a sequence of steps, and YAML expresses a sequence naturally. It is also the source of Ansible's most common failure mode: a playbook that depends on the order of its tasks is fragile when someone reorders them, and a playbook with complex conditionals and loops in Jinja2 becomes a program written in a templating language, which is nobody's idea of a good time.
The choice follows the author. If the authors are infrastructure engineers, HCL's limits are a feature — the configuration is readable, reviewable and hard to misuse. If they are developers who already write YAML and Jinja2, Ansible's playbooks feel natural. Neither language is better in the abstract; the question is which one the maintaining team already reads fluently.
State — The Difference That Decides Most Arguments
State is the difference that matters most, and it is the one teams underestimate when they choose. Terraform keeps a state file: a record of every resource it manages, mapping your configuration to real resource IDs. The state file is what makes the plan possible — Terraform compares the desired configuration against the state, and against the real infrastructure, and shows you the difference before anything changes. It is what makes deletion possible — Terraform knows what it created, so it can destroy exactly those things and nothing else. And it is what makes drift detection possible — when someone changes a resource outside Terraform, the next plan notices. State must be stored remotely, locked during applies so two engineers cannot change the same infrastructure at once, and treated as sensitive, because it can contain secrets in plain text. Our Terraform Tutorial for Beginners covers the workflow, and the Terraform State Explained guide covers backends, locking and state surgery in detail.
Ansible has no state file. It queries the live system on every run: it checks whether the package is installed, whether the file has the right content, whether the service is running, and it changes what does not match. That is why Ansible is stateless, and it is both its strength and its weakness. The strength is simplicity: there is no state to store, lock, back up or lose, and there is no state file to go stale. The weakness is that Ansible cannot tell you what it built, what has changed since, or what to delete. If a playbook creates an EC2 instance and the playbook is deleted, the instance remains, and nothing in Ansible knows it exists. If a server is changed by hand, Ansible will put it back on the next run — which is good for configuration drift — but it cannot show you a plan of the change before it happens, the way terraform plan can.
The practical consequence is that Terraform is the tool for things with a lifecycle — create, change, destroy — and Ansible is the tool for things with a desired state — installed, configured, running. Cloud resources have a lifecycle, so they belong to Terraform. Machine configuration has a desired state, so it belongs to Ansible. A team that tries to manage a lifecycle with Ansible ends up writing its own state tracking, badly. A team that tries to configure machines with Terraform ends up fighting provisioners, which HashiCorp itself describes as a last resort.
Workflow, Preview and Idempotency
The two tools also differ in how you preview a change before making it, and that difference shapes how teams review each other's work. Terraform's preview is terraform plan: a complete diff showing what will be created, changed and destroyed, with the exact attribute changes for each resource. In a team, the plan is the artifact that gets reviewed like a code change — attached to a pull request, read by a second engineer, approved before the apply runs. It is the single best feature of the Terraform workflow for reliability, because it makes infrastructure changes reviewable in the same way code changes are.
Ansible's preview is --check mode, run with --diff to show what would change. It is useful, and it should be part of every playbook run in a pipeline, but it is weaker than a plan: whether a module can predict its change depends on the module, and some modules cannot know what they would do without doing it. A template module can show the diff of a file it would write; a command module cannot predict the effect of the command it would run. The practical result is that Ansible changes are reviewed as code — the playbook diff — rather than as a preview of the effect, and the effect is verified after the run, not before it.
Both tools are idempotent in the sense that matters: running the same configuration twice should produce the same result. Terraform achieves it through state and the plan; Ansible achieves it through modules that check the current state before acting. The difference is what happens when the world has changed since the last run. Terraform detects the drift and proposes to correct it, showing you the difference first. Ansible corrects the drift silently on the next run, which is convenient for configuration and dangerous for anything where the change deserves a review. That is another way of saying the same thing: Terraform is for changes you want to review, Ansible is for changes you want to converge.
The execution model differs too. Terraform runs from a single binary and plans fast, because it works from state and API calls. Ansible runs over SSH, one host at a time by default, with five parallel forks — slow for a large fleet, and the first setting every Ansible user learns to raise. The agentless model is a real advantage for adoption, but SSH round-trips cost at scale, and a fleet of hundreds needs the parallelism tuned and the playbooks written efficiently.
Head-to-Head Comparison
The table below puts the comparison in one place. Read it as a summary of the sections above, not as a scorecard — no tool wins every row, and the row that matters most depends on your team.
| Factor | Ansible | Terraform |
|---|---|---|
| Primary job | Configuration management and orchestration | Infrastructure provisioning |
| Paradigm | Procedural tasks with idempotent modules | Declarative desired state |
| Language | YAML playbooks with Jinja2 | HCL |
| State management | Stateless — queries live systems | State file, local or remote backend |
| Change preview | --check / --diff mode, module-dependent |
terraform plan, full diff |
| Agent on targets | None — SSH, WinRM, vendor APIs | None — provider APIs |
| Python on targets | Needed for Linux targets | Not needed |
| Control node on Windows | Needs WSL | Native Windows builds |
| Licence | GPLv3 (ansible-core open source) | Business Source License 1.1 (OpenTofu fork: MPL-2.0) |
| Maintainer | Red Hat (IBM) | HashiCorp (IBM) |
| Managed offering | Ansible Automation Platform, priced per managed node | HCP Terraform, free tier up to 500 managed resources |
| Current version (30 Nov 2026) | ansible-core 2.22.0; Ansible 14.6.0 | Terraform 1.17.2 |
| Certification | Red Hat Certified Engineer in Ansible | HashiCorp Certified: Terraform Associate (004) |
| Best for | Configuring servers, network devices, ad-hoc orchestration | Provisioning cloud resources with reviewable plans |
Licensing deserves a paragraph, because it is a legal question, not a technical one. Ansible-core is GPLv3 open source, and Red Hat's commercial product is the Automation Platform, which adds a web UI, role-based access control, execution environments and support, priced per managed node through sales quotes. Terraform has been under the Business Source License 1.1 since version 1.6.0: it is free to use, but the licence restricts competing commercial offerings built on it. OpenTofu is the MPL-2.0 fork under the Linux Foundation, for organisations whose open-source policy rules out BUSL-licensed software — our Terraform vs Pulumi vs OpenTofu covers that fork in detail. If your organisation has a licensing policy, involve your legal or procurement team before committing, because this is a decision that is expensive to revisit.
Cost is simpler than it looks. Both tools are free in their open-source forms — ansible-core and the Terraform CLI cost nothing — and both vendors earn from a managed service. HCP Terraform's free tier covers up to 500 managed resources; beyond that it bills per resource. Ansible Automation Platform is quoted per managed node, with no public list price. For a team starting out, the cost that matters is the team's time, which is why the hiring and familiarity rows matter more than the price rows.
When to Use Each — and When to Use Neither Alone
The choice is a team decision, and it follows from the workload. Work through the questions below in order, and the answer usually emerges on its own.
Are you creating cloud resources — VPCs, subnets, databases, load balancers, Kubernetes clusters? Use Terraform. It is the tool for provisioning, it has the provider ecosystem for every cloud platform, it shows you a plan before it changes anything, and it remembers what it created so it can delete it. Ansible can create a cloud resource, but it cannot manage its lifecycle, and a cloud estate managed by playbooks is an estate nobody can delete cleanly.
Are you configuring machines that already exist — installing packages, writing configuration files, managing users and services? Use Ansible. It is the tool for configuration, it needs no agent, it runs over SSH, and its playbooks express a sequence of steps naturally. Terraform's provisioners can run a command on a machine, but they are a last resort, and a configuration managed by provisioners is a configuration nobody can review or rerun safely.
Are you managing network devices — switches, routers, firewalls? Use Ansible. Its network collections speak the devices' own interfaces — CLI over SSH, or NETCONF — and its resource modules describe the desired state of a VLAN or an interface and work out the commands themselves. Terraform has providers for network devices too, but the Ansible ecosystem for network automation is deeper, and the operational pattern — change a device, verify it, move to the next — is Ansible's natural shape.
Are you orchestrating a multi-step process across many machines — a rolling deploy, a failover, a patching window? Use Ansible. Playbooks are ordered lists of plays, and orchestration is ordering. Terraform's graph model is excellent for dependencies between resources and poor for sequences of human-judged steps, which is why the handoff pattern below puts orchestration on the Ansible side.
Are you doing ad-hoc work — running one command on fifty servers, or restarting a service everywhere? Use Ansible's ad-hoc mode: ansible all -m ping, ansible webservers -a "systemctl restart nginx". There is no Terraform equivalent, which is reason enough to learn Ansible even if Terraform provisions everything.
The honest answer to "which one should I learn first" depends on the job you want. If you are aiming at cloud and platform roles, learn Terraform first: it owns the provisioning layer most cloud roles hire for, and the HashiCorp Certified: Terraform Associate (004) is the entry credential. If you are aiming at systems and network roles, learn Ansible first: it is the tool those teams use daily, and the Red Hat Certified Engineer path is the credential. If you can learn both — and you should, because the two together are the standard stack — learn Terraform's workflow first and Ansible's playbooks second, because the provisioning decision shapes the architecture and the configuration fills it in.
How They Work Together — the Handoff Pattern
In practice, the two tools meet at a handoff: Terraform builds the machines and emits outputs, Ansible reads those outputs and configures them. The pattern is simple, it is the industry standard, and it is worth setting up properly rather than improvising.
Terraform's side of the handoff is its outputs. After the apply, Terraform knows the IP addresses, the hostnames and the IDs of everything it created, and it exposes them as outputs:
output "web_server_ips" {
description = "IP addresses of the web servers"
value = aws_instance.web[*].public_ip
}
Ansible's side is its inventory, and the clean way to connect the two is a dynamic inventory that reads Terraform's state. Red Hat publishes a certified collection for exactly this, installed with ansible-galaxy collection install cloud.terraform. It carries the cloud.terraform.terraform_provider inventory plugin, which builds an Ansible inventory straight from a Terraform state file, plus a terraform module for running Terraform from a playbook and a tf_output lookup for reading outputs. With that in place, the pipeline is: Terraform applies and writes state, Ansible reads the state as its inventory, and the playbook configures the machines Terraform just created — no hand-maintained inventory file, no copy-pasting IP addresses, no drift between the two tools' view of the world.
There is a second integration worth knowing: the Terraform Ansible provider (ansible/ansible), which lets Terraform call Ansible playbooks as part of an apply. It closes the gap in the other direction, for teams that want a single entry point. The general advice is to keep the boundary clean — Terraform provisions, Ansible configures, and the state file is the contract between them — rather than blurring it by embedding one tool inside the other. A blurred boundary is harder to debug: when a deploy fails, you want to know immediately whether the provisioning or the configuration failed, and a clean handoff gives you that answer.
A complete pipeline, then, looks like this. Code is committed to Git. A CI job runs terraform plan and attaches the plan to the pull request; on merge, it runs terraform apply against the remote backend. A second job runs the Ansible playbook against the dynamic inventory from Terraform state, with --check and --diff first in staging, then the real run in production. That pipeline is the answer most teams actually live with: not one or the other, but both, each doing the job it is good at, with a defined handoff between them.
FAQ
Should I learn Ansible or Terraform first?
Learn Terraform first if you are building cloud infrastructure, because it owns the provisioning layer most cloud roles hire for. Learn Ansible first if you are managing servers or network devices. Learn both if you can — the two together are the standard stack, and the job that asks for one usually expects the other as well.
Can Ansible replace Terraform?
Not for provisioning. Ansible can create a cloud resource through its cloud modules, but it keeps no state, so it cannot show you a plan, detect drift, or delete cleanly what it created. Use Ansible for configuration and Terraform for provisioning, and hand off between them.
Can Terraform replace Ansible?
Not for configuration. Terraform's provisioners can run commands on a machine, but they are a last resort, not a configuration management system, and HashiCorp's own guidance is to use a dedicated tool for configuration. Terraform is for things with a lifecycle; Ansible is for things with a desired state.
What are the current versions of Ansible and Terraform?
As of 30 November 2026, ansible-core 2.22.0 was released on 2 November 2026, with the Ansible 14.6.0 community package on ansible-core 2.21 and Ansible 15 in pre-release; Terraform 1.17.2 was released on 11 November 2026. Both projects ship frequently, so re-check the version before citing one.
How do Ansible and Terraform work together?
Terraform builds the infrastructure and emits outputs; Ansible reads those outputs and configures the machines. Red Hat's certified cloud.terraform collection provides an inventory plugin that builds an Ansible inventory from Terraform state, so the handoff needs no hand-maintained inventory file.
Conclusion
Ansible vs Terraform is not a contest, because the two tools answer different questions. Terraform provisions cloud infrastructure declaratively, with a state file, a reviewable plan and a provider ecosystem that covers every cloud platform. Ansible configures machines and network devices procedurally, over SSH, with no agent and no state, and it orchestrates multi-step processes that Terraform's graph model cannot express. The comparison that matters is not which tool is better but which job you are doing: lifecycle means Terraform, desired state means Ansible, and most real systems need both.
Learn Terraform first for cloud roles and Ansible first for systems roles, and learn both as soon as you can. Then set up the handoff properly: Terraform applies, Ansible reads the state as its inventory, and the playbook configures what Terraform built. Start with our Ansible Tutorial for Beginners and our Terraform Tutorial for Beginners for the hands-on foundations, and our Terraform vs Pulumi vs OpenTofu when you are ready to choose between the infrastructure-as-code tools themselves.
Sources checked 30 Nov 2026: Ansible documentation, Getting Started (inventory, SSH transport, module transfer to targets, ad-hoc ping, first playbook, playbook execution order, ansible-playbook command, PLAY RECAP); Ansible version and comparison guides checked 30 Nov 2026 (ansible-core 2.22.0 released 2 Nov 2026; Ansible 14.6.0 on ansible-core 2.21; Ansible 15 in pre-release; Terraform 1.17.2, 11 Nov 2026, per the Batch 8 GitHub release check); HashiCorp Developer, Terraform language documentation (declarative language, blocks, arguments, expressions, resource declaration as the main purpose); Red Hat cloud.terraform collection documentation via comparison guides (inventory plugin from Terraform state, terraform module, tf_output lookup, requires ansible-core 2.15 or later); Terraform Ansible provider (ansible/ansible) version 1.5.0 via comparison guide. NOT VERIFIED on an official page and therefore not stated as fact above: HCP Terraform per-resource pricing beyond the free tier (guides cite $0.10 per managed resource per month on Essentials, but the pricing page was not checked directly — re-check on HashiCorp's pricing page); Ansible Automation Platform pricing (Red Hat quotes per managed node, no public list price — no figure stated); the Terraform 1.17.2 release date beyond the Batch 8 check (re-check on GitHub at publication). External references: Ansible documentation — Getting Started · HashiCorp Developer — Terraform language documentation.



