Terraform vs Ansible
One builds the infrastructure, the other configures what runs on it. They overlap in the middle, and most teams that ask which to pick end up using both.
The short answer
If the job is creating and changing cloud resources (networks, clusters, databases, DNS, IAM) and keeping them matched to code you review, Terraform, or its open-source fork OpenTofu, is built for it: it works out the dependency order, shows a plan, and can destroy what it created.
If the job is configuring machines that already exist (installing packages, writing config files, rotating certificates, patching, running an ordered rollout across a fleet), Ansible is built for it: it connects over SSH without an agent and runs tasks in the order you write them.
They are not really rivals. A common split is Terraform for the infrastructure and Ansible for what runs on servers that can't be rebuilt from an image. If you run only containers and managed services, you may not need Ansible at all.
Terraform and Ansible side by side
| Feature | Terraform | Ansible |
|---|---|---|
| Made by | HashiCorp. You run the CLI yourself, or use HCP Terraform as a managed service. | Red Hat, with the community. ansible-core is the engine; Red Hat Ansible Automation Platform is the supported product, and AWX its open-source upstream. |
| Built for | Provisioning: creating, changing and destroying infrastructure through providers' APIs. | Configuration management and orchestration: setting up and changing what runs on hosts and devices. |
| Style | Declarative. You describe the end state; Terraform builds a dependency graph and decides the order. | Procedural playbooks of tasks, run top to bottom. Each module aims to be idempotent, so a rerun changes only what differs. |
| Written in | HCL (HashiCorp Configuration Language), or JSON. | YAML playbooks, with Jinja2 templating. |
| How it reaches things | Providers call each service's API (AWS, Azure, Google Cloud, Kubernetes, SaaS). | Agentless: SSH to Linux hosts (most modules need Python on the host), WinRM for Windows, and network_cli or APIs for network devices and clouds. |
| State | A state file in a backend you configure, such as S3 or HCP Terraform, compared with your code on every plan. | No state file. Each run checks the hosts as they are now; what exists is whatever the inventory and the hosts report. |
| Preview a change | terraform plan, for every resource. | --check (check mode) with --diff, for modules that support check mode. |
| Remove what it made | Delete it from the code and apply, or terraform destroy. | Write tasks that remove it (state: absent). Deleting a task leaves what it made in place. |
| Extensions | Providers and modules from the Terraform Registry. | Collections and roles from Ansible Galaxy, such as amazon.aws for AWS. |
| Secrets | Can end up in the state file in plain text, so the backend needs encryption and tight access. | Ansible Vault encrypts variables and files kept in the repo. |
| Licence | Business Source License 1.1 since version 1.6 (2023). OpenTofu is the open-source (MPL 2.0) fork. | ansible-core is open source under GPL v3. |
| Cost | The CLI costs nothing to run. HCP Terraform is priced on HashiCorp's pricing page. | ansible-core and AWX cost nothing to run. Ansible Automation Platform is a Red Hat subscription; ask Red Hat for its price. |
Provisioning and configuration are different jobs
Provisioning means asking a cloud for a VPC, a load balancer, a managed database or a Kubernetes cluster, and wiring them together. The hard parts are ordering (the subnet before the instance), knowing what already exists, and changing it without recreating it by accident. Terraform's graph, plan and state exist for exactly that.
Configuration means what happens on a machine once it exists: users, packages, services, kernel settings, application config, certificates. The hard parts are reaching many hosts, doing steps in a set order, and rolling changes out in batches so a fleet keeps serving. Ansible's inventory, ordered tasks, handlers and rolling batches (serial) exist for that.
Each tool can reach into the other's job. Ansible's cloud collections can create instances, buckets and security groups, and Terraform's provisioners (local-exec, remote-exec) can run commands on a new machine. HashiCorp's documentation calls provisioners a last resort, and Ansible's cloud modules leave you to track what you created and to write its removal yourself.
State, previews and removal
Terraform remembers what it created. That is why it can show an exact plan, tell you when someone changed a resource by hand, and delete a resource when you remove it from the code. The price is the state file: you store it, lock it so two applies can't run at once, and protect it, because it can hold secrets.
Ansible keeps no record between runs. A playbook describes steps, and each module checks the host before it acts, so a second run reports nothing changed when nothing needs to. There is no state to lose or lock, but also no memory: take a task out of a playbook and whatever it made stays on the host. Removing something is a task you write, with state: absent.
Ansible's preview is check mode. Run a playbook with --check --diff and supporting modules report what they would change, but modules that don't support check mode are skipped, and tasks that depend on an earlier task's real result can't predict accurately. Treat it as a strong hint, not a plan you can approve line by line the way you can with terraform plan.
Drift
In Terraform, drift is a difference between state and the real resource, and every plan shows it; a scheduled plan in CI turns it into an alert. Our guide to drift detection and remediation in Terraform-managed infrastructure covers how to run that loop.
Ansible handles drift by running the playbook again. A scheduled run in check mode reports hosts that no longer match; a normal run puts them back. It only covers what the playbook mentions: a package someone installed by hand, which no task names, is invisible to it.
Using them together
The usual hand-off is Terraform first, Ansible second. Terraform creates the instances and outputs their addresses or tags; Ansible finds them through a dynamic inventory plugin (amazon.aws.aws_ec2 on AWS reads instances straight from the EC2 API) and configures them. Matching on tags Terraform sets keeps the two decoupled.
For tighter wiring, the cloud.terraform collection lets a playbook run Terraform and read its state as an inventory, and the Ansible provider for Terraform (ansible/ansible in the Terraform Registry) declares inventory hosts and groups from Terraform code. Check each project's documentation for the versions it supports before you depend on it.
Teams that bake machine images change the split again. If Packer builds an image with everything installed (often using Ansible as its provisioner) and Terraform launches instances from it, Ansible runs at build time rather than against live servers, and replacing an instance replaces its configuration.
When you may not need Ansible
If everything you run is containers on Kubernetes or a managed container service, plus managed databases and queues, there may be no long-lived server for Ansible to configure. Terraform provisions the platform, and a GitOps tool such as Argo CD or Flux deploys what runs on it; see our piece on reconciliation drift with Argo CD and ApplicationSets. Ansible stays useful there for what is left: bastion hosts, self-managed databases, network devices and on-premises machines.
Licensing
Terraform releases from 1.6 (August 2023) are under the Business Source License 1.1, which restricts products that compete with HashiCorp's but not using Terraform to manage your own infrastructure. OpenTofu, under the Linux Foundation, is the MPL 2.0 fork. ansible-core is GPL v3; Red Hat's subscription buys support and the Automation Platform around it, not the right to run Ansible.
Which one to choose
Choose Terraform if
- You are creating and changing cloud resources: networks, clusters, managed databases, DNS, IAM.
- You want a reviewed plan of every change before it is applied.
- You want resources removed when you remove them from the code.
- You manage several clouds or SaaS APIs and want one workflow for all of them.
Choose Ansible if
- You configure long-lived servers: packages, files, services, users, patches.
- You need steps in a set order across many hosts, such as a rolling upgrade in batches.
- You manage network devices or on-premises machines that have no provisioning API.
- You want no agent on the hosts and no state file to store.
- You build machine images and want a readable way to say what goes in them.
Questions people ask
Can Ansible replace Terraform?
For small setups, yes: Ansible's cloud collections can create most common resources. You give up a full plan before changes, automatic dependency ordering and removal by deleting code, and you have to look up what exists on every run. Most teams managing more than a handful of cloud resources keep Terraform for provisioning.
Can Terraform replace Ansible?
Partly. If servers are built from images, or configured once at boot through user data or cloud-init, there may be nothing left for Ansible to do. Terraform's provisioners can run commands on a new machine, but HashiCorp describes them as a last resort, and they run only when the resource is created.
Should I use Terraform and Ansible together?
Often. Let Terraform create the infrastructure and tag it, then let Ansible find hosts through a dynamic inventory and configure them. Don't manage the same setting from both: each will undo the other's change.
Is Ansible declarative or procedural?
Both, depending on where you look. Each task declares the state it wants (a package present, a service started), but the playbook runs its tasks in the order written. Terraform is declarative throughout: the order comes from the dependency graph, not from the file.
Does Ansible have a state file like Terraform?
No. Ansible checks the current state of each host when it runs and keeps nothing between runs. That avoids storing and locking state, but it also means Ansible can't notice that something it once created is no longer in the playbook.
Are Terraform and Ansible free?
The Terraform CLI and ansible-core cost nothing to run. HCP Terraform and Red Hat Ansible Automation Platform are paid products; check HashiCorp's pricing page and ask Red Hat for current prices.
Sources
- Terraform: State
- Terraform: Provisioners
- Terraform: Sensitive data in state
- Ansible: Getting started
- Ansible: Validating tasks with check mode and diff mode
- Ansible: Controlling playbook execution (serial and strategies)
- Ansible: Protecting sensitive data with Ansible Vault
- amazon.aws.aws_ec2 inventory plugin
- cloud.terraform collection
- Ansible provider for Terraform
- Red Hat Ansible Automation Platform
- OpenTofu

