Use this when testing that automation is idempotent.
Idempotency Testing
Idempotency testing runs an automation twice against the same system: the first run may change things, the second must report no changes. Molecule and CI pipelines do this automatically.
1. Test idempotency automatically.
2. Molecule idempotence step; run-twice tests.
3. Fail CI on changes in second run.
4. Cover all roles/modules.
5. Fix sources of change.
first run → second run → assert no changes
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
Record run ID, target object ID, desired revision and comparison key/version; inspect the runtime/control-plane result; then query the target object in the intended state with no duplicate or unnecessary side effect on repeat through an independent read path and compare it with the versioned intent. For Idempotency → Idempotency Testing, the evidence must connect the control-plane result to the target object in the intended state with no duplicate or unnecessary side effect on repeat and explain any gap between desired, controller-reported and consumer-visible state.
Step 2 Common mistakes Narrow the fault
- No second-run check in CI.
Step 3 Where it fails Narrow the fault
Failure analysis for Idempotency → Idempotency Testing should follow the path through the components: desired intent → target read → normalize/compare → decide no-op/change → mutate → read-back → second-run no-op evidence. Common failure classes include ambiguous lookup, stale read, race between read/write, generated values causing perpetual diff, duplicate creation and declaring idempotency without second-run evidence. Before retrying a mutating operation, read current target state using the stable identity run ID, target object ID, desired revision and comparison key/version; if state is unknown, stop broad retries.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Run twice; second run must not change anything.
- Automate in CI (Molecule).
- Test on clean and existing systems.
molecule test— converge + idempotence- Run twice in CI and assert no changes — automation
terraform apply && terraform plan -detailed-exitcode— IaC check
ReferenceWhy it matters1 section · depth on demand
Method
Idempotency testing proves automatically that automation is idempotent, as part of CI.
1. Provision a test target (container, VM, virtual device).
2. Converge: run the automation once.
3. Run it again and capture the change count.
4. Fail the test if the second run reports any change.
5. Optionally verify state with separate assertions.
molecule converge && molecule idempotence
ansible-playbook site.yml -i test_inventory | tee run2.log
grep -q 'changed=0' run2.log || { echo 'not idempotent'; exit 1; }
Molecule's idempotence step does exactly this for Ansible roles.
ReferenceImportant terms & boundaries1 section · depth on demand
Mechanics
Idempotency → Idempotency Testing is owned at runtime by the automation engine and target read/write interface evaluating whether desired and actual state differ. Its durable intent is versioned desired-state operation with stable identity, comparison semantics and minimal mutation policy; the stable identity contract is run ID, target object ID, desired revision and comparison key/version. The consumer-visible target is the target object in the intended state with no duplicate or unnecessary side effect on repeat. The operating invariants are that identity is stable, comparison normalizes equivalent state, repeated execution is no-op, retries are safe and concurrent changes do not cause duplicate creation/update. Follow the chain of components desired intent → target read → normalize/compare → decide no-op/change → mutate → read-back → second-run no-op evidence and record which layer currently owns truth. The linked authority for bounded mechanics is Ansible Documentation — Idempotency and desired state; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Idempotency > Idempotency Testing. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_intro.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
Capture at minimum target ID, desired revision, before/after values, diff, changed/no-op flag, retry/attempt, error and second-run result.