Use it when configuring, checking or troubleshooting Conditionals in Ansible.
Conditionals
Ansible conditionals control whether tasks, roles or includes run based on expressions evaluated in host-specific variable/fact context.
1. when skips or runs tasks.
2. Use facts and variables.
3. Cast with | bool.
4. Test both paths.
5. Avoid complex conditions — compute vars.
condition → run or skip task
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 Operational evidence path Verify boundary
host/item context → variables/facts/types → conditional expression → run/skip decision → task action → target state
Healthy evidence: effective inputs and types explain the decision, host-specific behavior is expected and selected actions are separately validated.
Step 2 Failure modes and next checks Narrow the fault
Suspicious evidence: quoted booleans, undefined/default masking, stale facts, host-specific divergence or interpreting skip as failure/root cause without evaluating condition.
Step 3 Common mistakes Narrow the fault
when:with Jinja braces ({{ }}), which triggers warnings.- Comparing strings and booleans.
Step 4 Where it fails Narrow the fault
Where Conditionals usually goes wrong: evaluation context, truthiness/type rules, short-circuiting, missing data and deterministic branching.
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.
- Conditional false ≠ task failure; skipped is a deliberate result state.
- Same expression ≠ same result on every host; variables/facts can differ.
- String "false" ≠ boolean false necessarily; type and expression semantics matter.
- Condition passed ≠ action outcome correct; it only selected the path.
- when: conditions — logic
ansible-playbook site.yml --check -e var=x— test conditions- is defined / | bool usage — safety
ReferenceWhat it is1 section · depth on demand
Core model
For each skipped/unexpected execution, capture the exact when expression, effective values/types and host/item context; evaluate truth semantics rather than inferring from visible declarations.
ReferenceImportant terms & boundaries2 sections · depth on demand
What this shows and what it does not
A true/false conditional shows only the evaluated selection decision for that context, but it does not show the selected action succeeds or the skipped action was unnecessary.
In depth
Ansible conditionals use when: with a Jinja2 expression to decide whether a task runs, based on facts, variables or registered results.
- name: Install on Debian family only
ansible.builtin.apt: { name: chrony, state: present }
when: ansible_facts['os_family'] == 'Debian'
- name: Alert if disk is low
ansible.builtin.debug: msg="low disk on {{ inventory_hostname }}"
when: (ansible_facts.mounts | selectattr('mount','eq','/') | first).size_available < 5 * 1024**3
| Tip | Reason |
|---|---|
No {{ }} inside when | It is already an expression |
Apply the bool filter to string inputs | 'false' string is truthy |
Combine with and/or or a list | List items are ANDed |
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 Ansible > Conditionals. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.ansible.com/ansible/latest/ · confidence 94%Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
Conditionals Ansible boundary map
Fast input/selection, execution/result and target-proof boundaries for Conditionals.
| Boundary | Healthy evidence | Suspicious evidence |
|---|---|---|
| Inputs / selection | Correct inventory, hosts, variables/facts and content are explicit | Wrong scope, stale facts, hidden precedence or content-version drift |
| Execution | Connection, privilege, module/task context and ordering match intent | Unreachable, wrong execution host, privilege/plugin failure or misleading skip/change state |
| Result semantics | Per-host/item results are preserved and interpreted correctly | Aggregate recap hides partial/skipped/handled failures |
| Target proof | Resource/workload read-back and rerun confirm convergence | Green Ansible status treated as end-to-end acceptance |
Deep reference
Healthy vs suspicious evidence
Output interpretation
For Conditionals, rely on structured fields and stable IDs (evaluation context, truthiness/type rules, short-circuiting, missing data and deterministic branching) rather than text formatted for display.