InsightsAutomation
Terraform or Ansible: where provisioning ends and configuration begins
A practical line between the two tools, how to hand off from one to the other without coupling them, and the patterns that cause drift.
Published November 17, 20263 min read
"Should we use Terraform or Ansible?" is usually the wrong question. Both are good at what they were built for, and most estates need both. Trouble starts when the line between them is drawn differently in every repository, so that the same server is half-managed by each tool and fully understood by neither.
What each tool is for
Terraform describes resources and keeps a state file of what it created. On each run it compares that state with the configuration and the real world, then plans the difference. That model suits anything with an API and a lifecycle, such as networks, clusters, databases, DNS records and IAM.
Ansible runs ordered tasks against hosts over SSH or WinRM, with modules that only change what is not already in the desired state. It keeps no state of its own. That suits what happens inside a machine (packages, files, services, users) and procedures where order matters, such as a rolling restart or a database failover.
A line that holds up
| Concern | Tool |
|---|---|
| Networks, subnets, security groups, load balancers | Terraform |
| Virtual machines and their disks, tags and IAM roles | Terraform |
| Managed services: databases, queues, clusters, DNS | Terraform |
| Operating system configuration, packages, users | Ansible, or baked into the image |
| Application deployment to virtual machines | Ansible |
| Ordered operational procedures: rolling restarts, patching waves | Ansible |
| Network devices without a good Terraform provider | Ansible |
In short, Terraform builds the machine and its surroundings, and Ansible configures what runs inside. Where possible, move the configuration into the image itself: Packer and Ansible build a golden image once, and Terraform launches it. Machines then start in their final state, with nothing to run after boot.
Hand off through inventory, not through Terraform
The usual way to couple the tools is a local-exec provisioner that runs ansible-playbook at the end of terraform apply. It looks convenient, but it ties two lifecycles together:
- A failed playbook marks the resource as tainted, and the next apply tries to replace a working machine.
- Re-running configuration means running Terraform.
- A configuration change cannot be reviewed separately from an infrastructure plan.
Instead, let Terraform tag what it creates, and let Ansible discover hosts by those tags with a dynamic inventory:
resource "aws_instance" "web" {
count = 3
ami = data.aws_ami.base.id
instance_type = "t3.medium"
tags = {
Role = "web"
Environment = "production"
}
}plugin: amazon.aws.aws_ec2
regions: [eu-west-1]
filters:
tag:Environment: production
instance-state-name: running
keyed_groups:
- key: tags.Role
prefix: roleThe play then targets role_web. Neither tool needs to know the other exists. The same works on VMware with the community.vmware.vmware_vm_inventory plugin, and on other clouds with their own inventory plugins.
Patterns that cause drift
- Ansible creating cloud resources. Ansible modules can create a load balancer, but without state nobody can tell later whether it is still needed, and nothing will clean it up.
- Terraform managing files on servers. A provisioner runs only when the resource is created. Edit the script later and existing servers never see the change.
- Both tools touching the same thing. If Terraform sets a security group rule and a playbook adds another, Terraform removes the playbook's rule on its next apply. Pick one owner for every setting.
Where Ansible still shines
Containers and managed services have reduced how much configuration lives on servers, but several areas are still Ansible's:
- Long-lived estates: VMware clusters, bare-metal hosts, appliances.
- Network equipment.
- Operational runbooks that need to run in a precise order across many hosts.
For those, a well-structured collection of roles, run from CI with the same review as any other change, beats a wiki page of manual steps.
The result
Each tool does the job it is built for, and a reviewer can tell from the repository which tool owns a given setting. Changes to infrastructure and changes to configuration can each be planned, reviewed and rolled back on their own.