Ansible Include_Tasks

Ansible is an open-source tool for configuration management, application deployment, task automation, and more. Ansible offers an easy-to-configure format using a human-readable declarative language (YAML) to define the automation tasks that are known as playbooks.

One way that Ansible can automate various tasks is using modules. Modules are singular entities that comprise of single tasks that can comprehensively encompass a given task subset. For example, the apt module is designed to handle the package management in Debian-based distributions.

In this tutorial, we will learn about the include_task directive that dynamically allows us to load the tasks from other playbooks. This feature enables us to break down the complex playbooks into more manageable units that are easier to read, write, debug, reuse, and maintain.

Ansible.builtin.include_tasks Module

The include_tasks module is part of the ansible-core collection that is available in all Ansible installations.

It allows us to include a file with a list of the tasks to be executed in the current playbook.

The following shows the syntax of the include_tasks directive:

    file: <filename>
        - tag

The directive accepts the following parameters:

File – It specifies the file with a list of tasks to be executed in the current playbook.

Apply – It specifies a hash of task keywords, such as tags, to be applied to the tasks within “include”.

Basic Usage:

Let us kick things off with a basic example. Suppose we have a set of tasks in a file called “install_packages.yml”. We can include these tasks in our main playbook as follows:

- hosts: all
    - name: Update repositories
        update_cache: yes
    - name: Include task to install packages
        file: install_packages.yml

In this case, we tell Ansible to load the tasks that are defined in the “install_packages.yml” playbook and run them in the current playbook.

We can have the “install_packages.yml” file that is defined as follows:

- name: Install Apache
    name: apache2
    state: present
- name: Install Nginx
    name: nginx
    state: present

Using the Include_Tasks with Variables

We can also use the variables with the include_tasks module. The variables allow us to dynamically control which tasks are included based on a different condition or input.

- hosts: all
    - name: Include task list in play only if the condition is true
      ansible.builtin.include_tasks: "{{ hostvar }}.yaml"
      when: hostvar is define

Including Multiple Task Files

Ansible also allows us to use the include_tasks in a loop to include multiple task files. Suppose we have separate files for installing Apache, Nginx, and MySQL. We can iterate over them as follows:

- hosts: nodes
    - name: Update repositories
        update_cache: yes
    - name: Include task to install software
      include_tasks: "{{ item }}"
        - install_apache.yml
        - install_nginx.yml
        - install_mysql.yml

In this case, Ansible iterates over each task that is defined in the files and execute them individually.


We explore the workings of the include_tasks directive in Ansible to dynamically load the tasks that are defined in other files. By now, you should have a solid understanding on how to break down the complex playbooks into manageable chunks and import them when necessary.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list