Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Conditionals

Direct answer

Ansible conditionals control whether tasks, roles or includes run based on expressions evaluated in host-specific variable/fact context.

When to use this

Use it when configuring, checking or troubleshooting Conditionals in Ansible.

First 30 seconds

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.

Operational verification

condition → run or skip task

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Ansible automation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformAnsible automation · Conditionals owns Ansible run/skip expression semantics. Variables/Facts/Jinja2 own their input-resolution details.
Scope / prerequisiteUse it when configuring, checking or troubleshooting Conditionals in Ansible.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

4 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points
  • 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.
Key checks
  • 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
TipReason
No {{ }} inside whenIt is already an expression
Apply the bool filter to string inputs'false' string is truthy
Combine with and/or or a listList items are ANDed
Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docAnsible automation current

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%
Terminology layer

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.

Aliases that resolve here
ansible whentask conditionalansible conditionskipped task
Embedded reference data

Quick lookup tables

Conditionals Ansible boundary map

Fast input/selection, execution/result and target-proof boundaries for Conditionals.

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
Depth when you need it

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.

Confirmation

Confirm action

Continue?