Use this when a resource should be created only if missing.
Create-if-Missing
Create-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.
1. Create-if-missing: look up by unique key; create only if absent.
2. Treat 'already exists' as success.
3. Race conditions — use atomic APIs if available.
4. Log decisions.
5. Test reruns.
lookup → missing? create : 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
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 existence/read-before-create checks, stable lookup keys, race-safe creation and no-op behavior when the object already exists.
Step 2 Common mistakes Narrow the fault
- Check-then-create races in parallel runs.
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.
- Look up the resource before creating it.
- Use a unique key for the lookup.
- Report whether it was created or already existed.
- Check existence before create — guard
- Unique keys for lookup — identification
- Handle 409/already exists as success — idempotency
ReferenceWhy it matters1 section · depth on demand
What it is
Create-if-missing checks whether an object exists before creating it.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Create-if-missing is the simplest idempotent pattern: check whether an object exists, and create it only if it does not. Running it again finds the object and does nothing. It does not correct an object that exists with wrong settings — that requires update-if-different.
r = requests.get(f"{api}/vlans/?vid=20", headers=h)
if r.json()["count"] == 0:
requests.post(f"{api}/vlans/", headers=h, json={"vid": 20, "name": "USERS"})
id -u svc_backup >/dev/null 2>&1 || useradd --system svc_backup
mkdir -p /opt/app # -p: no error if it exists
if (-not (Get-ADGroup -Filter "Name -eq 'SG-Backup'")) { New-ADGroup SG-Backup -GroupScope Global }
Race condition warning: two parallel runs can both see "missing" and both create. Use unique constraints or locks where duplicates would cause harm.
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 > Create-if-Missing. 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 existence/read-before-create checks, stable lookup keys, race-safe creation and no-op behavior when the object already exists, prefer typed fields and stable IDs over human presentation text.