How roles are organized

Introduction

Role is a collection of task files, variable files, and handlers files. The notable features of this collection are portability and repeatability.

In practice, we usually deploy a service as a unit as a role, and then put these service units (role) in a roles directory. The main playbook file implements various flexible deployment requirements by calling the roles in the roles directory.

Create roles

There are usually two ways to create a role:

  • command mkdir and touch line to create manually
  • Automatically initialize a role with ansible-galaxy

Of course I still use the existing ones.

For example, I want to use the "ansible-galaxy init" command to create a role named role_A, which can be written like this:

ansible-galaxy init role_A

The created directory structure is as follows:

➜ tree role_A
role_A
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml  directories,  files

The role automatically created by the "ansible-galaxy" command is the most complete directory structure, and unnecessary directory files can be deleted according to requirements.

Let's take the "role_A" created above as an example to introduce the role of each directory file:

  • tasks : It is used to store the main tasks of role_A, and other task files can also be added to be called by main.yaml to achieve more complex deployment functions.
  • handlers : Used to store tasks that trigger execution ( hanlders ).
  • defaults : Used to store default variables with the lowest priority. For variable priority, please refer to " Ansible Basics - Variables ".
  • vars : used to store variable files, variables used in tasks and templates in role_A can be defined here.
  • files : used to store files that need to be copied to the destination host, e.g. as the default root directory for the src parameter of the "copy" module.
  • template : used to store template files, the format is .j2, and the content of the file must conform to the Jinja2 grammar rules. Usually, the "template" module is used to deploy the configuration file of the service.
  • meta : used to store the role dependency list, this knowledge point will be explained in detail later.
  • tests : The playbook and host definition files used to test the function of the role itself are commonly used in the development and testing stages.

The main.yaml file in each directory in the role is very important. This is the YAML file loaded by ansible by default.


Role reference and execution

roles statement reference

More commonly, we can use the "roles:" statement to refer to roles:

---
- hosts: node1
  roles:
     - role_A

or

---
- hosts: node1
  roles:
     - name: role_A
     - name: role_A

or

---
- hosts: node1
  roles:
     - role: role_A
     - role: role_A

Or use absolute paths:

---
# playbooks/test.yaml
- hosts: node1
  roles:
    - role: /root/lab-ansible/roles/role_A

Add variable parameters while introducing:

---
# playbooks/test.yaml
- hosts: node1
  roles:
    - role: role_A
      vars:
        name: Maurice
        age: 

Add the tag parameter while introducing:

---
# playbooks/test.yaml
- hosts: node1
  roles:
    - role: role_B
      tags:
        - tag_one
        - tag_two
    # 等价于上面
    - { role: role_B, tags:['tag_one','tag_two'] }

According to the requirements, we refer to different roles in the playbook, and the effect after referencing is also well understood: Ansible will load the tasks, variables, handlers, dependencies, etc. contained in the role into the playbook, and execute them in sequence.

search path

The above describes the reference method using the "roles" statement, so where does Ansible find these roles?

Without using absolute paths, the default paths for ansible to retrieve roles are:

  • The current directory where the ansible-playbook command is executed
  • The directory where the playbook file is located and the roles directory of the directory where the playbook file is located
  • The ~/.ansible/roles directory under the current system user
  • /usr/share/ansible/roles directory
  • The directory specified by "roles_path" in ansible.cfg, the default value is /etc/ansible/roles directory

Note: The above retrieval path is the result of testing under the centos operating system.

Guess you like

Origin blog.csdn.net/qq_43762191/article/details/123752613