Skip to content

Lesson 1 of 9 · 25m

Idempotency Is a Design Contract, Not a Module Feature

Understand convergence, change reporting, and the difference between repeating a command safely and modeling desired state correctly.

The worst Ansible outage I have debugged was not caused by a complicated playbook. It was a single command: systemctl restart sshd hidden in a routine hardening run. Every execution restarted SSH on every host, health checks flapped, and the on-call engineer learned the difference between safe to repeat and safe to ignore. Idempotency is not that a command can run twice. It means repeated runs converge the system to the same declared state without unnecessary side effects.

Desired state beats shell history

A shell script records what a human did once. A configuration management task declares what must remain true:

- name: Ensure nginx is installed
  ansible.builtin.package:
    name: nginx
    state: present

- name: Ensure nginx is running and enabled
  ansible.builtin.service:
    name: nginx
    state: started
    enabled: true

The package module inspects current state before acting. If nginx is already present and running, Ansible reports ok rather than manufacturing change. If the package is absent or the service is stopped, it converges the host toward the declaration.

changed is an operational signal

Ansible's output colors are not decoration. They are a contract between modules and operators:

ok=4      no difference from desired state
changed=1 the task observed or caused a mutation
failed=1  convergence could not be proven or completed

If every run reports changes for a stable configuration, your automation is noisy. Operators stop trusting change counts, reviews become rubber stamps, and real regressions hide in the noise. Treat permanent changed output as a bug in the role.

Command modules need guardrails

command, shell, and raw are sometimes necessary, but they usually know nothing about system state. Give them explicit idempotence controls:

- name: Initialize the application database once
  ansible.builtin.command: /opt/app/bin/manage.py migrate --create-extensions
  args:
    creates: /var/lib/app/.migrations-initialized

creates tells Ansible the artifact that proves the work already happened. For commands without a stable artifact, use when against a fact you can verify, or write a small custom module. A shell fragment with ten clever pipes is rarely a good long-term configuration interface.

Mutating state outside Ansible breaks the loop

Configuration management assumes repeated reconciliation. If operators hot-edit /etc/nginx/nginx.conf by hand, Ansible may overwrite it later or, worse, only partly restore the managed state. Decide which files are owned by automation and mark them clearly:

# Managed by Ansible. Local changes will be overwritten.
# Role: webserver | Template: nginx.conf.j2

This is not bureaucratic noise. It is a warning label on a sharp edge.

Field notes

  • Idempotency does not mean zero changes on the first run; first convergence is supposed to change things.
  • A task can be idempotent at the file level and still trigger disruptive service behavior through handlers.
  • check_mode support varies by module. Never assume a dry run proves mutation safety unless the role was designed for it.
  • If you need shell arithmetic to decide whether a task should run, pause and ask whether the state model is wrong.
  • The best test of idempotency is running the same role twice and requiring the second run to produce no unexpected changes.

Was this lesson helpful?

Sign in to rate