Ansible Role Management-Roles (Example Demo!!!)

Keywords: ansible MySQL PHP vim

An overview of Roles

Roles can automatically load variable files, task s, handlers, and so on, depending on the hierarchy.
Simply put, Roles is about including variables, files, tasks, modules, and processors in separate directories and easily.
Roles is typically used in scenarios where services are built on hosts, but it can also be used in scenarios such as building daemons.

Directory of Roles

Directory Name Specific meaning
files Used to store files called by copy or script modules
templates To store the j regular template, the template module automatically looks for the regular template file in this directory
tasks This directory should contain a main.yml file that defines the task list for this role. This file can use include to include 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 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 their dependencies for this role

Steps to use Roles in playbook

  • Create a directory with roles command
#yum is ready by default
mkdir /etc/ansible/roles/ -p 
  • Create global variable directory
mkdir /etc/ansible/group_vars/ -p
touch /etc/ansible/group_vars/all
#File name defined by itself, note when referencing
  • Create directories in the roles directory with commands by role name, such as httpd
mkdir /etc/ansible/roles/common -p
  • Create files, handlers, tasks, templates, meta, defaults, and vars directories in the directory of each role command. Unused directories can be created as empty directories, but not without creating them
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
  • Create a main.yml file in the handlers, tasks, meta, defaults, vars directory for each role, and never customize it
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
  • In the playbook file, invoke roles
vim /etc/ansible/site.yml

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

Installation and Setup of LAMP Architecture Instance Demo

  • Create Service Component Working Directory
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
  • Create an empty yml file
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 modules
#Write a simple tasks/main.yml
vim /etc/ansible/roles/httpd/tasks/main.yml

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

#Define variables, either in global variables or roles role variables, generally in role variables
vim /etc/ansible/roles/httpd/vars/main.yml

pkg: httpd
  • Writing mysql module
vim /etc/ansible/roles/mysql/tasks/main.yml

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

#Define Variables
vim /etc/ansible/roles/mysql/vars/main.yml

pkg: mariadb*
  • Writing php modules
vim /etc/ansible/roles/php/tasks/main.yml

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

#Define Variables
vim /etc/ansible/roles/php/vars/main.yml

pkg: php*
  • Write roles instances
vim /etc/ansible/site.yml

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

#Execute the script
ansible-playbook site.yml 

On the remote host after execution, query to verify whether the specified service is installed

rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64
rpm -q mariadb
mariadb-5.5.64-1.el7.x86_64
rpm -q php
php-5.4.16-46.1.el7_7.x86_64

Thank you for reading!

Posted by hemoglobina on Tue, 04 Feb 2020 15:13:50 -0800