ansible manages the inclusion and import files of large projects

Posted by JoeyT2007 on Mon, 03 Jan 2022 07:11:30 +0100

Manage large playbook s

If the playbook is complex, we can divide it into smaller files (that is, decouple) 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. To facilitate simpler management.

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_playbook: used to import one or more additional playbooks into the main 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.

If you want to import to the main playbook, you should write the import file at the bottom.
If you combine the playbook s, you can do it in order.

Import and include tasks

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:

Import task file

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@ansible playbook]# tree 
.
├── 111.yml
├── ac.yml
├── files
│   ├── hosts.j2
│   └── test.j2
├── lamp.yml
├── mysql.yml
[root@ansible playbook]# cat 111.yml 
---
- name: yum mysql
  hosts: 192.168.216.172
  tasks:
    - name: yun install
      yum:
        name: mariadb*
        state: present 

    - include_tasks: mysql.yml
[root@ansible playbook]# cat mysql.yml 
- name: Starts the httpd service
  service:
    name: mariadb
    state: started
[root@ansible playbook]# ansible-playbook 111.yml 

PLAY [yum mysql] ****************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [yun install]   **************************************************************************************************************
ok: [192.168.216.172]

TASK [Starts the mariadb service] *************************************************************************************************
ok: [192.168.216.172]

PLAY RECAP **********************************************************************************************************************
192.168.216.172            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


  1. Using import_ When using the tasks function, conditional statements such as when set during import will be applied to each imported task

  2. Cannot use loop for import_tasks function

  3. 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.

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.

[root@ansible playbook]# ansible-playbook 111.yml 

PLAY [yum mysql] ****************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [yun install] **************************************************************************************************************
changed: [192.168.216.172]

TASK [include_tasks] # Detailed name************************************************************************************************************
included: /etc/ansible/playbook/mysql.yml for 192.168.216.172

TASK [Starts the mariadb service] *************************************************************************************************
changed: [192.168.216.172]

PLAY RECAP **********************************************************************************************************************
192.168.216.172            : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  • 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 specific tasks contained will not be displayed. In contrast, import_ The tasks function lists the tasks in the imported task file
  • 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

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

If the variable is general, it is written at the top level. If it is only for a reference or contains, it is written under the targeted module

Topics: Linux ansible