IaC Beyond Terraform - Ansible for Provisioning and Configuration
1. Provisioning Is Not Configuring
In the previous article of this series, we looked at OpenTofu as an alternative (or drop-in replacement) to Terraform for provisioning infrastructure — creating VMs, networks, managed databases, buckets. But provisioning a server is only the first step: once the VM exists, someone needs to install packages, set up users, apply hardening, deploy the application and keep all of it consistent over time. That’s the space where Ansible comes in — and it’s common to see the two working together in the same pipeline, not as competitors.
2. Where Terraform Stops and Ansible Starts
The most useful distinction in practice is:
- Terraform (and OpenTofu) are provisioning tools: they talk to cloud APIs to create, update or destroy resources. The mental model is declarative and oriented around the desired state of the resource: “I want a VM of this instance type, on this network, with this disk”.
- Ansible is a configuration management tool: it connects to machines that already exist (over SSH, with no agent installed) and runs tasks to bring them to a desired state: “I want Nginx installed, this version, this configuration file, this service running”.
It’s not unusual to see both in the same pipeline: Terraform creates the VM and exposes its IP as an output; Ansible uses that IP to connect and configure what’s inside it. One takes care of the “hardware” (even if virtual), the other of the “software”.
3. Ansible Core Concepts
Before looking at real examples, it’s worth pinning down the vocabulary:
- Inventory: the list of machines Ansible manages, grouped logically (for example,
webservers,databases). It can be a static file (INI or YAML) or generated dynamically (e.g. from the tags of an AWS account). - Playbook: a YAML file that describes, in order, which tasks (
tasks) should run on which groups of machines from the inventory. - Module: the unit of work a task executes — there are ready-made modules for practically everything (
apt,yum,copy,service,user,template, cloud-specific modules, etc.). - Role: a way of packaging playbooks, variables, templates and files related to a specific responsibility (e.g. an
nginxrole that knows how to install and configure Nginx), reusable across projects. - Idempotency: like Terraform, Ansible is designed so that running the same playbook several times produces the same end result, with no side effects — if a package is already installed, the
aptmodule simply does nothing on the second run. - Agentless: unlike tools such as Puppet or Chef, Ansible doesn’t require installing a permanent agent on the managed machine — it connects over SSH (or WinRM, in the case of Windows), copies the necessary modules, runs them and cleans up afterwards. This simplifies the initial setup considerably.
4. Inventory in Practice
# inventory/hosts.ini
[webservers]
web1 ansible_host=10.0.1.10
web2 ansible_host=10.0.1.11
[databases]
db1 ansible_host=10.0.2.10
[webservers:vars]
ansible_user=deploy
ansible_ssh_private_key_file=~/.ssh/deploy_key
5. A Real Playbook: Configuring a Web Server After Provisioning
This example assumes the VM already exists (created by Terraform/OpenTofu) and focuses only on the configuration part: installing Nginx, creating a deploy user, copying a configuration file and making sure the firewall opens the right port.
# playbook-webserver.yml
- name: Configure web server
hosts: webservers
become: true
vars:
app_port: 8080
tasks:
- name: Update package cache
apt:
update_cache: true
cache_valid_time: 3600
- name: Install Nginx
apt:
name: nginx
state: present
- name: Create deploy user
user:
name: deploy
shell: /bin/bash
groups: www-data
append: true
- name: Copy custom Nginx configuration
template:
src: templates/nginx-site.conf.j2
dest: /etc/nginx/sites-available/app
owner: root
group: root
mode: "0644"
notify: Reload Nginx
- name: Enable the site
file:
src: /etc/nginx/sites-available/app
dest: /etc/nginx/sites-enabled/app
state: link
notify: Reload Nginx
- name: Open HTTP port in the firewall (ufw)
ufw:
rule: allow
port: "80"
proto: tcp
- name: Make sure Nginx is running and enabled on boot
service:
name: nginx
state: started
enabled: true
handlers:
- name: Reload Nginx
service:
name: nginx
state: reloaded
The referenced template uses Jinja2 variables, the same template engine Ansible uses internally:
# templates/nginx-site.conf.j2
server {
listen 80;
server_name {{ inventory_hostname }};
location / {
proxy_pass http://127.0.0.1:{{ app_port }};
proxy_set_header Host $host;
}
}
Running the playbook:
ansible-playbook -i inventory/hosts.ini playbook-webserver.yml
Note the use of notify/handlers: the task that copies the template and the one that enables the site notify the Reload Nginx handler, which only runs once at the end, even if multiple tasks trigger it — avoiding restarting the service several times unnecessarily in a single run.
6. Integrating Terraform and Ansible in the Same Flow
A common way to connect the two tools is to use Terraform’s output to generate the Ansible inventory dynamically, instead of keeping the IPs hardcoded:
# outputs.tf
output "web_ips" {
value = aws_instance.web[*].public_ip
}
# Generate the inventory from the Terraform output
terraform output -json web_ips | jq -r '.[]' > inventory/hosts_dynamic.txt
ansible-playbook -i inventory/hosts_dynamic.txt playbook-webserver.yml
In more mature pipelines, this usually becomes a dynamic inventory plugin (Ansible has official plugins for AWS EC2, Azure, GCP, etc.), eliminating this manual step of exporting and converting outputs.
7. Can Ansible Replace Terraform?
Technically, Ansible has cloud modules (amazon.aws.ec2_instance, azure.azcollection.azure_rm_virtualmachine, etc.) that can create infrastructure resources, not just configure them. In practice, this is used less often, for a few reasons:
- Terraform keeps an explicit state and handles drift more robustly — it knows exactly what it created and detects divergences. Ansible’s cloud modules tend to be more imperative, without that native tracking.
- Terraform’s (and OpenTofu’s) provider ecosystem for cloud APIs is more complete and up to date than Ansible’s cloud modules.
- Mixing responsibilities — using Ansible both to create and to configure — tends to make the pipeline harder to reason about in terms of “what exists” versus “how it’s configured”.
That’s why the most common combination on the market is still: Terraform (or OpenTofu) provisions, Ansible configures.
8. Conclusion
Ansible doesn’t compete with Terraform — it solves a different, complementary problem: what to do once the infrastructure already exists. Inventory, playbooks, modules and idempotency are the building blocks for reliably automating server configuration, and integrating with Terraform’s output closes the full provisioning + configuration loop. In the next and final article of this series, we change subjects: how to test Terraform code before running apply in production, using Terratest, checkov and tflint.
References: