ansible role management

Use of roles in ansible

Introduction to roles

Role is a new feature introduced by ANSI ble from version 1.2, which 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.

Explanation of the contents in roles

Files: used to store files called by copy module or script module.
templates: used to store the jinjia2 template. The template module will automatically find the jinjia2 template file in this directory.
tasks: this directory should contain a main.yml file to define the task list of this role. This file can use include to contain other task files located in this directory.
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 to define 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 to define the special settings and dependencies of this role.

To use roles in a playbook

1) Create a directory with the roles command.

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     #The file name is defined by itself. Note when referencing

3) In the roles directory, create a directory with each role name command, such as httpd.

mkdir /etc/ansible/roles/common -p

4) Create the files, handlers, tasks, templates, meta, defaults, and vars directories in the directories of each role command. You can create empty directories if you don't use them, but you can't leave them alone.

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) Create the main.yml file in the handlers, tasks, meta, defaults, vars directory of each role. It 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) In the playbook file, call each role.

vi /etc/ansible/site.yml
- hosts: webserver
  remote_user: root
  roles:
     - httpd
     - mysql

Experiment: install and build LAMP architecture

1. Create directory

[root@localhost ~]# cd /etc/ansible/roles/
[root@localhost roles]# ls
[root@localhost roles]# mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@localhost roles]# mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@localhost roles]# mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@localhost roles]# ls
httpd  mysql  php

[root@localhost roles]# ls httpd/
defaults  files  handlers  meta  tasks  templates  vars
[root@localhost roles]# ls mysql/
defaults  files  handlers  meta  tasks  templates  vars
[root@localhost roles]# ls php/
defaults  files  handlers  meta  tasks  templates  vars

2. Create an empty yml file

[root@localhost roles]# touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
[root@localhost roles]# touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
[root@localhost roles]# touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml

[root@localhost roles]# ls httpd/
defaults  files  handlers  meta  tasks  templates  vars
[root@localhost roles]# ls httpd/defaults/
main.yml
[root@localhost roles]# ls mysql/defaults/
main.yml
[root@localhost roles]# ls php/defaults/
main.yml

Write httpd module

[root@localhost roles]# vim /etc/ansible/roles/httpd/tasks/main.yml

- name: ensure apache is at the latest version 
  yum: pkg={{ pkg }} state=latest

Define variable: it can be defined in global variable or role variable, generally in role variable

[root@localhost roles]# vim /etc/ansible/roles/httpd/vars/main.yml

pkg: httpd

Writing mysql module

[root@localhost roles]# vim /etc/ansible/roles/mysql/tasks/main.yml

- name: ensure mysql is at the latest version 
  yum: pkg={{ pkg }} state=latest

Defining variables

vim /etc/ansible/roles/mysql/vars/main.yml

pkg: mariadb*

Writing php module

[root@localhost roles]# vim /etc/ansible/roles/php/tasks/main.yml

- name: ensure php is at the latest version
  yum: pkg={{pkg}} state=latest

Defining variables

[root@localhost roles]# vim /etc/ansible/roles/php/vars/main.yml

pkg: php*

Write a role example

[root@localhost roles]# vim /etc/ansible/site.yml

- hosts: webserver
  remote_user: root
  roles:
   - httpd
   - mysql
   - php

Execution script

[root@localhost ansible]# ansible-playbook site.yml 

PLAY [webserver] ***************************************************************

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

TASK [httpd : ensure apache is at the latest version] **************************
ok: [192.168.35.101]

TASK [mysql : ensure mysql is at the latest version] ***************************
changed: [192.168.35.101]

TASK [php : ensure php is at the latest version] *******************************
changed: [192.168.35.101]

PLAY RECAP *********************************************************************
192.168.35.101             : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

After execution, you should see the installed services on the remote host

[root@localhost ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64
[root@localhost ~]# rpm -q mariadb
mariadb-5.5.64-1.el7.x86_64
[root@localhost ~]# rpm -q php
php-5.4.16-46.1.el7_7.x86_64

189 original articles published, praised 63, visited 7290
Private letter follow

Tags: ansible MySQL PHP vim

Posted on Mon, 27 Jan 2020 06:27:46 -0500 by shadow1200