Use this when designing operations that can repeat safely.
Idempotent Operations
Idempotent operations can be repeated without changing the result after the first run, such as setting a value rather than appending it, or using PUT instead of POST in APIs.
1. Idempotent operations produce the same result when repeated.
2. Use ensure/upsert semantics.
3. Avoid append-only actions without checks.
4. Guard imperative commands.
5. Test.
ensure X → present? no-op : create
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 ambiguous target identity, stale inputs, partial success, unsafe retry, permission drift and confusing tool completion with target-evidence of the actual state, choosing the cases most relevant to stable lookup keys, compare-before-write or server idempotency semantics, safe retries and proving second-run no-change behavior.
Step 2 Common mistakes Narrow the fault
- Append operations that duplicate config.
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.
- Set, don't append.
- PUT over POST.
- Check before change.
- Operations designed as "ensure state" — idempotent
- PUT/upsert vs POST — API choice
- Guards on commands (creates/unless) — protection
ReferenceWhy it matters1 section · depth on demand
What it is
An idempotent operation gives the same result whether it runs once or many times.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
An idempotent operation has the same effect whether it runs once or many times. Some operations are naturally idempotent (setting a value, HTTP PUT, deleting something that may already be gone); others are not (appending a line, incrementing a counter, HTTP POST that creates a new object each time).
| Not idempotent | Idempotent alternative |
|---|---|
echo line >> file | lineinfile / check before adding |
| POST /objects | Search first, or PUT with a known ID |
useradd bob | Create-if-missing |
| Toggle a setting | Set it to an explicit value |
vlan 20 + name X blindly | Compare running config, then apply diff |
Building automation from idempotent operations means any run can be repeated safely, which simplifies retries, resume and scheduled enforcement.
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 > Idempotent Operations. 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 stable lookup keys, compare-before-write or server idempotency semantics, safe retries and proving second-run no-change behavior, prefer typed fields and stable IDs over human presentation text.