Use this when updating resources only when they differ.
Update-if-Different
Update-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.
1. Update-if-different: compare then update only real differences.
2. Minimal changes.
3. Accurate changed status.
4. Avoid always-updating timestamps.
5. Test.
current vs desired → different? update : no-op
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 → Update-if-Different, 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
- Comparing unnormalised values (case, whitespace).
Step 3 Where it fails Narrow the fault
Failure analysis for Idempotency → Update-if-Different 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.
- Compare each property with the desired value.
- Update only what differs.
- Report exactly what changed.
- Compare fields before update — diff
- Update only changed fields (PATCH) — minimal change
- Report changed=true only when updated — accuracy
ReferenceWhy it matters1 section · depth on demand
Method
Update-if-different is the pattern behind idempotent automation: read current state, compare with desired, and only write when they differ.
1. Fetch the current value from the target.
2. Normalise both values (case, whitespace, ordering, defaults).
3. Compare.
4. If equal, report "ok" and do nothing.
5. If different, apply the change and report "changed" with a diff.
6. Read back to confirm.
current = api.get(f"/vlans/{vid}").json()
desired = {"id": vid, "name": name}
if {k: current.get(k) for k in desired} != desired:
api.put(f"/vlans/{vid}", json=desired)
changed = True
Normalisation is where most bugs live: a device that returns Users for users makes every run report a change.
ReferenceImportant terms & boundaries1 section · depth on demand
Mechanics
Idempotency → Update-if-Different 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 > Update-if-Different. 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.