LOGBOOK

HELP

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.

A task as a dictionary: name, module and when are sibling keys; module arguments are nested one level deeper

* 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:

From Quiz: ITIA / Ansible Task Control: Loops, Conditionals, Handlers and Error Handling | Updated: Nov 12, 2026