Ansible - Include, File separation & Modularization

In any scripting language, we organize our code using classes, methods, variables, etc. and we reference them as required. In the same way, we can modularize the Ansible playbooks to use them efficiently and in an organized manner. We can store the repeated tasks or variables in some other file and reference them in our main playbook.

Let's consider the below playbook:

- name: this is our first play.
  hosts: webserver1
  vars:
    var1: var1
    var2: var2
    var3: var3
    var4: var4
    var5: var5
    var6: var6
    var7: var7
    var8: var8
    var9: var9
    var10: var10
  tasks:
    - name: "task 1"
      command: touch /tmp/large/{{ var1 }}.txt
    - name: "task 2"
      command: touch /tmp/large/{{ var2 }}.txt
    - name: "task 3"
      command: touch /tmp/large/{{ var3 }}.txt
    - name: "task 4"
      command: touch /tmp/large/{{ var4 }}.txt
    - name: "task 5"
      command: touch /tmp/large/{{ var5 }}.txt
    - name: "task 6"
      command: touch /tmp/large/{{ var6 }}.txt
    - name: "task 7"
      command: touch /tmp/large/{{ var7 }}.txt
    - name: "task 8"
      command: touch /tmp/large/{{ var8 }}.txt
    - name: "task 9"
      command: touch /tmp/large/{{ var9 }}.txt
    - name: "task 10"
      command: touch /tmp/large/{{ var10 }}.txt

In this playbook, there are nearly 10 variables and 10 tasks. This makes our playbook inefficient and unnecessarily huge.

We can store these vars in a separate file variable.yml as follows:

var1: var1
var2: var2
var3: var3
var4: var4
var5: var5
var6: var6
var7: var7
var8: var8
var9: var9
var10: var10

And, in the same way we can store the tasks also in different file tasks.yml:

- name: "task 1"
  command: touch /tmp/task/{{ var1 }}.txt
- name: "task 2"
  command: touch /tmp/task/{{ var2 }}.txt
- name: "task 3"
  command: touch /tmp/task/{{ var3 }}.txt
- name: "task 4"
  command: touch /tmp/task/{{ var4 }}.txt
- name: "task 5"
  command: touch /tmp/task/{{ var5 }}.txt
- name: "task 6"
  command: touch /tmp/task/{{ var6 }}.txt
- name: "task 7"
  command: touch /tmp/task/{{ var7 }}.txt
- name: "task 8"
  command: touch /tmp/task/{{ var8 }}.txt
- name: "task 9"
  command: touch /tmp/task/{{ var9 }}.txt
- name: "task 10"
  command: touch /tmp/task/{{ var10 }}.txt

The new playbook references the variable.yml and tasks.yml file:

- name: this is our first play.
  hosts: webserver1
  vars_files:
    - variable.yml
  tasks:
    - include: task.yml

Running the playbook and verifying the results concurrently: ansible-loop-output

This layout gives you more flexibility for larger environments, as well as a total separation of inventory variables between different environments. The downside of using this is it is harder to maintain because there are more files.

**NOTE: **

INCLUDE - DEPRECATED

include action dealing with both plays and tasks, being both dynamic and static is being deprecated now. This module will be removed in version 2.8. As alternatives use include_tasks, import_playbook, import_tasks.