Conditionals
Cheat sheet · https://pcornel.dk/topic.php?topic=5408
Answer
Ansible conditionals control whether tasks, roles or includes run based on expressions evaluated in host-specific variable/fact context.
Remember
- 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.
First checks
- when: conditions — logic
ansible-playbook site.yml --check -e var=x— test conditions- is defined / | bool usage — safety
Verify in this order
condition → run or skip task
Where it usually breaks
A true/false conditional proves only the evaluated selection decision for that context; it does not prove the selected action succeeds or the skipped action was unnecessary.
Inventory/vars → play/task selection → connection/become → module request → module result → target object read-back → handler/service effective state → workload verification.
Conditionals Ansible boundary map
| 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 |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5408