Use this when making automation safe to run again without side effects.
Idempotency
Idempotency means running an automation once or many times gives the same end state, because it checks current state and only changes what differs. It makes reruns and retries safe.
1. Idempotency: running again changes nothing when state is correct.
2. Compare desired vs current.
3. Create-if-missing, update-if-different, otherwise no-op.
4. Test by running twice.
5. Enables safe retries and enforcement.
desired → compare → 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 Target-state verification ladder Verify boundary
Idempotency uses a strict target-state ladder for repeat execution with no unintended delta: request accepted → job/task succeeded → target object changed → effective runtime state confirmed → original workload verified.
Step 2 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 repeated execution against unchanged desired state should produce a no-op, while drift or changed intent produces the minimal required mutation.
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.
Subtopics
Go one level deeper into this subject.
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.
Subtopic Desired StateDesired state in idempotency is the target description the operation compares against, such as 'package present at version 1.2', so it knows whether any change is needed.
Subtopic State ComparisonState comparison reads the current value, compares it with the desired value and decides whether to act, which is the core of every idempotent step and of diff or check-mode output.
Subtopic Create-if-MissingCreate-if-missing is an idempotent pattern: check whether a resource exists and create it only if it does not, such as a user, folder or VLAN, so reruns do not create duplicates or fail.
Subtopic Update-if-DifferentUpdate-if-different changes a resource only when its current properties differ from the desired ones, reporting exactly what changed, instead of rewriting it on every run.
Subtopic No-opA no-op is a run that finds everything already in the desired state and changes nothing, reported as ok or unchanged.
Subtopic Repeated ExecutionRepeated execution means automation can safely run again after a failure, on a schedule or by mistake, because idempotent steps skip work that is already done.
Subtopic Idempotency TestingIdempotency testing runs an automation twice against the same system: the first run may change things, the second must report no changes.
- Check current state before every change.
- A second run should report no changes.
- Prefer set over append operations.
- Second run reports no changes — proof
terraform plan -detailed-exitcode— 0 after apply- Idempotency tests in CI (Molecule idempotence) — automation
ReferenceWhy it matters1 section · depth on demand
How it works in operation
Idempotency means repeated execution converges without unintended change; it does not by itself show the desired state is correct or useful.
Path: Automation → Idempotency.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Idempotency is the property that makes automation trustworthy: an operation can be repeated any number of times and the result is the same as doing it once. It allows safe retries after failures, scheduled runs that correct drift, and confident re-runs when nobody is sure what happened last time.
The topics in this group cover the patterns that achieve it:
| Pattern | Idea |
|---|---|
| State comparison | Compare current with desired, act on differences |
| Create-if-missing | Create only when absent |
| Update-if-different | Change only fields that differ |
| No-op | Report "nothing to do" clearly |
| Repeated execution / testing | Show a second run changes nothing |
Tools like Ansible modules, Terraform, Kubernetes and DSC are idempotent by design; idempotency is lost mainly in raw shell commands, scripts and custom API calls.
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. 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.