Terraform vs Ansible: Which Should You Use?
Terraform builds infrastructure and Ansible configures it. Here is how they differ on state, language and approach, and how teams use them together.
Use Terraform to provision infrastructure such as networks, servers, databases and cloud services, because it tracks what exists in a state file and plans changes before applying them. Use Ansible to configure servers and deploy software on them over SSH, without agents. Most teams get the best results using Terraform first, then Ansible.
Photo: Unsplash
On this page
If you are moving from clicking around a cloud console to managing infrastructure as code, you will hit this question fast: Terraform or Ansible? Both are popular, both use text files you keep in Git, and both can create a server in the cloud. So which one should you learn, and which should your team standardize on?
The honest answer is that they were built for different jobs. Terraform is a provisioning tool. Ansible is a configuration management and automation tool. Once you see that split, the choice gets much easier, and you will probably find you want both.
New to the concept? Our plain-English guide to infrastructure as code covers the basics first.
Provisioning vs configuration: the core difference
Think of building a restaurant.
- Provisioning is the construction: the building, the plumbing, the electrical supply. In cloud terms, that means networks, virtual machines, load balancers, databases, DNS records and access policies.
- Configuration is fitting out the kitchen: installing equipment, setting up the menu, training staff. In server terms, that means installing packages, editing config files, creating users, deploying application code and restarting services.
Terraform is designed for provisioning. You describe the infrastructure you want, and it creates, changes or destroys cloud resources to match.
Ansible is designed for configuration and task automation. You describe what should be true on your machines, and it connects to them and makes it so.
There is overlap. Ansible has modules that can create cloud resources, and Terraform can run basic setup scripts on new machines. But each tool is strongest in its own lane.
Key takeaway: Terraform builds the infrastructure; Ansible configures what runs on it. Asking “Terraform or Ansible?” is often like asking “builder or interior designer?” Most serious projects need both.
Side-by-side comparison
| Factor | Terraform | Ansible |
|---|---|---|
| Primary job | Provisioning infrastructure | Configuring systems and automating tasks |
| Language | HashiCorp Configuration Language (HCL) | YAML playbooks |
| Style | Declarative: describe the end state | Mostly declarative modules run in the order you write them |
| Tracks what exists | Yes, in a state file | No persistent state; checks systems on each run |
| Preview changes | terraform plan shows exactly what will change |
Check mode (--check) gives a dry run, with module-dependent accuracy |
| How it connects | Calls cloud and service APIs through providers | Connects to machines over SSH (or WinRM for Windows), no agent needed |
| Best at | Cloud resources, networking, managed services, multi-cloud | OS setup, packages, app deployment, patching, ad hoc tasks |
| Owner | HashiCorp, now part of IBM | Red Hat, part of IBM |
| License | Business Source License 1.1 since August 2023 | Open source (ansible-core is GPL licensed) |
How Terraform works
You write configuration files in HCL describing resources, such as “a virtual network with these subnets” or “a database of this size.” Terraform uses providers, plugins that talk to each platform’s API, to create them.
The key concept is state. HashiCorp’s documentation describes state as a record that maps real-world infrastructure to the resources in your configuration. By default it is a local file called terraform.tfstate. When you run terraform plan, Terraform compares your configuration with the state and the real infrastructure, then shows exactly what it will add, change or destroy. terraform apply makes those changes.
This plan step is Terraform’s superpower. Reviewers can see “this will delete a database” before it happens.
State also brings responsibilities:
- Store it remotely in a backend such as cloud object storage or HCP Terraform, so the team shares one source of truth and locking prevents simultaneous changes.
- Protect it. HashiCorp warns that state can contain secrets, so it should never be committed to version control.
- Avoid changes outside Terraform. Manual edits in the cloud console cause drift, which the next plan will try to undo.
The license question and OpenTofu
On August 10, 2023, HashiCorp announced it was moving its products from the Mozilla Public License 2.0 to the Business Source License 1.1 for future releases. The license still allows most end users to use Terraform, but restricts building competing commercial offerings.
In response, a group of companies forked the last open source version as OpenTofu, which now runs under the Linux Foundation and describes itself as a drop-in replacement for Terraform. IBM completed its acquisition of HashiCorp on February 27, 2025. For most teams using Terraform internally, the license change has little practical effect, but if open source licensing matters to your organization, OpenTofu is worth evaluating. This is not legal advice; check the license terms with your legal team if you build products on top of these tools.
How Ansible works
Ansible’s documentation highlights its agentless architecture: it uses SSH with existing operating system credentials to reach remote machines, so there is nothing extra to install on the servers you manage.
You write playbooks in YAML. Each playbook lists tasks, such as “install nginx,” “copy this config file,” or “make sure this service is running,” and Ansible runs them in order against hosts listed in an inventory. Most modules are idempotent: if a server already matches the desired state, Ansible makes no change, even if you run the playbook again.
Ansible is also good for one-off operational work: rolling out a security patch to 200 servers, rotating a certificate, or restarting a service across a cluster in a controlled order. Red Hat also sells Ansible Automation Platform, a commercial product that adds a web interface, access control and scheduling on top of the open source engine.
When to use each
| Task | Better fit | Why |
|---|---|---|
| Create a cloud network, subnets and firewall rules | Terraform | Tracks resources and dependencies; plans changes |
| Spin up managed databases, queues and storage buckets | Terraform | API-driven resources with a clear lifecycle |
| Manage resources across AWS, Azure and Google Cloud | Terraform | One workflow with providers for each platform |
| Install and configure software on Linux or Windows servers | Ansible | Connects to the OS and manages packages, files and services |
| Patch and update an existing server fleet | Ansible | Runs ordered tasks across many hosts |
| Configure network devices such as switches and routers | Ansible | Large library of network modules |
| Deploy application code to virtual machines | Ansible | Ordered steps such as stop, deploy, migrate, start |
| Tear down a whole test environment cleanly | Terraform | terraform destroy removes everything it created |
If your applications run in containers on a managed platform like Kubernetes, you may need less Ansible, because container images replace much of the server configuration work. Our comparison of Docker vs Kubernetes explains that model.
How teams use Terraform and Ansible together
The most common pattern is a hand-off:
- Terraform provisions the network, virtual machines, load balancer and database.
- Terraform outputs details such as IP addresses or hostnames.
- Ansible consumes those outputs, often through a dynamic inventory that queries the cloud directly, and configures each server.
- A CI/CD pipeline runs both, typically Terraform plan for review, Terraform apply after approval, then the Ansible playbook.
Git commit
|
[CI/CD pipeline]
|-- terraform plan -> human review
|-- terraform apply -> creates VMs, network, database
|-- ansible-playbook -> installs packages, config, app
v
Running, configured environment
Keep a clear boundary. Let Terraform own anything with an API-driven lifecycle, and let Ansible own what happens inside the machines. When both tools try to manage the same thing, they fight, and drift follows.
If you are choosing a pipeline tool to run this workflow, see our comparison of GitHub Actions vs GitLab CI vs Jenkins.
Where to start
- List what you manage today. Separate cloud resources (provisioning) from server setup and app deployment (configuration).
- Start with the bigger pain. If environments are hand-built in the console, start with Terraform. If servers drift and patching is manual, start with Ansible.
- Pick one small, real target. For Terraform, a single network and VM in a test account. For Ansible, a playbook that sets up one web server.
- Put everything in Git and require review for changes.
- Set up remote state with locking before a second person touches Terraform.
- Automate in CI/CD once the manual runs are reliable. Our guide to building a CI/CD pipeline shows the basic structure.
- Secure secrets. Keep credentials in a secrets manager or Ansible Vault, not in plain-text files. Our cloud security checklist covers related controls.
Conclusion
Terraform and Ansible are not true rivals. Terraform is the better tool for creating and tracking infrastructure, with a plan you can review before anything changes. Ansible is the better tool for configuring machines and running operational tasks, with no agents to install. Learn the one that matches your biggest problem today, and expect to add the other. Both skills show up in DevOps engineer job descriptions for a reason.
Sources
- HashiCorp: Terraform state documentation — what state is, default local storage and the warning about secrets
- HashiCorp: HashiCorp adopts Business Source License — August 10, 2023 move from MPL 2.0 to BSL 1.1
- IBM: IBM completes acquisition of HashiCorp — February 27, 2025 completion
- Ansible documentation: Introduction — agentless architecture, SSH, YAML playbooks and idempotency
- OpenTofu — Terraform fork under the Linux Foundation
Frequently asked questions
Can Ansible replace Terraform?
Ansible can create cloud resources with its cloud modules, so it can replace Terraform for simple setups. But it does not keep a state file or produce a full plan of changes, which makes tracking, updating and cleanly destroying large infrastructure harder. Most teams use Terraform for provisioning and Ansible for configuration rather than forcing one tool to do both.
Is Terraform harder to learn than Ansible?
Most beginners find Ansible quicker to start, because playbooks are readable YAML and you can automate a single server in minutes. Terraform requires learning HCL, providers and state management. Neither is especially hard, and both have extensive official documentation. Your background matters: system administrators often find Ansible natural, while cloud engineers often prefer Terraform.
Is Terraform still free to use?
Yes, for most users. Since August 2023, Terraform's source has been under the Business Source License 1.1, which allows free use, including commercial internal use, but restricts offering a competing product. HashiCorp also sells paid HCP Terraform and enterprise plans. OpenTofu is a free, open source fork if you need a traditional open source license.
Does Ansible need an agent on each server?
No. Ansible is agentless. It connects to Linux and Unix machines over SSH using existing credentials, and to Windows machines over WinRM or SSH. You only install Ansible on the control machine that runs the playbooks. This keeps maintenance low, but the control machine needs network access and credentials for every host it manages.
Should I learn Terraform or Ansible first?
Learn the one that matches your work. If you mainly build cloud environments, start with Terraform. If you manage existing servers, patching or app deployments, start with Ansible. For DevOps and cloud engineering roles, plan to learn both, since many job postings list them together and teams commonly use them side by side.
What is the difference between Terraform and OpenTofu?
OpenTofu is a community fork of Terraform created after HashiCorp changed Terraform's license in August 2023. It runs under the Linux Foundation and describes itself as a drop-in replacement, so most existing configurations work with it. The main differences are licensing, governance and some features that each project has added independently since the fork.
Can Terraform configure servers?
Only to a limited degree. Terraform can pass startup scripts such as cloud-init to new virtual machines, and it has provisioners for running commands, but HashiCorp's documentation advises exhausting all alternatives before using provisioners. For ongoing server configuration, patching and app deployment, a configuration tool such as Ansible, or baked machine and container images, is a better fit.
Was this guide useful
No reviews yet. Share your experience and help other readers.