Interview Prep/Ansible/Modules, Idempotency and Handlers

A `template` task reports `changed` and restarts the service on every run

IntermediateTroubleshootingModules, Idempotency and Handlers
Answer

Likely causes →

  • The template contains something that changes on every run: {{ ansible_date_time.iso8601 }}, now(), a random value, or {{ groups['web'] }} in non-deterministic order.
  • The file is modified after deployment by the application or another tool.
  • owner/group/mode differ from what the package resets on update.

Diagnose →

ansible-playbook site.yml -l web01 --check --diff --tags nginx

The diff shows exactly which line changes.

Fix → Remove the timestamps (use {{ ansible_managed }} without a date), sort lists (| sort), and seed random values (random(seed=inventory_hostname)).

Prevent → Run each role twice in Molecule. A second run must show changed=0.

All Ansible questions Roadmap: Ansible

More on Modules, Idempotency and Handlers