Ansible's roles introduction and actual combat

Source: Internet
Author: User

Roles is used for hierarchical, structured organization of playbook.
Roles can automatically load variable files, tasks, and handlers, depending on the hierarchical structure.
To use roles, you only need to use the include directive in playbook.
Simply put, roles is a mechanism by which variables (VARs), files (file), tasks, modules (modules), and processors (handlers) are placed in separate directories and easily include them.
Roles are typically used in scenarios based on host build services, but can also be used in scenarios such as building daemons.


Steps to create a roles

(1) Create a directory named after the roles;
(2) A directory named after each role name, such as Webservers, is created in the roles directory. Note: The roles must include the Site.yml file, which can be empty;
(3) The files, handlers, meta, tasks, templates, and VARs directories are created in each role-named directory, and directories that are not available can be created as empty directories or created;
(4) In the Playbook file, call each role;


Files available in each directory within the roles

Tasks directory: You should include at least one file named Main.yml that defines the task list for this role, which you can use include to include other task files that are located in this directory;
Files directory: A file that is called by a module such as copy or script;
Templates Directory: Template module will automatically look for JINJA2 templates files in this directory;
Handlers directory: This directory should contain a MAIN.YML file that defines the handler used for this role, and the other handler files that are included in the use of include in handler should also be located in this directory;
VARs Directory: You should include a main.yml file that defines the variables used by this role;
Meta Directory: A main.yml file should be included to define the special settings and dependencies of this role; Ansible 1.3 and later versions are supported;
Default directory: This directory is used when you set defaults for the current role, and should contain a main.yml file.


Example One,

Create three role, respectively, HTTP MySQL php, create the directory to use, generally created for all, can not be used to empty,

Test Host 1 Installation httpd

Test Host 2 Install MySQL

Test Host 3 install HTTP MySQL php

1.

Create a Directory

[Email protected] ansible_playbooks]# pwd/opt/ansible_playbooks[[email protected] ansible_playbooks]# lshosts[[ Email protected] ansible_playbooks]# MKDIR-PV Roles/{http,mysql,php}/{tasks,handlers,files,vars,templates,meta, Default

2. The test host is:

[email protected] ansible_playbooks]# cat hosts[http]192.168.100.131[mysql]192.168.100.132[lamp]192.168.100.130[[ Email protected] ansible_playbooks]#

3. Edit Role HTTP Task file

[Email protected] roles]# pwd/opt/ansible_playbooks/roles[[email protected] roles]# tree httphttp├──default├──files│  └──httpd.conf├──handlers│└──main.yml├──meta├──tasks│└──main.yml├──templates│└──httpd.conf└──vars└── MAIN.YML7 directories, 5 files[[email protected] roles]#

Tasks Task List

[email protected] roles]# cat http/tasks/main.yml-name:install httpd service yum:name=httpd State=present-name:sta RT httpd Service service:name=httpd state=started enabled=true-name:modify httpd config file from template template : src=httpd.conf dest=/etc/httpd/conf/httpd.conf Tags:-modifyhttpconf notify:-Restart httpd service[[email prote CTED] roles]#

Handlers

[email protected] roles]# cat http/handlers/main.yml-name:restart httpd service service:name=httpd state=restarted[[ Email protected] roles]#

Files and templates file differences are as follows, this is just to demonstrate the role of VARs

[[Email protected] roles]# diff http/files/httpd.conf http/templates/httpd.conf 136c136< Listen 8090---> Listen {{ listen.0}}:{{listen.1}}277c277<---> ServerName {{host_fqdn.0}}[[email protected] roles]#

VARs

[email protected] roles]# cat http/vars/main.yml Listen:-"{{ansible_all_ipv4_addresses.0}}"-8080host_fqdn:-"{{an Sible_nodename}} "[[Email protected] roles]#

Here hostname and address can actually use the setup variable directly, this is to demonstrate the use of VARs

4. Edit Role MySQL task file

[Email protected] roles]# pwd/opt/ansible_playbooks/roles[[email protected] roles]# tree mysqlmysql├──default├──  files│└──my.cnf├──handlers│└──main.yml├──meta├──tasks│└──main.yml├──templates│└──my.cnf└──vars└── MAIN.YML7 directories, 5 files[[email protected] roles]#


Tasks

[email protected] roles]# Cat Mysql/tasks/main.yml-name:install mysql-server package Yum:name=mysql-server State=pre Sent-name:start mysqld service service:name=mysqld state=started enabled=true-name:copy my.cnf to remote host Templ ATE:SRC=MY.CNF dest=/etc/my.cnf Tags:-modifymycnf notify:restart mysqld service[[email protected] roles]#


Handlers:

[email protected] roles]# cat mysql/handlers/main.yml-name:restart mysqld service Service:name=mysqld State=restarte D[[email protected] roles]#

The my.cnf differences under files and templates are as follows (two more configuration items under templates, one referencing the VARs variable file)

[Email protected] roles]# diff mysql/files/my.cnf mysql/templates/my.cnf 1a2,3> port=3306> bind-address={{host_ Ip.0}}[[email protected] roles]#

VARs

[email protected] roles]# cat mysql/vars/main.yml host_ip:-"{{ansible_all_ipv4_addresses.0}}" [[Email protected] roles]#

5. Edit Role PHP Task file

[email protected] roles]# Cat php/tasks/main.yml-name:install PHP package yum:name=php state=present

6. Start execution

[[email protected] ansible_playbooks]# ansible-playbook -i hosts site.yml  play [http] ******************************************************************* gathering  Facts *************************************************************** ok: [192.168.100.131]task :  [http | install httpd service] ******************************************  changed: [192.168.100.131]task: [http | start httpd service] ********  changed: [192.168.100.131]task: [http | modify  httpd config file from template] ************************* changed: [ 192.168.100.131]notified: [http | restart httpd service] ***********************  changed: [192.168.100.131]play [mysql] *********************************** ****************** gathering facts ***************************************************************  ok: [192.168.100.132]task: [mysql | install mysql-server package] ***  changed: [192.168.100.132]TASK: [mysql | start  Mysqld service] ****************************************** changed: [192.168.100.132]task:  [mysql | copy my.cnf to remote host] ********************************  ok: [192.168.100.132]play [lamp] ****************************************************  gathering facts ***************************************************************  ok: [192.168.100.130]task: [http | install httpd service] ***********  changed: [192.168.100.130]TASK: [http | start  Httpd servIce] ******************************************** changed: [192.168.100.130]task: [http  | modify httpd config file from template] *************************  changed: [192.168.100.130]task: [mysql | install mysql-server package]  ********************************** changed: [192.168.100.130]TASK: [mysql |  start mysqld service] ****************************************** changed: [ 192.168.100.130]task: [mysql | copy my.cnf to remote host] ***********  changed: [192.168.100.130]task: [php | install php  package] ********************************************* changed: [192.168.100.130]notified : [http | restart httpd service] **************************************  Changed: [192.168.100.130]notified: [mysql | restart mysqld service] ************************************  changed: [192.168.100.130]play recap **********************************************************  192.168.100.130            :  ok=10   changed=9    unreachable=0    failed=0    192.168.100.131            : ok=5     changed=4    unreachable=0    failed=0    192.168.100.132            : ok=4     changed=2    unreachable=0    failed=0    [[email protected] ansible_playbooks]#

View results

[[email protected] ansible_playbooks]#  ansible -i hosts all -m  shell -a  ' netstat -natpl |grep httpd ' 192.168.100.131 | success |  Rc=0 >>tcp        0      0  192.168.100.131:8080        0.0.0.0:*                    LISTEN       14662/httpd         192.168.100.132  | failed | rc=1 >>192.168.100.130 | success | rc=0 >> tcp        0      0  192.168.100.130:8080        0.0.0.0:*                    listen      11127/httpd          [[email protected] ansible_playbooks]#   ansible -i hosts all -m shell -a  ' netstat -natpl |grep  Mysqld ' 192.168.100.131 | failed | rc=1 >>192.168.100.132 | success  | rc=0 >>tcp        0       0 192.168.100.132:3306        0.0.0.0:*                    listen       61783/mysqld         192.168.100.130 | success | rc=0 >>tcp         0      0 192.168.100.130:3306        0.0.0.0:*                    LISTEN       11388/mysqld        [[email protected]  ansible_playbooks]#

You can also make when judgments:

[email protected] ansible_playbooks]# cat Hosts[http]192.168.100.131[mysql]192.168.100.132192.168.100.131[lamp] 192.168.100.130[[email protected] ansible_playbooks]# cat site.yml-hosts:mysql remote_user:root roles:-{role:m Ysql, When: "Ansible_nodename = = ' v3.lansgg.com '"}

Execution result: Can see the skipping that does not satisfy the condition

[[email protected] ansible_playbooks]# ansible-playbook -i hosts site.yml  play [mysql] ****************************************************************** gathering  Facts *************************************************************** ok: [192.168.100.131]ok:  [192.168.100.132]task: [mysql | install mysql-server package] ************  skipping: [192.168.100.131]changed: [192.168.100.132]task: [mysql  | start mysqld service] ****************************************** skipping:  [192.168.100.131]changed: [192.168.100.132]task: [mysql | copy my.cnf to  remote host] ************************************ skipping: [192.168.100.131]ok:  [192.168.100.132]play recap ****************************************************************** ** 192.168.100.131            : ok=1     changed=0    unreachable=0    failed=0    192.168.100.132            : ok=4     changed=2    unreachable=0    failed=0

7, you can also transfer the variable

[email protected] ansible_playbooks]# cat site.yml-hosts:http remote_user:root roles:-{role:http, Http_port: 2020,servername: "{{ansible_nodename}}"}[[email protected] ansible_playbooks]#

Tasks

[email protected] ansible_playbooks]# cat roles/http/tasks/main.yml-name:install httpd service yum:name=httpd state= Present-name:start httpd service service:name=httpd state=started enabled=true-name:modify httpd config file from Te Mplate template:src=httpd.conf dest=/etc/httpd/conf/httpd.conf Tags:-modifyhttpconf notify:-Restart httpd SE Rvice[[email protected] ansible_playbooks]#

Template file Variables section:

[[email protected] ansible_playbooks]# grep Listen roles/http/templates/httpd.conf |grep-v ^ #Listen {{Listen.0}}:{{htt P_port}}[[email protected] ansible_playbooks]# grep ServerName roles/http/templates/httpd.conf |grep-v ^# ServerName {{ Servername}}[[email protected] ansible_playbooks]#


Execution Result:

[[email protected] ansible_playbooks]# ansible-playbook -i hosts site.yml  play [http] ******************************************************************* gathering  Facts *************************************************************** ok: [192.168.100.131]task :  [http | install httpd service] ******************************************  changed: [192.168.100.131]task: [http | start httpd service] ********  changed: [192.168.100.131]task: [http | modify  httpd config file from template] ************************* changed: [ 192.168.100.131]notified: [http | restart httpd service] ***********************  changed: [192.168.100.131]play recap ************************************* ****************** 192.168.100.131            :  ok=5    changed=4    unreachable=0    failed =0


This article is from the "Big Wind" blog, please be sure to keep this source http://lansgg.blog.51cto.com/5675165/1747265

Ansible's roles introduction and actual combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.