Use this when making a job safe to rerun after partial failure.
Idempotency · Automation Design
Idempotency as a design principle means every step checks state before acting, so the whole job can be rerun safely after a partial failure without creating duplicates or breaking things.
1. Design every step to be idempotent.
2. Check state before acting.
3. Use declarative modules.
4. Test by running twice.
5. Idempotency enables safe retries and resume.
desired state → check → change only if needed
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
The negative fixture should deliberately exercise risks such as implicit defaults, hidden dependencies, duplicate dispatch, worker drift, timeout without cancellation, retries against non-idempotent operations and confusing control-plane completion with target convergence, choosing the cases most relevant to repeated execution against unchanged desired state should produce a no-op, while drift or changed intent produces the minimal required mutation.
Step 2 Common mistakes Narrow the fault
- Tasks that report changed on every run.
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.
- Each step checks state first.
- Reruns must not duplicate resources.
- Prove it by running twice.
ansible-playbook site.yml && ansible-playbook site.yml— second run changed=0terraform plan -detailed-exitcode— 0 after apply- Guarded imperative steps (creates/unless) — safety
ReferenceWhy it matters1 section · depth on demand
Internals and state
Idempotency means repeated execution converges without unintended change; it does not by itself show the desired state is correct or useful.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Idempotency means running the same automation twice gives the same result as running it once: the second run finds everything already correct and changes nothing. It is the property that makes automation safe to re-run after a failure, on a schedule, or by someone who is unsure whether it already ran.
How to achieve it: describe the desired end state instead of actions ("VLAN 20 exists with name USERS" rather than "run vlan 20"), compare current state with desired state before acting, and report changed only when something was actually modified.
Test: run twice in a row; the second run must report zero changes. Any change on the second run points to a non-idempotent step — commonly shell commands, timestamps written into files, or templates that render differently each time.
ansible-playbook site.yml --check --diff # preview
ansible-playbook site.yml && ansible-playbook site.yml | tail -3 # second run: changed=0
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 Automation Design > Idempotency. 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
For repeated execution against unchanged desired state should produce a no-op, while drift or changed intent produces the minimal required mutation, prefer typed fields and stable IDs over human presentation text.