Ansible vs Immutable Infrastructure
On this page
Snowflake servers and configuration drift
A fleet of servers is configured by hand over the years. Someone patches one, someone tweaks a config on another. Eventually no two servers are alike, and the one that "just works" can't be rebuilt. These are snowflake servers, and they make every incident and migration harder.
Configuration management with Ansible
Tools like Ansible, Chef, Puppet and Salt describe the desired configuration and apply it to servers. Ansible is the most common today: YAML playbooks, run over SSH, no agent on the servers.
The key property is idempotency: running the same playbook twice leaves the system the same, and the second run changes nothing. A playbook states what should be true ("this line is in the file"), not a sequence of commands ("append this line"), which would add a duplicate every run.
Tested on a local machine, run twice:
- name: Idempotency demo
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Ensure the config directory exists
ansible.builtin.file:
path: /tmp/ansible-demo/conf.d
state: directory
mode: "0755"
- name: Ensure the worker count is set
ansible.builtin.lineinfile:
path: /tmp/ansible-demo/conf.d/app.conf
line: "workers = 4"
create: true
mode: "0644"# first run
localhost : ok=2 changed=2 unreachable=0 failed=0
# second run
localhost : ok=2 changed=0 unreachable=0 failed=0The first run made two changes; the second found everything already correct and changed nothing. changed=0 on a repeat run is the sign of a well-written playbook, and --check --diff shows what would change without changing it.
Immutable infrastructure
Configuration management keeps changing long-lived servers. Immutable infrastructure stops changing them at all:
- Build a machine image (AMI) or container image with everything installed and configured.
- Deploy new servers or containers from that image.
- To change anything, build a new image and replace the old instances. Never SSH in to fix things.
| Mutable (configure in place) | Immutable (replace) | |
|---|---|---|
| Drift | Accumulates over time | Can't happen; instances are disposable |
| Rollback | Re-run old config, hope it reverses cleanly | Deploy the previous image |
| Speed of change | Fast for small tweaks | Requires a new image build |
| Fits | Long-lived servers, network devices, legacy systems | Containers, Auto Scaling groups, cloud-native services |
Containers made immutability the default: every deploy is a new image. For virtual machines, Ansible still has a role, but at build time: Packer runs a playbook to bake an image, and that image is what's deployed:
- name: Bake the web image
hosts: image_builder
become: true
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Deploy nginx config
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: "0644"
validate: nginx -t -c %s
notify: Reload nginx
- name: Enable nginx at boot
ansible.builtin.systemd_service:
name: nginx
enabled: true
handlers:
- name: Reload nginx
ansible.builtin.systemd_service:
name: nginx
state: reloadedansible-playbook --syntax-check passes. The validate option tests the rendered config with nginx -t before replacing the live file, and the handler reloads nginx only when the config changed.
Ansible vs immutable infrastructure: where each fits
- Immutable for application servers and containers.
- Ansible for building images, for systems that can't be replaced easily (databases on VMs, network devices, on-premises servers), and for one-off operational tasks across a fleet.
- Terraform (Day 16) for creating the infrastructure itself; Ansible for what runs inside it.
Check yourself
Running it any number of times produces the same end state. The first run makes the needed changes, and later runs report no changes because everything already matches, so it's safe to rerun.
You deploy the previous image, which is known to work, instead of trying to reverse configuration changes on live servers, which often aren't cleanly reversible and leave servers in unknown states.
At image build time, with tools like Packer running Ansible to configure the image, and for systems that can't simply be replaced, such as network devices or long-lived databases.
Servers that started identical slowly differ due to manual fixes, partial updates and one-off changes, so behavior differs between them and nobody knows the true configuration.
It connects over SSH, or WinRM on Windows, and runs modules remotely, so there's nothing to install or keep updated on managed hosts besides Python.