Detailed explanation of Ansible roles

roles role

Role is a new feature introduced by ansible since version 1.2. It is used to organize playbooks hierarchically and structurally. Roles can automatically load variable files, tasks and handlers according to the hierarchical structure. To use roles, you only need to use the include instruction in the playbook. In short, roles is a mechanism that can easily include variables, files, tasks, templates and processors by placing them in separate directories. Roles are generally used in scenarios of building services based on hosts, but they can also be used in scenarios such as building daemons

Complex operation and maintenance scenarios: roles is recommended, with high code reuse

Roles: a collection of multiple roles. Multiple roles can be placed in separate subdirectories under the roles directory
roles/
mysql/
httpd/
nginx/
redis/

Ansible Roles catalog choreography

The roles directory structure is as follows

Each role is organized in a specific hierarchical directory structure

roles directory structure:
playbook.yml
roles/
project/
tasks/
files/
vars/
templates/
handlers/
default/
meta/

Roles role of each directory
roles/project /: project name, with the following subdirectories

  • Files /: store files called by copy or script modules
  • templates /: the template module finds the directory of the required template files
  • tasks /: defines the basic elements of task and role, which should contain at least one element named main YML's documents; Other files need to be included in this file through include
  • handlers /: at least one handler named main should be included YML's documents; Other files need to be included in this file through include
  • vars /: defines a variable, which should contain at least one named main YML's documents; Other files need to be included in this file through include
  • meta /: defines the special settings and dependencies of the current role. It should include at least one named main YML files, and other files need to be included in this file through include
  • Default /: use main in this directory when setting default variables YML file, lower priority than vars

Create role

To create a role
(1) Create a directory named roles
(2) Create directories named after each role in the roles directory, such as web servers
(3) Create files, handlers, meta, tasks, templates and vars directories in the directory named by each role; Directories that are not used can be created as empty directories or not
(4) calling roles in the playbook file.

Orchestrate with Roles for large projects
Example: directory structure of roles

nginx-role.yml 
roles/
└── nginx 
     ├── files
     │    └── main.yml 
     ├── tasks
     │    ├── groupadd.yml 
     │    ├── install.yml 
     │    ├── main.yml 
     │    ├── restart.yml 
     │    └── useradd.yml 
     └── vars 
          └── main.yml 

playbook calling role

Call role method 1:

---
- hosts: websrvs
  remote_user: root
  roles:
    - mysql
    - memcached
    - nginx   

Call role method 2:

The key role is used to specify the role name, and the subsequent k/v is used to pass variables to the role

---
- hosts: all
  remote_user: root
  roles:
    - mysql
    - { role: nginx, username: nginx }

Call role method 3:

Role calls can also be implemented based on conditional tests

---
- hosts: all
  remote_user: root
  roles:
    - { role: nginx, username: nginx, when: ansible_distribution_major_version == '7'  }

tags in roles

#nginx-role.yml
---
- hosts: websrvs
  remote_user: root
  roles:
    - { role: nginx ,tags: [ 'nginx', 'web' ] ,when: ansible_distribution_major_version == "6" }
    - { role: httpd ,tags: [ 'httpd', 'web' ]  }
    - { role: mysql ,tags: [ 'mysql', 'db' ] }
    - { role: mariadb ,tags: [ 'mariadb', 'db' ] }

ansible-playbook --tags="nginx,httpd,mysql" nginx-role.yml

Reference link: http://www.yunweipai.com/34669.html

Keywords: Linux

Added by scbmx on Sun, 26 Dec 2021 14:01:46 +0200