Quiz Entry - updated: 2026.11.26
Why can't you put a loop: on import_tasks, and what do you use instead?
An import is expanded once while the playbook is parsed, before any loop could be evaluated, so it can't be repeated per item. Use include_tasks with loop:, which runs the whole file once per item.
A loop is evaluated when a task runs: Ansible works out the list for the current host and repeats the task for each item. An import never runs as a task, because it was already replaced by its tasks during parsing. There is no import task left that could be repeated.
An include is a real task at run time, so it can loop:
- name: Create each user with its own task file run
ansible.builtin.include_tasks: create_user.yml
loop:
- alice
- bob
Each pass runs all tasks of create_user.yml with item set to the current entry. Looping is the main reason to choose an include.
Go deeper:
Stacking loops via include_tasks (official docs) — looping over a task file, and loop_var for nested loops.
ansible.builtin.include_tasks module (official docs) — the module reference, including the file and apply options.