TExcelTAD
Home › Configuration Management: Ansible › Imports & Includes

Imports & Includes

A 600-line playbook is hard to review. Split it into files and pull them in, either statically (import_*) or dynamically (include_*).

import_tasks, import_playbook, import_role include_tasks, include_role, include_vars
when it is read at parse time, before the play starts at run time, when the task is reached
file name from a variable / fact only from variables known at parse time yes, any variable
loop: not allowed allowed
--list-tasks, --list-tags shows the imported tasks shows only the include task
--start-at-task into the file works doesn't find tasks inside
tags/when on it copied to every task inside apply to the include itself only

Rule of thumb: import by default (predictable, visible to --list-tasks); include when the file name or the number of repetitions is only known at run time.

Both in one playbook

bash · try it yourself
mkdir -p tasks
cat > tasks/common.yml <<'YML'
- name: Common baseline
  ansible.builtin.debug:
    msg: baseline
YML
cat > tasks/os-Debian.yml <<'YML'
- name: Debian specifics
  ansible.builtin.debug:
    msg: "apt-based setup for {{ os }}"
YML
cat > tasks/os-RedHat.yml <<'YML'
- name: Red Hat specifics
  ansible.builtin.debug:
    msg: "dnf-based setup for {{ os }}"
YML
cat > tasks/vhost.yml <<'YML'
- name: Virtual host
  ansible.builtin.debug:
    msg: "vhost {{ site.name }} on port {{ site.port }}"
YML
cat > site.yml <<'YML'
- name: Imports and includes
  hosts: localhost
  gather_facts: false
  vars:
    os: Debian
    sites:
      - {name: shop, port: 8080}
      - {name: blog, port: 8081}
  tasks:
    - name: Static import
      ansible.builtin.import_tasks: tasks/common.yml

    - name: Dynamic file name
      ansible.builtin.include_tasks: "tasks/os-{{ os }}.yml"

    - name: Include in a loop
      ansible.builtin.include_tasks: tasks/vhost.yml
      loop: "{{ sites }}"
      loop_control:
        loop_var: site
YML
echo "=== --list-tasks"
ansible-playbook site.yml --list-tasks | sed -n '/tasks:/,$p'
echo "=== run"
ansible-playbook site.yml | grep -E '^(included|TASK)|"msg"'
=== --list-tasks
    tasks:
      Common baseline	TAGS: []
      Dynamic file name	TAGS: []
      Include in a loop	TAGS: []
=== run
TASK [Common baseline] *********************************************************
    "msg": "baseline"
TASK [Dynamic file name] *******************************************************
included: /home/learner/tasks/os-Debian.yml for localhost
TASK [Debian specifics] ********************************************************
    "msg": "apt-based setup for Debian"
TASK [Include in a loop] *******************************************************
included: /home/learner/tasks/vhost.yml for localhost => (item={'name': 'shop', 'port': 8080})
included: /home/learner/tasks/vhost.yml for localhost => (item={'name': 'blog', 'port': 8081})
TASK [Virtual host] ************************************************************
    "msg": "vhost shop on port 8080"
TASK [Virtual host] ************************************************************
    "msg": "vhost blog on port 8081"

--list-tasks shows the imported Common baseline task, but for the includes it only knows the include tasks themselves; what they contain is decided while the play runs. loop_var: site renames item, which avoids clashes when the included file has loops of its own.

import_playbook: a site playbook made of parts

A common top-level layout:

yaml
# site.yml
- name: Base config for every server
  ansible.builtin.import_playbook: base.yml
- name: Web tier
  ansible.builtin.import_playbook: web.yml
- name: Database tier
  ansible.builtin.import_playbook: db.yml

ansible-playbook site.yml runs everything; ansible-playbook web.yml runs one tier. import_playbook can only appear at the top level of a playbook, between plays.

when: copied vs evaluated once

The difference in how when applies bites when a task inside changes the variable in the condition:

bash · try it yourself
mkdir -p tasks
cat > tasks/setup.yml <<'YML'
- name: Mark as done
  ansible.builtin.set_fact:
    setup_done: true
- name: Second step
  ansible.builtin.debug:
    msg: second step ran
YML
cat > when.yml <<'YML'
- name: when on import vs include
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Import (when copied to each task)
      ansible.builtin.import_tasks: tasks/setup.yml
      when: not (setup_done | default(false))

    - name: Reset
      ansible.builtin.set_fact:
        setup_done: false

    - name: Include (when checked once)
      ansible.builtin.include_tasks: tasks/setup.yml
      when: not (setup_done | default(false))
YML
ansible-playbook when.yml | grep -E '^(TASK|ok|skipping|included)'
TASK [Mark as done] ************************************************************
ok: [localhost]
TASK [Second step] *************************************************************
skipping: [localhost]
TASK [Reset] *******************************************************************
ok: [localhost]
TASK [Include (when checked once)] *********************************************
included: /home/learner/tasks/setup.yml for localhost
TASK [Mark as done] ************************************************************
ok: [localhost]
TASK [Second step] *************************************************************
ok: [localhost] => {

With the import, Second step was skipped: after Mark as done, the copied condition was false. With the include, the condition was checked once, so both tasks ran.

include_vars

include_vars loads a variables file at run time, typically chosen by facts:

yaml
- name: OS-specific variables
  ansible.builtin.include_vars: "{{ item }}"
  with_first_found:
    - "vars/{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_major_version'] }}.yml"
    - "vars/{{ ansible_facts['os_family'] }}.yml"
    - vars/default.yml

Test yourself

You want to repeat a set of tasks for each item in a list. Which do you use?

  1. import_tasks with loop
  2. include_tasks with loop
  3. import_playbook
  4. roles:

Why might --list-tasks not show tasks from a file?

  1. The file is empty
  2. It is pulled in with include_tasks, which is resolved only at run time
  3. Tags hide it
  4. It uses FQCNs

when: on an import_tasks is…

  1. evaluated once
  2. copied to every imported task and evaluated for each
  3. ignored
  4. an error