Ansible vs Terraform: Infrastructure Tool Comparison
Every team manages its infrastructure somehow. Some teams have their provisioning steps written down in a runbook. Others run a shell script that configures each server, with the patching schedule living in one engineer’s memory. But what happens when the script meets a host it was never tested against, or the engineer leaves?
This is what Ansible and Terraform are for. Terraform builds the infrastructure, while Ansible configures what runs on it. The two get framed as rivals, but they complement each other.
#What Is the Difference Between Ansible and Terraform?
Each tool works at a different point in a server’s life. Terraform goes first, creating and managing infrastructure through a platform’s API. There is no operating system to log into yet, only an API to request resources from. Ansible comes after, once a server is running and reachable over SSH. It installs packages, edits config files, and starts services.
#What Is Ansible?
Ansible is an open-source automation tool maintained by Red Hat. It works on running servers, configuring the system, and deploying applications on it. It also orchestrates multi-step workflows across several machines, such as a zero-downtime rolling update. Ansible is written in Python and released under the GPLv3 license.
Ansible is agentless. It runs no permanent service of its own on the machines it manages, connecting to each over SSH instead.
For each task, it transfers the code to a temporary location on the target, runs it, and cleans up when the task finishes. Each task names a desired state, and the module changes only what differs from it, so running the same playbook twice is safe.
Ansible Core, the free command-line tool, covers this for one operator working from a terminal or a CI pipeline. It has no web interface, no shared access control, and no support contract, which a team automating across many people and hosts eventually needs. Red Hat’s Ansible Automation Platform adds those: a web console, role-based access control, event-driven automation, and vendor support.
Ansible fits the configuration half of the lifecycle, keeping running systems in a known state as software and requirements change around them.
Also Read: Ansible Cheat Sheet
#What Is Terraform?
Terraform is a provisioning tool from HashiCorp that manages infrastructure as code. It defines servers, networks, and storage in configuration files that can be versioned, reused, and shared like any other code. Terraform then creates and manages the infrastructure they describe.
Those files are written in HashiCorp Configuration Language (HCL), where the end state is declared rather than the steps to reach it.
A provider connects Terraform to each platform, translating that declared state into the platform’s own API calls. Terraform records what it created in a state file, then compares that record to the real infrastructure on each run.
HashiCorp released Terraform under the open-source MPL 2.0 license, then moved it to the source-available Business Source License in August 2023. The BSL restricts only competitive commercial use, so an ordinary team running Terraform is unaffected.
If you need a fully open-source tool, you can use OpenTofu, a fork of the last MPL-licensed release. It is now a Linux Foundation project and runs the same configurations as a drop-in alternative.
The command-line tool is free to run. HCP Terraform and Terraform Enterprise add shared state, access controls, and policy enforcement for teams that need them.
Terraform fits the provisioning half of the lifecycle, building and changing the infrastructure that applications run on before any software is configured on it.
Also Read: Terraform Cheat Sheet
#Ansible vs Terraform: Key Differences
The table below compares Ansible and Terraform, from their roles through how each applies changes and what it retains between runs, to licensing and ownership.
| Dimension | Ansible | Terraform |
|---|---|---|
| Primary role | Configuration management | Provisioning |
| Approach | Procedural tasks, desired-state modules | Declarative (HCL) |
| Infrastructure model | Leans mutable (in-place) | Leans immutable (replace) |
| State | No state file, checks live | State file tracks resources |
| Connection | Agentless, over SSH | Agentless, via provider APIs |
| Ecosystem | Modules, roles, collections | Providers, modules |
| License | GPLv3, open source | BSL, source-available |
| Backing | Red Hat (IBM) | HashiCorp (IBM) |
Terraform records the resources it creates and often replaces them to apply a change, while Ansible updates running servers and retains nothing between runs. Terraform’s record allows the infrastructure to be checked against its configuration at any time, and Ansible’s live updates avoid downtime. The sections below discuss the key differences in detail.
Run your deployments in a scalable and cost-effective open cloud infrastructure. Cherry Servers' secure virtual private servers offer automatic scaling, flexible pricing, and 24/7 technical support. Our pre-defined Ansible module helps you automate the installation and configuration.
#Provisioning vs Configuration Management
Terraform calls a platform’s API to create each resource, then manages it over its lifetime. Ansible installs packages, edits files, and manages services on a running server. In a build, that line falls between the resource and its operating system. Terraform is responsible for the server’s existence, and Ansible for everything configured in it.
Each tool can take on part of the other’s job. Ansible can create infrastructure through cloud modules, but it keeps no record of those resources, so it has no lifecycle model for them.
Terraform can run configuration through provisioners, which execute scripts on a resource after it is created. But provisioners run outside the plan Terraform builds before making changes, so they break its declarative model. HashiCorp recommends using provisioners only as a last resort, and points to configuration management tools like Ansible instead. The overlap has limits in both directions, which is why the two are usually paired rather than substituted.
Also Read: How to Provision Bare Metal Servers with Ansible
#Declarative vs Procedural
Terraform is declarative. It builds a dependency graph from the references between resources, then uses that graph to determine the order of operations. Resources with no dependency between them run at the same time.
Ansible is declarative within a task and procedural across tasks. Within a task, a module takes the declared state and works out the steps to reach it. Across tasks, Ansible follows the order written, running each task on every host before starting the next.
#Mutable vs Immutable
A provider’s API determines how Terraform applies a change. Terraform edits a resource in place if the API allows that change on a running resource. Otherwise, it destroys the resource and creates a new one.
Many properties cannot be changed while a resource runs, so Terraform replaces it. Each rebuild starts from the same definition, which keeps configuration from accumulating over time. In a build, that means a rebuilt server is identical to the definition every time, with no record of manual edits made along the way. HashiCorp designed Terraform primarily around this immutable pattern.
Ansible modifies a running server’s configuration directly, leaving the server itself intact. That is the practical option for a stateful server like a database, since a rebuild would destroy the data it holds.
Neither approach is absolute. Ansible can configure the image Packer builds for an immutable deployment, and Terraform’s lifecycle rules can force or prevent a replacement. Teams set the balance through deployment practice as much as through the tool.
#State and Drift
Terraform maintains a state file, a persistent record of the resources it manages. It can compare that record to the live infrastructure at any time and detect drift. Terraform also reports drift in every plan, before applying any changes. In practice, that record is what lets Terraform show what changed outside the configuration before touching anything.
Ansible keeps no record between runs, so it finds drift only while a playbook executes. By default, it applies the declared state to everything the playbook covers, correcting drift as it goes without reporting what the drift was.
For a dry run, Ansible offers check mode, which reports the changes a run would make without making them. The practical split is that Terraform tracks state so it can tell what drifted, while Ansible reconverges to the playbook each run.
#Licensing, Ownership, and Cost
Ansible is open source, under the GPLv3. Terraform is licensed under the Business Source License, which restricts using the software to compete commercially. OpenTofu, its open-source fork, removes that restriction entirely.
IBM now owns both tools. It acquired Red Hat, Ansible’s maintainer, in 2019, and completed its purchase of HashiCorp in February 2025.
Both tools are free to download and run. Each has a paid tier that adds collaboration, governance, and support for larger teams.
Rent Dedicated Servers
Deploy custom or pre-built dedicated bare metal. Get full root access, AMD EPYC and Ryzen CPUs, and 24/7 technical support from humans, not bots.
#When to Choose Ansible vs Terraform
You most likely run Terraform or Ansible already. However, some workloads only require Terraform while others require Ansible, or even both. The following sections will help you decide when to use each tool or both.
#When to Choose Terraform
Terraform creates and manages your infrastructure through a platform’s API. Servers, networks, storage, load balancers, and DNS records are all defined in configuration files, and it builds what those files describe.
Terraform is the better fit whenever the change is to the infrastructure itself. Adding a load balancer, resizing a database, or building a second environment in a new region all qualify.
Its declarative model and state tracking show their value in these recurring situations:
- Multiple platforms at once: You can describe resources across several clouds and services in one configuration. Terraform applies every change in a single run, resolving the dependencies between them. You manage everything from one workflow instead of a separate tool for each provider.
- Identical environments: Development, staging, and production build from the same files and differ only in their variables.
- Short-lived environments: Create an environment to test an upgrade before it reaches production, then destroy it from the same configuration when the work is done. Terraform’s state records exactly what it built, so destroying it removes every resource.
Terraform can also work alone. When the software is baked into a machine image ahead of time, deploying that image is only a provisioning step. Nothing has to be configured after the server boots. That configuration was done earlier, when the image was built.
#When to Choose Ansible
Ansible manages the software on servers you already run. It installs and configures software such as a web server, adds a firewall rule to every host, and creates the user accounts an application needs.
Ansible is the tool for the following:
- Ongoing maintenance: Patching, security updates, and certificate rotation recur for as long as a server is in service. Because those tasks are idempotent, you can run the same playbook repeatedly, and Ansible keeps the whole fleet on one known configuration without rebuilding anything.
- Ordered rollouts: A rolling update drains a node, upgrades it, and returns it to service before moving to the next. Ansible runs those steps in sequence, and each can be made conditional on the result of the one before it.
- Existing or mixed fleets: Ansible converges production servers onto one configuration from a single control point. It reaches every host over SSH with no agent to install first, so a hand-built box and a cloud instance take the same playbook.
#When to Use Both
Together, Ansible and Terraform remove the handoff between provisioning and configuration. Ansible reads Terraform’s state to build its inventory, so the servers Terraform creates become the hosts a playbook targets. Red Hat maintains a certified collection for that, and a Terraform provider that triggers Ansible Automation Platform jobs once a run finishes.
Ansible no longer needs a cloud module for each platform, because Terraform's providers already do that. In exchange, Terraform's state becomes an interface between them, and how resources are named and grouped there decides what Ansible sees.
When Terraform replaces a server, the new one has none of the configuration Ansible applied to the old one. The playbook has to run again. Some teams have Ansible configure a machine image instead, the template every server launches from. Terraform deploys that image, and each replacement boots with the configuration already on it.
#Managing Bare Metal with Ansible and Terraform
Racking, cabling, and powering a server are jobs no configuration file can do. That physical step is why bare metal is left out of infrastructure as code. Servers arrive through tickets rather than code, and the record of what exists is whatever someone wrote down. Each one ends up configured the way whoever built it preferred.
A bare metal provider has already racked and cabled the hardware, so servers are requested from an existing pool through an API. Terraform reaches that API through a provider, so a physical server is declared, created, and recorded in state exactly as a cloud instance would be. Ansible then configures it over SSH, the same connection it uses for any other host.
Cherry Servers maintains integrations for both tools:
- A Terraform provider on the Terraform Registry, covering servers, projects, SSH keys, and IP addresses. It works with OpenTofu as well.
- An official Ansible collection with modules for the same resources, for teams that provision from Ansible rather than Terraform.
- A REST API, a command-line client, and Python and Go SDKs for automation beyond either tool.
You declare a cherryservers_server resource with its project, region, and plan, then run terraform apply. Terraform calls the API, waits for the hardware, and records the result in state, the server’s address included. Ansible reads that same state, so playbooks run against physical servers unchanged.
The fleet is then described in the same files, reviewed the same way, and reproduced from the same definitions as the rest of the infrastructure. One difference remains. Destroying and recreating a cloud instance takes seconds, while a physical server is a heavier commitment. Changes are applied to it as it runs.
#Conclusion
Terraform and Ansible work on the same infrastructure at different stages. Terraform creates servers, networks, and storage through a platform’s API and records each in state. Ansible installs packages, edits configuration files, and starts services on already-running servers. Using both tools removes the manual steps between creating a server and configuring it.
Running Terraform and Ansible on bare metal servers requires a host that publishes a Terraform provider and an Ansible collection. Cherry Servers maintains both for its dedicated servers.
FAQs
Should I learn Terraform or Ansible first?
Start with Ansible if you are already maintaining running servers. You can automate patching, package installation, and configuration changes from day one. If you are building infrastructure from scratch, start with Terraform, which creates and tracks the resources you would otherwise handle manually.
Can Terraform replace Ansible?
Neither tool fully replaces the other. Terraform can push commands to a server through provisioners, though that path is a documented last resort. Ansible can create cloud resources through its modules, but it doesn’t track them, so later updates and teardowns are left to you.
Are Terraform and Ansible free?
Both tools have a free version. Ansible Core, the Terraform CLI, and OpenTofu all cost nothing to install or use.
The paid options are Ansible Automation Platform, HCP Terraform, and Terraform Enterprise. Teams pay for them to get a web interface, access controls, policy enforcement, and vendor support.
Is Terraform still open source?
Terraform is source-available. HashiCorp moved it off the open-source MPL 2.0 license and onto the Business Source License in August 2023.
That license restricts using Terraform to build a competing commercial product, so ordinary commercial use carries no additional conditions. OpenTofu, which split from the final MPL version, remains open source under the Linux Foundation. It reads the same HCL, uses the same providers, and keeps the same state format.
Get 100% dedicated resources for high-performance workloads.