Quiz Entry - updated: 2026.11.26
How do you use ansible.builtin.import_tasks, and what decides where the imported tasks run?
Put import_tasks: <file> in a play's task list. The tasks are inserted at that exact position during parsing, so the position of the import decides their place and order.
A task file holds only a list of tasks, without hosts: or tasks::
# webserver_tasks.yml
- name: Install the httpd package
ansible.builtin.dnf:
name: httpd
state: latest
- name: Start the httpd service
ansible.builtin.service:
name: httpd
state: started
The play imports it where those tasks should run:
- name: Install web server
hosts: webservers
tasks:
- name: Import webserver tasks
ansible.builtin.import_tasks: webserver_tasks.yml
Because the import is static, the play behaves exactly as if the two tasks had been typed in place of the import. With several imports, they run in the order they appear, each at its position among the play's other tasks.
Go deeper:
ansible.builtin.import_tasks module (official docs) — the module reference with examples.
Imports: static reuse (official docs) — why import file names need values known at parse time, and passing vars to imports.