Using The Ansible Include & Import Module

In this lesson, you will learn how to use the include and import module such as include_tasks, import_tasks, import_playbook

UNDERSTANDING THE SUBJECT MATTER

Just as I mentioned in one of the previous lessons, a playbook, which is your script can also be your documentation provided you write it in an easy-to-understand way.

Troubleshooting also becomes easy when a playbook is nicely written and one of the ways to nicely write a playbook is not to put so many files in a playbook, especially files that can be referenced outside of a playbook, this makes troubleshooting easy.

It is understandable that so many tasks, many times needs to run, especially in a large environment which can elongate a playbook due to the numerous tasks and plays defined in a playbook. Well, there is good news.

The good news is that you can choose to have a primary playbook and/or tasks, and other secondary playbooks and/or tasks. Other secondary playbooks or tasks can be included or imported into the primary playbook without having to define them in the primary playbook.

Related tasks can be stored in one file, and can be imported or included in a playbook

Importing Ansible Playbook

With the import_playbook directive, one or more playbooks can be imported. The import_playbook directive imports a playbook and not a play, hence, the directive cannot be defined inside of a play but as a playbook itself. It can be immediately after a playbook or before a playbook as defined below.

- name: Install Apache httpd
  import_playbook: install_httpd.yml

- name: start and enable Apache httpd
  import_playbook: start_httpd.yml

- name: start and enable firewall
  hosts: group1
  tasks:
     - name: start and enable firewall
       service:
       name: firewalld
       enabled: true
       state: started
 
- name: allow apache in firewall
  import_playbook: allow_httpd.yml

Importing and Including Ansible Tasks

Tasks can be imported or included from another flat file into a primary playbook. The tasks file will of course be written in YAML. An example of a task file is shown below

[lisa@drdev1 ~]$ vim import_task1.yml
 - name: start and enable firewall services
   service:
     name: firewalld
     enabled: true
     state: started

- name: Install nginx
  yum:
  name: nginx
  state: latest
    

import tasks are always being pre-processed with a playbook and parsed at the beginning of a playbook, hence, if there is any mistake in the task file that is imported in a playbook, the playbook will immediately throw an error at the run time start and will not run.

On the other hand, include tasks will be parsed when Ansible gets to the/that task line. Ansible only gets to know if there is an error or mistake in the flat file when it gets to the task line.

To see the different Ansible modules that can do import and include, you can do a search when you list the Ansible modules available on your system.

[lisa@drdev1 ~]$ ansible-doc -l

To import a task in a playbook, the keyword, “import_tasks” will be used. Similarly, to include a task in a playbook, the keyword, “include_tasks” will be used.

Let’s see the step by step guide of how a playbook and tasks can be imported and included into another playbook in the “ACTION TIME” section below.

ACTION TIME

Step By Step Guide Of How To Create a Playbook Using The import and include statement

To create a playbook that will install httpd by importing another playbook, start and enable httpd by including tasks, start and enable firewall by importing a task, allow httpd in a firewall rule by including a task, and install autofs by importing another playbook, the following steps can be taken.

1. Create a task file to install httpd

[lisa@drdev1 ~]$ vim install_httpd.yml
- name: Install httpd
  yum:
     name: httpd
     state: present

2. create a task file to start httpd.

[lisa@drdev1 ~]$ vim start_httpd.yml
 - name: start and enable {{ package }} service
   service:
     name: "{{ package }}"
     enabled: true
     state: started

3. create a task file to enable firewall.

[lisa@drdev1 ~]$ vim enable_firewall.yml
- name: start and enable firewall services
  service:
     name: firewalld
     enabled: true
     state: started

4. create a task file to allow httpd in firewall.

[lisa@drdev1 ~]$ vim allow_httpd_firewall.yml
- name: allow apache in firewall
  firewalld:
     service: http
     state: enabled
     permanent: true
     immediate: true

5. create a playbook to install autofs

[lisa@drdev1 ~]$ vim playbook7.yml
- name: Install basic package
  hosts: hqdev1.tekneed.com
  tasks:
     - name: install autofs
       yum:
         name: autofs
         state: present
       ignore_errors: true

6. create a primary playbook to import and include the task files and playbook created above.

[lisa@drdev1 ~]$ vim playbook18.yml
- name: Web services deployment
  hosts: hqdev1.tekneed.com
  tasks:
    - name: Install apache httpd
      import_tasks: install_httpd.yml

    - name: start and enable httpd service
      include_tasks: start_httpd.yml
      vars:
        package: httpd
      when: ansible_facts[ 'os_family' ] == 'RedHat'

    - name: start and enable firewall services
      import_tasks: enable_firewall.yml

    - name: allow apache in firewall
      include_tasks: allow_httpd_firewall.yml

- name: install autofs package
  import_playbook: playbook7.yml

7. Run the playbook

[lisa@drdev1 ~]$ ansible-playbook playbook18.yml

PLAY [Web services deployment] ************************************************************
TASK [Gathering Facts]
.............
import module

Click Here To Watch Video On Using The Ansible Include & Import Module

RHCE EX294 Exam Practice Question On Using The Ansible Include & Import Module

Ansible Automation Video

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.


*