What are conditionals (when) in Ansible playbooks?
Learn how Ansible's when keyword runs tasks conditionally using facts, variables, and registered results, with idiomatic examples and interview tips.
Expected Interview Answer
Conditionals in Ansible use the `when` keyword to run a task only if a given expression evaluates to true, letting the same playbook behave differently per host or situation.
The `when` clause takes a raw Jinja2 expression (no surrounding {{ }}) that Ansible evaluates against facts, variables, and registered results. You can combine conditions with `and`/`or`, test operating-system facts like `ansible_facts['os_family']`, check registered command output with `.rc` or `.stdout`, and provide a list of conditions which are ANDed together. When the expression is false the task is skipped for that host rather than failing.
- Runs tasks only where they apply, avoiding errors on the wrong hosts
- Enables one playbook to support many OS families or environments
- Reacts to runtime state via registered variables and facts
- Keeps playbooks idempotent by skipping unnecessary work
- Reduces duplication compared to writing separate playbooks
AI Mentor Explanation
A captain sets a field only when conditions warrant it: bring in a third slip when the ball is new and swinging, drop it when the ball goes soft. The decision is checked against the live match state before the change is made. Ansible's `when` works the same way — a task fires only if the current condition, like the OS family or a registered result, evaluates true for that host.
Step-by-Step Explanation
Step 1
Attach when to a task
Add a `when:` key to any task with a raw Jinja2 expression — no surrounding {{ }} braces.
Step 2
Reference facts or variables
Use gathered facts like ansible_facts['os_family'] or your own vars inside the expression.
Step 3
Register runtime results
Use `register:` on a prior task, then test its `.rc`, `.stdout`, or `.changed` in a later when.
Step 4
Combine conditions
Join with `and`/`or`, or pass a YAML list of conditions which Ansible ANDs together.
Step 5
Observe skips
When the expression is false the task shows as 'skipping' for that host instead of running.
What Interviewer Expects
- Knows when takes a raw expression without {{ }}
- Can test facts such as os_family or distribution
- Uses registered variables in conditions
- Understands a list of conditions is ANDed
- Explains that a false condition skips rather than fails the task
Common Mistakes
- Wrapping the when expression in {{ }} braces unnecessarily
- Assuming a skipped task is a failure
- Testing a registered variable before the registering task ran
- Confusing string 'false' with the boolean false
- Forgetting that a list of when conditions is joined with AND, not OR
Best Answer (HR Friendly)
“Conditionals let a playbook decide whether to run each step based on the situation, using the `when` keyword. For example, a task can install software only on Ubuntu machines and quietly skip everything else, so one playbook safely handles many kinds of servers.”
Code Example
- name: Install Apache on Debian family
ansible.builtin.apt:
name: apache2
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Check if service exists
ansible.builtin.command: systemctl status nginx
register: nginx_status
ignore_errors: true
- name: Restart only if nginx is present
ansible.builtin.service:
name: nginx
state: restarted
when:
- nginx_status.rc == 0
- ansible_facts['distribution'] == "Ubuntu"Follow-up Questions
- How do you test a registered variable inside a when condition?
- What is the difference between a list of when conditions and using 'and'?
- How do conditionals interact with loops using item?
- Why must the when expression omit the {{ }} braces?
- How can you combine when with the failed_when and changed_when keywords?
MCQ Practice
1. Which keyword applies a conditional to an Ansible task?
Ansible uses the `when` keyword, which takes a raw Jinja2 expression, to conditionally run a task.
2. How should the expression in a when clause be written?
The when value is already a Jinja2 expression context, so you write it raw without the {{ }} delimiters.
3. When a when condition evaluates to false, the task is:
A false condition causes Ansible to skip the task for that host, not fail it.
Flash Cards
What does when do in Ansible? — Runs a task only if its Jinja2 expression evaluates to true for the host, otherwise skips it.
Do you use {{ }} in a when expression? — No — the when value is already a Jinja2 expression, so you write it raw without braces.
How is a YAML list of when conditions combined? — All conditions in the list are ANDed together; every one must be true for the task to run.
How do you condition on a command's result? — Register the command's output, then test the registered variable's .rc or .stdout in when.