Quiz Entry - updated: 2026.11.12
Where must when be placed in a task, and why there?
At the task's top level, aligned with name and the module, not indented under the module's arguments.
* when is a key of the task, not an argument of the module. *
A task is a YAML dictionary. Its top-level keys are task keywords that Ansible itself reads (name, when, loop, register, notify), plus exactly one module key whose nested dictionary holds the module's arguments.
- name: Install httpd using dnf, where supported
ansible.builtin.dnf:
name: httpd
state: present
when: ansible_facts['distribution'] in supported_distros
when is not a module variable, so it has to sit outside the module. Indent it under ansible.builtin.dnf: and it becomes an argument passed to dnf, which rejects it as an unsupported parameter. By convention, keywords such as when go after the name and the module block.
Go deeper:
Playbook keywords: Task (official docs) โ every key that may appear at task level.