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/modediffer 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.
More on Modules, Idempotency and Handlers
`ERROR! The requested handler 'Restart nginx' was not found in either the main handlers list nor in the listening handlers list`BeginnerA handler named `restart {{ app_service }}` is never found when notified as `restart myapp`AdvancedA `command` task reports `changed` on every run, and the Molecule idempotence check failsBeginner`curl ... | tar xz` fails on the host but the `shell` task reports successIntermediate`lineinfile` appends the same line again on every runIntermediate`lineinfile` with `backrefs: true` reports `ok` but the line was never addedIntermediate