LOGBOOK

HELP

Quiz Entry - updated: 2026.11.26

Why can't the file name of an import_tasks come from a host or group variable in the inventory?

Imports are resolved while the playbook is parsed, before any host is being worked on, so per-host inventory variables aren't available yet. An include resolves its file name at run time, per host, so it can use them.

Inventory variables belong to individual hosts: web1 may have os_tasks: rhel.yml and web2 may have os_tasks: debian.yml. During parsing, Ansible builds a single task list for the play, not one per host, so it can't know which host's value to use.

A variable in an import's file name works only if its value is known at parse time, for example from --extra-vars or a vars: entry on the play or the import. To pick a different file per host, use an include:

- name: Load the tasks for this host's OS
  ansible.builtin.include_tasks: "{{ ansible_facts['os_family'] | lower }}.yml"

The include is evaluated separately for each host while the play runs, so each host can load a different file. (Facts like os_family exist only after fact gathering at run time, which is another reason this works only with an include.)

Go deeper:

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