Pcornel
Automation · Ansible

Conditionals

Cheat sheet · https://pcornel.dk/topic.php?topic=5408

Back to the Topic

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

BoundaryHealthy evidenceSuspicious evidence
Inputs / selectionCorrect inventory, hosts, variables/facts and content are explicitWrong scope, stale facts, hidden precedence or content-version drift
ExecutionConnection, privilege, module/task context and ordering match intentUnreachable, wrong execution host, privilege/plugin failure or misleading skip/change state
Result semanticsPer-host/item results are preserved and interpreted correctlyAggregate recap hides partial/skipped/handled failures
Target proofResource/workload read-back and rerun confirm convergenceGreen 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

Confirmation

Confirm action

Continue?