A `command` task reports `changed` on every run, and the Molecule idempotence check fails
BeginnerTroubleshootingModules, Idempotency and Handlers
Answer
Likely causes → command and shell cannot know whether they changed anything, so they always report changed: true.
Fix → Choose the option that fits:
# 1. Run only if an artifact is missing
- ansible.builtin.command: /opt/app/bin/init-db
args:
creates: /var/lib/app/.initialised
# 2. Read-only commands
- ansible.builtin.command: /opt/app/bin/app --version
register: v
changed_when: false
# 3. Decide from the output
- ansible.builtin.command: /opt/app/bin/migrate
register: m
changed_when: "'No migrations to apply' not in m.stdout"
Prevent → Prefer a real module when one exists. ansible-lint rule no-changed-when flags these tasks.
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`Advanced`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 addedIntermediateA `template` task reports `changed` and restarts the service on every runIntermediate