LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

main.yml imports other_tasks.yml with when: x is not defined. The file first sets x with set_fact, then prints x with debug. x starts undefined. What happens, and how do you fix it?

set_fact runs and defines x, then debug checks the same condition, now finds x defined, and is skipped. Use include_tasks instead, so the condition is checked only once, at the include.

With import_tasks the condition is copied onto both tasks: set_fact runs and defines x, so debug's own copy of the condition is false and it is skipped. With include_tasks the condition is checked once, at the include, and both tasks run

* An import checks the condition again on every task. An include checks it once. *

After parsing, the import turns into two tasks that both carry when: x is not defined:

  1. set_fact: x: foo checks the condition: x is undefined, so it runs and defines x.
  2. debug: var: x checks the condition again: x is now defined, so the condition is false and the task is skipped.

The intention was "if x was not set, set it and show it", but only half of it happens. With include_tasks, the condition belongs to the include task alone: it is checked once, before the file is read, and if it is true all tasks in the file run normally, without any condition of their own.

Tip: if a task inside the file changes what the condition tests, use an include.

Go deeper:

From Quiz: ITIA / Ansible: Complex Playbooks with Imports and Includes | Updated: Nov 26, 2026