Quiz Entry - updated: 2026.11.12
How does the when statement decide whether a task runs?
when takes a condition: if it is true the task runs, if it is false the task is reported as skipped (not failed) and the play continues.
The simplest condition is a boolean variable:
- name: Simple Boolean Task Demo
hosts: all
vars:
run_my_task: true
tasks:
- name: httpd package is installed
ansible.builtin.dnf:
name: httpd
when: run_my_task
Two details are worth knowing:
- The value of
whenis already a Jinja2 expression, so you writewhen: run_my_task, notwhen: "{{ run_my_task }}". The braces are redundant and Ansible warns about them. - A skipped task is a normal outcome. It shows as
skipping:in the output and counts underskipped=in the recap; it neither fails the host nor notifies handlers.
Go deeper:
Basic conditionals with when (official docs) โ the syntax and more examples.