Use this when deciding whether a step needs to act.
State Comparison
State 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.
1. State comparison must normalise (sort lists, defaults, formats) to avoid false changes.
2. Read fresh state.
3. Show diff.
4. Act only on real differences.
5. Test edge cases.
current + desired → normalised diff → actions
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 normalization and identity alignment between desired/current state, ignored/computed fields and producing a minimal, explainable diff.
Step 2 Common mistakes Narrow the fault
- Comparing text with different formatting (normalise first).
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.
- Read the current value first.
- Compare it with the desired value.
- Act only when they differ.
- Read current state before change — facts
- Normalised comparison (ordering, defaults) — accurate diff
- Diff output — visibility
ReferenceWhy it matters1 section · depth on demand
What it is
State comparison reads current state, compares it with desired state and changes only differences.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
State comparison is the core of idempotent automation: read the current state, compare it with the desired state, and act only on the differences. The comparison must be normalised — the same configuration can be written in different orders, with default values shown or hidden, or with different formatting — or every run reports false differences.
Practical points:
- Compare structured data (JSON, YANG, API objects) rather than raw text where possible.
- Normalise ordering, case, whitespace and defaults before comparing.
- Show the diff before applying, so reviewers see exactly what will change.
current = {v["vid"]: v["name"] for v in get_vlans(dev)}
desired = {10: "MGMT", 20: "USERS"}
to_add = {k: v for k, v in desired.items() if current.get(k) != v}
to_remove = set(current) - set(desired)
terraform plan # comparison
ansible-playbook site.yml --check --diff
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 > State Comparison. 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 normalization and identity alignment between desired/current state, ignored/computed fields and producing a minimal, explainable diff, prefer typed fields and stable IDs over human presentation text.