Ansible Detailed Explanation (12) - Ansible Roles Detailed Explanation

Today, I will continue to introduce Linux operation and maintenance related knowledge. The main content of this article is the detailed explanation of Ansible's Roles.

1. Introduction to Ansible Roles

Ansible has introduced new features of Roles since version 1.2 to organize Playbooks in a hierarchical and structured way. Compared with the control method of Ansible command, the control method of Ansible Playbook greatly improves the management ability of Ansible for the controlled device. However, if we want to configure a more complex host environment, then writing all configuration commands into a Playbook file can reach hundreds or thousands of lines. This is extremely detrimental to the management and maintenance of Ansible Playbooks.
Compared to Ansible Playbooks, Roles can automatically load variables, tasks, and handlers in a hierarchical structure. In essence, Ansible Roles is to put variables, tasks, templates and processors in different directories, call them by means of include, and combine them into a whole.

2. Ansible Roles directory structure

The Ansible Roles directory structure is generally as follows:
insert image description here

3. Ansible Roles Directory Explanation

Combined with the above figure, we can understand Ansible's directory structure in this way.
Ansible's Roles configurations are all stored in the /etc/ansible/roles/ directory. In this directory, there are many subfolders, and each subfolder is a role. Each Role should normally contain the following subfolders.
1. files/
is used to store the files of the copy module, or the script files of the script module.
2. tasks/
is used to store a series of task files. In this directory, there must be a main.yml file, which will call other files through include.
3. vars/
A series of variables are stored in this directory. In this directory, there must be a main.yml file, which will call other files through include. If the entire roles configuration variables are used less, only one main.yml file can be used, but if the entire roles configuration variables are more, then the variables can be classified and stored in different directories, and then passed through main.yml make a call.
4. meta/
This directory usually stores the special settings of this Role and its dependencies. In this directory, there must also be a main.yml file.
5. default/
This directory stores the default variables of this Role. In this directory, there must also be a main.yml file.
6. handlers/
This directory is used to store the actions executed after the trigger conditions in the Role. In this directory, there must also be a main.yml file.
7. The template/
directory is used to store the jinjia2 template files that this Role needs to use.
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200

Guess you like

Origin blog.csdn.net/weixin_40228200/article/details/123553489