Include and import tasks

Posted by zahidraf on Fri, 31 Dec 2021 22:28:11 +0100

Include and import files

If the playbook is long or complex, we can divide it into smaller files for easy management. Multiple playbooks can be combined into one main playbook in a modular way, or the task list in the file can be inserted into the play. This makes it easier to reuse play or task sequences in different projects.

Include or import files

Ansible can use two operations to bring content into the playbook. You can include content or import content.

Inclusion is a dynamic operation. During playbook operation, Ansible processes the content when it arrives.

Importing content is a static operation. Before the run starts, Ansible preprocesses the imported content when it initially parses the playbook.

Import playbook

​ import_ The playbook directive allows an external file containing a play list to be imported into playbook. In other words, one or more additional playbooks can be imported into the main playbook.

Since the imported content is a complete playbook, import_ The playbook function can only be used at the top level of playbook, not in play. If you import multiple playbooks, they are imported and run sequentially.

A simple example of importing a primary playbook with two additional playbooks is as follows:

[root@localhost ansible]# tree /etc/ansible/
/etc/ansible/
├── installed.yml   # Install playbook for httpd
├── main.yml        # playbook that combines two playbooks
├── started.yml     # Start the playbook of httpd

[root@localhost ansible]# cat installed.yml 
---
- hosts: 192.168.220.8
  tasks:
    - name: installed httpd
      yum: 
        name: httpd
        state: latest

[root@localhost ansible]# cat started.yml 
---
- hosts: 192.168.220.8
  tasks:
    - name: started httpd  
      service: 
        name: httpd
        state: started

[root@localhost ansible]# cat main.yml 
- name: install
  import_playbook: installed.yml
  
- name: start
  import_playbook: started.yml

You can also use the imported playbook to play alternately in the main playbook.

- name: Play 1
  hosts: localhost
  tasks:
    - debug:
      msg: Play 1
      
- name: Import Playbook
  import_playbook: play2.yml

In this example, Play 1 runs first and then from play2 Play imported from ymlplaybook.

Import and include tasks

Import task

You can import or include the task list in the task file in play. A task file is a file that contains a list of task planes

You can use import_ The tasks function statically imports the task file into play in the playbook. When importing a task file, the tasks in the file will be inserted directly when the playbook is parsed. Import in Playbook_ The location of tasks controls where tasks are inserted and the order in which multiple imports are run.

[root@localhost ansible]# tree /etc/ansible/
/etc/ansible/
├── installed.yml   # Tasks for installing httpd
├── main.yml        # playbook combining two tasks
├── started.yml     # Task to start httpd

[root@localhost ansible]# cat installed.yml 
- name: installed httpd
  yum: 
    name: httpd
    state: latest
    
[root@localhost ansible]# cat started.yml 
- name: started httpd  
  service: 
    name: httpd
    state: started
    
[root@localhost ansible]# cat main.yml # Executing the playbook will import the above two tasks
---
- hosts: 192.168.220.8
  tasks:
    - import_tasks: installed.yml
    - import_tasks: started.yml

When importing a task file, the tasks in the file will be inserted directly when the playbook is parsed. Due to import_tasks statically import tasks when parsing playbook, so it has some impact on its working mode.

  • Using import_ When using the tasks function, conditional statements such as when set during import will be applied to each imported task
  • Cannot use loop for import_tasks function
  • If you use variables to specify the name of the file to import, you cannot use host or group manifest variables

Include task files

You can use include_ The tasks function dynamically imports task files into play in the playbook, which is not different from importing tasks.

[root@localhost ansible]# cat installed.yml 
- name: installed httpd
  yum: 
    name: httpd
    state: latest
    
[root@localhost ansible]# cat started.yml 
- name: started httpd  
  service: 
    name: httpd
    state: started

[root@localhost ansible]# cat main.yml  # Executing the playbook will include the above two tasks
---
- hosts: 192.168.220.8
  tasks:
    - include_tasks: installed.yml  # Include tasks
    - include_tasks: started.yml
    
[root@localhost ansible]# ansible-playbook main.yml 
.....
TASK [include_tasks] ***********************************************************
included: /etc/ansible/installed.yml for 192.168.220.8                 # When executed, it displays which host the task applies to

TASK [installed httpd] *********************************************************
ok: [192.168.220.8]

TASK [include_tasks] ***********************************************************
included: /etc/ansible/started.yml for 192.168.220.8                   # When executed, it displays which host the task applies to

TASK [started httpd] ***********************************************************
ok: [192.168.220.8]
.....

Before the play runs and this part of the play arrives, include_ The tasks feature does not handle the content in the playbook. The order in which the playbook content is processed affects how the included task functionality works.

  • Use include_ When using the tasks function, conditional statements such as when set during inclusion will determine whether the task is included in play

  • If you run ansible playbook -- List tasks to list the tasks in the playbook, the tasks in the included task file will not be displayed. The task that contains the task file is displayed. In contrast, import_ The tasks function does not list the tasks imported into the task file, but lists the tasks in the imported task file

    [root@localhost ansible]# cat main.yml  # Import tasks and include tasks
    ---
    - hosts: 192.168.220.8
      tasks:
        - import_tasks: installed.yml  # Import task
        - include_tasks: started.yml   # Include tasks
    
    [root@localhost ansible]# ansible-playbook main.yml --list-tasks   # The difference between importing tasks and including tasks using the command view
    
    playbook: main.yml
    
      play #1 (192.168.220.8): 192.168.220.8        TAGS: []
        tasks:
          installed httpd   TAGS: []   # Import task displays the specific task to be executed
          include_tasks     TAGS: []   # Including tasks does not display the specific tasks to be executed
    
  • You cannot use ansible playbook -- start at task to start a playbook from a task already contained in a task file

  • You cannot use the notify statement to trigger a handler name in an included task file. The handler can be triggered in the main playbook that contains the entire task file, in which case all tasks in the included file will run

Use cases for task files

Please refer to the following examples. It may be helpful to manage task groups as an external file independent of playbook in these scenarios:

  • If the new server needs to be fully configured, the administrator can create different task sets It is used to create users, install software packages, configure services, configure privileges, set access rights to shared file systems, strengthen servers, install security updates, and install monitoring agents. Each task set can be managed through a separate self-contained task file
  • If the server is managed by developers, system administrators and database administrators, each organization can write its own task file, which can be reviewed and integrated by the system manager
  • If the server requires a specific configuration, it can be consolidated into a set of tasks performed according to a certain condition. In other words, tasks are included only when certain criteria are met
  • If a group of servers needs to run a task / group, it / they can only run on servers belonging to a specific host group

Manage task files

To facilitate management, you can create a directory dedicated to task files and save all task files in this directory. PlayBook can then include or import task files from this directory. In this way, you can build a complex playbook while simplifying its structure and component management.

Define variables for external play and tasks

Using Ansible's import and include functions to merge play or tasks in external files into playbook greatly enhances the ability to reuse tasks and playbook in Ansible environment. In order to maximize the possibility of reuse, these tasks and play files should be as common as possible. Variables can be used to parameterize play and task elements to expand the application scope of tasks and play.

For example, the following task file installs the software packages required for Web services, and then enables and starts the necessary services.

---
- name: Install the httpd package
  yum:
    name: httpd
    state: latest
    
- name: Start the httpd service
  service:
    name: httpd
    enabled: True
    state: started

If the software package and service elements are parameterized as shown in the following example, the task file can also be used to install and manage other software and its services, not just Web services.

[root@localhost ansible]# cat installed.yml  # Install service task
- name: installed packages
  yum: 
    name: "{{ packages }}"
    state: latest
    
[root@localhost ansible]# cat started.yml  # Install service task
- name: service packages
  service: 
    name: "{{ packages }}"
    state: started

Then, when merging the task file into a playbook, define the variables used to execute the task, as shown below:

[root@localhost ansible]# cat main.yml 
---
- hosts: 192.168.220.8
  tasks:
    - name: test
      import_tasks: installed.yml     # Import installation services task
      import_tasks: started.yml       # Import start service task
      vars:
         packages: httpd             # variable

Ansible makes the passed variables available for tasks imported from external files.

Use the same technology to make play files more reusable. When the play file is merged into the playbook, variables are passed to execute the play, as follows:

...output omitted...
- name: Import play file and set the variable
  import_playbook: play.yml
  vars:
    package: mariadb

Topics: Linux