What is the difference between Terraform and Ansible?
Terraform is an infrastructure as code tool that provisions infrastructure, such as networks, virtual machines, managed databases and DNS records, by calling provider APIs, and it records what it created in a state file so it can plan changes and deletions. Ansible is an automation tool that configures machines that already exist: it connects over SSH without an agent and runs ordered tasks from YAML playbooks to install packages, write configuration files and deploy software. Most teams that use both let Terraform create the machines and Ansible configure what runs on them.
Side-by-side comparison
| Terraform | Ansible | |
|---|---|---|
| Main job | Provisioning: create, change and delete infrastructure | Configuration management and task automation on hosts |
| Model | Declarative: describe the end state, Terraform computes the steps | Ordered tasks; each module is idempotent, but tasks run top to bottom |
| Language | HCL (HashiCorp Configuration Language) | YAML playbooks with Jinja2 templating |
| State | State file that maps configuration to real resources | No state file; checks each host on every run |
| How it reaches targets | Provider plugins call cloud and service APIs | SSH to Linux and Unix hosts, WinRM to Windows hosts |
| Agent on targets | None | None; managed nodes need Python for most modules |
| Preview of changes | terraform plan | ansible-playbook --check --diff, where modules support it |
| Removing something | Delete the block; the next plan destroys the resource | Write a task with state: absent; deleting the task changes nothing |
| License | Business Source License 1.1 since Terraform 1.6 | GPL v3 |
Declarative state vs procedural runs
The most practical difference is memory. Terraform keeps a state file that, in the words of its docs, maps “real world resources to your configuration” (Terraform state). When you remove a resource block, Terraform sees a resource in state that is no longer in the code and plans to destroy it. Before anything changes, terraform plan lists every create, update and delete.
Ansible has no record of previous runs. Its modules are declarative at the task level: a task that says a package must be present does nothing if it already is, and the Ansible docs describe playbooks as declaring “the desired state” of a system. But a playbook runs its tasks in order, and if you delete a task, Ansible simply stops running it. The package it installed stays on every host until you write a task that removes it.
So Terraform suits resources with a full lifecycle, where deleting from code should delete in the cloud, and Ansible suits the inside of a machine, where order matters. The state file has a cost: it needs a remote backend with locking once a team shares it, and it often holds secrets in plain text.
Providers, modules and agentless SSH
A Terraform provider is a plugin that translates resources such as aws_instance or hcloud_server into API calls. Clouds, DNS services, monitoring tools and many SaaS products publish one. Terraform never logs in to a machine; it manages what the provider API exposes.
Ansible’s equivalent is modules, distributed in collections. The control node, a Linux or macOS machine with Python, connects to each managed node over SSH, copies small Python programs over and runs them. The managed node needs only Python and an SSH account: no daemon and no extra open port.
Each tool can do part of the other’s job. Ansible has cloud modules that create instances, and Terraform has provisioners that run scripts on new machines. HashiCorp says provisioners are a last resort, and Ansible-created cloud resources have no state to drive later deletion. Using each tool for its own job usually ends up simpler.
Using Terraform and Ansible together
A common workflow:
- Terraform creates the network, firewall rules and virtual machines, and outputs their addresses.
- Ansible reads the inventory from Terraform, for example with the inventory plugin in the
cloud.terraformcollection and theansible/ansibleTerraform provider from the Ansible project, or from cloud tags with a dynamic inventory. - Ansible hardens the operating system, installs runtimes and agents, and deploys the application.
Two trends shrink Ansible’s part. Machines can configure themselves on first boot with cloud-init user data, or start from prebuilt images. And on Kubernetes, nodes only need a container runtime and a kubelet; applications ship as manifests through Helm or GitOps, not through playbooks. Ansible remains common for bare-metal servers, on-premises fleets, network devices and operating system patching. The article on running Kubernetes on-premises shows where that preparation work fits, and immutable node operating systems remove most of it.
Where OpenTofu and Pulumi fit
On August 10, 2023, HashiCorp moved future releases of Terraform to the Business Source License; Terraform 1.6 was the first release under it. The license permits commercial use except in products that compete with HashiCorp. IBM completed its acquisition of HashiCorp on February 27, 2025. Red Hat, which develops Ansible, is also an IBM company.
OpenTofu is the open-source fork of Terraform created in response to the license change. It is licensed under MPL 2.0 and run as a Linux Foundation project, and it was accepted into the CNCF at the Sandbox level on April 23, 2025. As of November 2026, the current release is 1.13. The OpenTofu FAQ describes it as a drop-in replacement that works with Terraform providers through its own registry, and with state files created by Terraform up to version 1.5.x. Newer Terraform features may not have an OpenTofu equivalent, so check before migrating a large codebase.
Pulumi follows the same provisioning model as Terraform, with desired state, a state backend and a preview before changes, but you write the infrastructure in a general-purpose language: TypeScript, JavaScript, Python, Go, .NET or Java, with YAML and HCL as further options (Pulumi languages). State goes to Pulumi Cloud by default, or to self-managed backends such as S3, Azure Blob Storage, Google Cloud Storage, PostgreSQL or a local file.
| Tool | Category | Language | License and steward |
|---|---|---|---|
| Terraform | Provisioning | HCL | BSL 1.1, HashiCorp (IBM) |
| OpenTofu | Provisioning | HCL | MPL 2.0, Linux Foundation, CNCF Sandbox |
| Pulumi | Provisioning | General-purpose languages, YAML, HCL | Open-source engine; Pulumi Cloud is a commercial service |
| Ansible | Configuration management | YAML | GPL v3, Red Hat (IBM) |
How to choose
- Creating cloud resources with a lifecycle: Terraform, OpenTofu or Pulumi. Pick OpenTofu if an open-source license under foundation governance matters to you, Pulumi if your team prefers a programming language to HCL.
- Configuring long-lived servers, network devices or on-premises machines: Ansible.
- Running on Kubernetes: provision clusters and cloud resources with an infrastructure as code tool, and manage workloads with Kubernetes manifests. Keep Ansible for hosts outside the cluster.
Terraform with Cloudfleet
The Cloudfleet Terraform provider manages Cloudfleet Kubernetes Engine (CFKE) clusters, Fleets for node auto-provisioning across AWS, GCP and Hetzner, and self-managed nodes. For existing servers, the cloudfleet_cfke_self_managed_node resource connects over SSH and joins the machine to the cluster. For new machines, the cloudfleet_cfke_node_join_information resource generates cloud-init user data so nodes join on first boot.