[Ansible] – How to include tasks and variables file definitions?

It is possible to create files with tasks and variables definitions to reuse in many playbooks. To use them in the playbook scripts is required to include them as the following example.

- name: Example of playbook with include tasks and variable files
  hosts: myexample
    - name: Include the variable file
      include_vars: vars/vars.yml

    - name: Include the tasks
      include: tasks/tasks.yml

    - name: Install the firewall
        name: "{{ firewall }}"
        state: latest
# It is the vars/vars.yml file
firewall: firewalld
path: /var/www/html/
# It is the tasks/tasks.yml file
# All tasks defined here will be executed when it is include
- name: Create index.html
    content: "here is the content\n"
    dest:"{{ path }}index.html"

Following the tree of this example.

$ tree
├── ansible.cfg
├── inventory
├── myplaybook.yml
├── tasks
│   └── tasks.yml
└── vars
    └── vars.yml

For further information check the Ansible documentation Load variables from files, dynamically within a task and Sets attributes of files



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s