roles in ansible
Roles can automatically load variable files, tasks and handlers according to hierarchical structure. Simply put, roles include variables, files, tasks, modules and processors in separate directories, and can be easily included. roles are generally used in host-based service building scenarios, but can also be used to build daemons and other scenarios.
roles/
common/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
web/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
Interpretation of the meanings of directories in roles
Files: Used to store files invoked by the copy module or script module.
templates: used to store jinjia2 template, template module will automatically find jinjia2 template files in this directory.
Tasks: This directory should contain a main.yml file that defines the list of tasks for this role. This file can use include
Contains other task files located in this directory. Default executor for mail.yml
handlers: This directory should contain a main.yml file that defines the actions to be performed when triggering conditions in this role.
vars: This directory should contain a main.yml file that defines the variables used by this role.
defaults: This directory should contain a main.yml file to set default variables for the current role.
meta: This directory should contain a main.yml file that defines the special settings and dependencies of this role.
Steps to use roles in a playbook:
1)Create with roles Directory of commands.
mkdir /etc/ansible/roles/ -p #yum is installed by default
2)Create a global variable directory.
mkdir /etc/ansible/group_vars/ -p
touch /etc/ansible/group_vars/all #File names are defined by themselves. Note when referencing
3)stay roles Create directories with roles name commands separately in the directory, such as httpd.
mkdir /etc/ansible/roles/common -p
4)Create separately in the directory of each role command files,handlers,tasks,templates,meta,defaults and vars A directory that is not used can be created as an empty directory, but can not be created without it.
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
5)In each role handlers,tasks,meta,defaults,vars Create under the directory main.yml Documents must not be customized.
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
6)stay playbook In the file, the roles are invoked.
vi /etc/ansible/site.yml
---
- hosts: webserver
remote_user: root
roles:
- httpd
- mysql
Experimental case: Building LAMP architecture with roles
Create httpd, mysql, php role name directories, and create files, handlers, tasks, templates, meta, defaults, and vars directories under their directories
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml
Writing httpd module
Write a simple task / main. YML
vi /etc/ansible/roles/httpd/tasks/main.yml
- name: ensure apache is at the late st version
yum: pkg={{ pkg }} state=latest
Definition variables: They can be defined in global variables, roles role variables, and generally role variables.
vi /etc/ansible/roles/httpd/vars/main.yml
pkg: httpd
Writing mysql module
vi /etc/ansible/roles/mysql/tasks/main.yml
- name: ensure mysql is at the latest version
yum: pkg={{ pkg }} state=latest
vi /etc/ansible/roles/mysql/vars/main.yml
pkg: mariadb*
Write php module
vi /etc/ansible/roles/php/tasks/main.yml
- name: ensure php is at the latest version
yum: pkg={{ pkg }} state=latest
vi /etc/ansible/roles/php/vars/main.yml
pkg: php
Write roles examples
vi /etc/ansible/lamp.yml
---
- hosts: webserver
remote_user: root
roles:
- httpd
- mysql
- php
Implementation of playbook
ansible-playbook lamp.yml
Write php test home page under httpd site directory on Web server and access it through browser
Conclusion:
Using the roles function of ansible to decouple all tightly connected services can reduce software coupling. It greatly simplifies the deployment and implementation of software projects, facilitates later maintenance, and is conducive to the promotion and use of the project.
Decoupling application is a major trend nowadays, which can be well realized by roles.