Terraform vs. Ansible: What’s the Difference, and When Should You Use Both?
Terraform provisions infrastructure and Ansible configures it. Here’s where they overlap, when to use each, and how teams run both.
Terraform vs. Ansible? Short answer: probably both. Terraform provisions and manages infrastructure: servers, networks, and other cloud resources. Ansible configures what runs on them. The trouble starts when nobody is clear on which tool owns what. Their capabilities overlap, so the useful question isn't which tool wins. It's which layer each one should own.
Terraform vs. Ansible: What's the Difference?
In the HashiCorp Terraform Associate (004) course, CBT Nuggets trainer John McGovern uses an analogy that makes the split easy to explain to anyone on your team. Terraform is the builder. It decides how many houses go up, how big each one is, and what it sits on. Ansible is the interior designer. Once the houses exist, it decides what goes inside each one.
In his example, Terraform creates three VMs, including an Ubuntu machine with 4 CPUs and 100 GB of disk. Ansible then connects to that Ubuntu VM and installs Nginx.
Treat the analogy as a description of each tool's primary job, not a hard technical boundary. Ansible can create cloud resources, and Terraform can run commands on a machine after it boots.
What Terraform does
Terraform reads your configuration, compares it with its state file and the real resources, and builds a plan to close the gap. You run terraform init to set up the working directory and download providers, terraform plan to preview the changes, and terraform apply to make them.
Providers do the platform-specific work. There are providers for AWS, Azure, Google Cloud, Docker, GitHub, VMware, and many more, so the same workflow covers multi-cloud and hybrid environments. Terraform standardizes the workflow, not the platforms. An EC2 instance is still an AWS concept, and your engineers still need to understand the platform they're building on.
Terraform's state file maps configuration to managed resources, helping it plan updates and removals. When Terraform refreshes state during planning, it can also identify some out-of-band changes to managed resources, depending on the provider and the attributes it tracks. State isn't a complete inventory of everything in your environment, and drift detection isn't automatic monitoring.
What Ansible does
Ansible runs playbooks: ordered lists of tasks, written in YAML, against an inventory of hosts. It's agentless, typically connecting over SSH (or WinRM for Windows), running its modules, and reporting what changed.
Many Ansible modules check the current state before making a change. For example, a package task can leave an already-installed package alone on a second run. Not every task behaves that way: commands and shell scripts may need explicit checks to avoid repeating work. That's one reason playbook design still matters.
Where their capabilities overlap
Ansible has collections for AWS, Azure, and other platforms, with modules that create and manage instances, networks, storage, and databases. Teams already invested in Ansible sometimes provision with it successfully, especially for a modest number of resources. Because Ansible doesn't keep a state file, removing resources and catching out-of-band changes usually takes more deliberate playbook design.
Terraform can push configuration onto a machine through cloud-init, user_data, or provisioners such as remote-exec. HashiCorp’s documentation describes provisioners as a last resort, partly because Terraform can’t model what they do in a plan.
Neither tool is incapable of the other's work. Each one just has a natural home.
| Category | Terraform | Ansible |
|---|---|---|
| Primary focus | Infrastructure provisioning and lifecycle management | Configuration management and automation |
| Typical tasks | Creating and managing networks, VMs, load balancers, DNS, managed databases | Installing software, configuring systems, patching, automating operational tasks |
| Common approach | Declarative desired-state configuration | Task-based automation; many modules check desired state |
| Tracks changes with | A state file mapping managed resources | Checks target systems when tasks run |
| Previews changes with | terraform plan | Check/diff modes (partial preview; module-dependent) |
| Common use case | Provisioning cloud and hybrid infrastructure | Configuring and maintaining provisioned systems |
When Should Your IT Team Use Terraform, Ansible, or Both?
Terraform on its own fits environments where little happens inside the operating system after deployment: containers on managed Kubernetes, serverless functions, managed databases, and workloads built from prebuilt machine images.
Ansible on its own fits teams managing existing fleets: on-prem servers that aren't being rebuilt, network device configuration, patching, and day-two operations.
Both fit the common middle ground: cloud or hybrid VMs that need consistent OS and application configuration. Terraform owns the infrastructure lifecycle, and Ansible owns what runs inside the machines.
Example: deploying a new tier of application servers
Say your team needs three application servers behind a load balancer in a cloud environment. The technical work matters, but so does deciding who reviews provisioning changes, who owns configuration changes, and how the team troubleshoots failures when a deployment crosses both layers.
- Terraform provisions the network, security groups, load balancer, and three VMs, tagging each one with its role.
- Ansible builds its inventory, either from Terraform’s outputs or with a dynamic inventory plugin, such as aws_ec2, that queries the cloud provider for instances with that tag.
- An Ansible playbook configures the operating systems, installs the application runtime and Nginx, applies application settings, and sets up monitoring agents.
- Both sets of code live in version control and run from the same pipeline, so one merge request shows the infrastructure change and the configuration change together.
When the tier needs a fourth server, you change the desired capacity in Terraform and rerun the provisioning and configuration steps. Ansible's state-checking modules can leave the existing servers' configuration unchanged while bringing the new one in line. With responsibilities clearly separated, engineers can see whether a problem came from a missing cloud resource or an application setting, and hand off changes without relying on one person's memory.
Why Infrastructure as Code Matters
Most teams don't start with IaC. They start in a cloud console, then move to scripts, and both approaches have limits.
Console changes aren't invisible. AWS CloudTrail and similar services log who did what. But an activity log tells you what happened, not how to rebuild it. A setup assembled by hand isn't captured anywhere as a reusable definition, so recreating it in another region means clicking through it again and hoping nothing gets missed. Over time, the knowledge of how things are configured ends up in a few engineers' heads.
Scripts are a real improvement. They live in a text file, they go into Git, and you can rerun them. A script describes steps, though, not an end state, and that difference shows up quickly.
The Docker example: scripted vs. declarative
In the skill, John provisions a Docker container with a short bash script. The script runs docker run to create a container named “web,” with set -e so it stops on any error. The first run works. On the second run, Docker refuses because a container named “web” already exists, and the script exits.
You can fix that by adding checks for whether the container exists, whether it's running, and whether its settings match. Scripts can absolutely be written to be idempotent. Every check adds code you have to maintain, though, and you end up building your own state handling in bash.
He then declares the same container in a Terraform configuration. The first apply creates it. The second apply reports no changes, because Terraform checked the real container through the Docker provider, compared it with the configuration, and found they already matched.
That's idempotency: applying the same configuration once or ten times converges on the same end state. John compares it to the lock button on a car key fob. Press it once and the car locks. Press it again and the car stays locked.
Next, he renames the container in the configuration. That change can't be applied to the existing container, so Terraform's plan shows a replacement: destroy the old container, create a new one. That's specific to this attribute, not universal Terraform behavior. Terraform updates resources in place when the provider and platform API allow it, and replaces them when they don't. The plan tells you which will happen, which is one reason to read it before applying.
Idempotency doesn't make every apply risk-free. When a managed resource changes outside Terraform, a refreshed plan may propose bringing it back into line with the configuration. Replacement can cause downtime or data loss for stateful resources. Terraform shows the proposed infrastructure changes, but your team still needs to review the plan and understand the consequences.
What happens when you run the same Terraform configuration twice? In the Infrastructure as Code with Terraform skill, John McGovern runs a script that fails on its second run next to a Terraform apply that finds nothing to change, then renames the container to show a replacement.
Watch the Infrastructure as Code skill →
What Terraform Changes for IT Teams
John also makes the point that leadership rarely cares about declarative vs. imperative. The practical outcomes are what matter.
Repeatable deployments. A reviewed configuration can stand up the same environment again, in another region or another account, without someone reconstructing it from memory.
Consistency across environments. Staging and production built from the same code drift apart less, which removes a common source of surprises during releases.
Changes you can review and track. Infrastructure changes go through the same merge requests as application code. The git log command shows who changed what and when, and terraform plan output shows what a change will do before anyone approves it. Version control lets you revert a definition, but reverting code doesn’t automatically undo every infrastructure change or restore deleted data.
Easier collaboration. Teams can store state in a remote backend rather than on one engineer's laptop. Some backends support state locking, which helps prevent concurrent state-writing operations; not every backend provides it. Remote state is available with Terraform Community Edition, not only HCP Terraform (HashiCorp's managed platform).
Knowledge that doesn't live in one person. John also notes that once a senior engineer has written the configuration, routine changes can go to someone more junior. The configuration documents how the environment is built, which helps with onboarding.
Less repetitive manual work. Engineers spend less time clicking through consoles and more time on the parts of the job that need judgment.
None of this is automatic. IaC still needs code review, sensible state management, and a team that understands what a plan is telling them.
See Infrastructure as Code in Action
This article explains the concepts. The skill shows them running.
In the 55-minute Infrastructure as Code with Terraform skill, John walks through console, scripted, and declarative provisioning side by side. He demonstrates what changes between the first and second apply, shows a container replacement, and explains how provisioning differs from configuration management. It's the first skill of the HashiCorp Terraform Associate (004) course.
Watching the second terraform apply come back with nothing to change, and the plan for a container replacement, gives your team a concrete reference for how Terraform would fit alongside Ansible.