首页 > 代码库 > Ansible 的角色定义及调用
Ansible 的角色定义及调用
ansible 角色定义及调用
==========================================================================
概述:
本章是上篇ansible的后续,将主要介绍ansible中角色的定义和调用,内容如下:
角色目录的定义方法
在playbook中调用角色的方法;
示例:
·定义nginx角色并调用;
·定义memcached角色并调用;
·定义mysql角色并调用;
==========================================================================
角色:roles
---以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files(依赖的文件)等;
★角色目录的定义:
role_name/(以角色名命名的目录)
files/:
存储由copy或script等模块调用的文件;
tasks/:
此目录中至少应该有一个名为main.yml的文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用;
handlers/:
此目录中至少应该有一个名为main.yml的文件,用于定义各handler;其它的文件需要由main.yml进行“包含”调用;
vars/:
此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件需要由main.yml进行“包含”调用;
templates/:
存储由template模块调用的模板文本;
meta/:
此目录中至少应该有一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系;其它的文件需要由main.yml进行“包含”调用;
default/:
此目录中至少应该有一个名为main.yml的文件,用于设定默认变量;
★在playbook中调用角色的方法:
演示:
创建角色,并在playbook中调用角色
在/etc/ansible/roles(路径不唯一)中定义role_name以及各role_name中的模块文件如下:
[root@centos7 roles]# mkdir ./{nginx,memcached,httpd,mysql}/{files,templates,vars,handlers,meta,default,tasks} -pv [root@centos7 roles]# ls httpd memcached mysql nginx [root@centos7 roles]# tree . ├── httpd │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars ├── memcached │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars ├── mysql │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars └── nginx ├── default ├── files ├── handlers ├── meta ├── tasks ├── templates └── vars
定义nginx角色,并调用:
1.现在我们首先定义nginx角色的任务目录,其中会调用到一些其他的模块,它会自动到对应的模块中获取文件内容,如下:
[root@centos7 roles]# vim nginx/tasks/main.yml - name: copy nginx package to remote host # 调用files模块 copy: src=http://www.mamicode.com/nginx-1.10.0-1.el7.ngx.x86_64.rpm dest=/tmp/nginx-1.10.0-1.el7.ngx.x86_64.rpm >2.定义在nignx/tasks中要调用模块的目录,如下:
1)handlers
2)准备templates的模板文件,并修改其进程数为系统变量,监听端口为自定义变量如下:
[root@centos7 roles]# cp /etc/nginx/nginx.conf nginx/templates/nginx.conf.j2 [root@centos7 roles]# vim nginx/templates/nginx.conf.j2 worker_processes {{ ansible_processor_vcpus }}; [root@centos7 roles]# cp /etc/nginx/conf.d/default.conf nginx/templates/default.conf.j2 [root@centos7 roles]# vim nginx/templates/default.conf.j2 listen {{ ngxport }}; # 自定义变量3)准备files文件
[root@centos7 roles]# cp /root/nginx/nginx-1.10.0-1.el7.ngx.x86_64.rpm nginx/files/ [root@centos7 roles]# ls nginx/files/ nginx-1.10.0-1.el7.ngx.x86_64.rpm4)因为我们在templates中用到了自定义变量,所以,在/etc/ansible/roles/nginx/vars中定义变量,如下:
[root@centos7 roles]# vim nginx/vars/main.yml 1 ngxport: "8088"定义的文件概览
[root@centos7 roles]# pwd /etc/ansible/roles [root@centos7 roles]# tree . ├── httpd │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars ├── memcached │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars ├── mysql │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars └── nginx ├── default ├── files │ └── nginx-1.10.0-1.el7.ngx.x86_64.rpm ├── handlers │ └── main.yml ├── meta ├── tasks │ └── main.yml ├── templates │ ├── default.conf.j2 │ └── nginx.conf.j2 └── vars └── main.yml
3.如上,我们要调用的各模块都已经准备好了,现在我们要去调用nginx角色
1)首先定义play-book,指明远程主机,和要调用的角色
[root@centos7 ansible]# pwd /etc/ansible [root@centos7 ansible]# ls ansible.cfg ansible.cfg.bak hosts hosts.bak roles [root@centos7 ansible]# vim roles.yaml - hosts: ngxsrvs remote_user: root roles: # 这里要写调用的角色roles - nginx2)编辑/etc/ansible/ansible.cfg,修改其调用roles的路径
4.检测语法,测试运行
[root@centos7 roles]# ansible-playbook --syntax-check /etc/ansible/nginx.yaml playbook: /etc/ansible/nginx.yaml # 首先把程序包复制过去(因为如果远程主机没有程序报的话,测试执行后面的任务会报错) [root@centos7 roles]# ansible-playbook -t cppkg /etc/ansible/nginx.yaml PLAY [ngxsrvs] ***************************************************************** TASK [setup] ******************************************************************* ok: [10.1.252.156] ok: [10.1.252.161] TASK [nginx : copy nginx package to remote host] ******************************* changed: [10.1.252.156] changed: [10.1.252.161] PLAY RECAP ********************************************************************* 10.1.252.156 : ok=2 changed=1 unreachable=0 failed=0 10.1.252.161 : ok=2 changed=1 unreachable=0 failed=0 # 测试执行, [root@centos7 roles]# ansible-playbook -C /etc/ansible/nginx.yaml PLAY [ngxsrvs] ***************************************************************** TASK [setup] ******************************************************************* ok: [10.1.252.156] ok: [10.1.252.161] TASK [nginx : copy nginx package to remote host] ******************************* ok: [10.1.252.156] ok: [10.1.252.161] TASK [nginx : install nginx package] ******************************************* ok: [10.1.252.156] ok: [10.1.252.161] TASK [nginx : install conf file nginx.conf] ************************************ changed: [10.1.252.156] ok: [10.1.252.161] TASK [nginx : install conf file default.conf] ********************************** changed: [10.1.252.156] changed: [10.1.252.161] TASK [nginx : start nginx service] ********************************************* changed: [10.1.252.156] changed: [10.1.252.161] RUNNING HANDLER [nginx : reload nginx service] ********************************* changed: [10.1.252.156] changed: [10.1.252.161] PLAY RECAP ********************************************************************* 10.1.252.156 : ok=7 changed=4 unreachable=0 failed=0 10.1.252.161 : ok=7 changed=3 unreachable=0 failed=05.测试运行没有问题,然后真正的运行,在远程主机上查看,可以发现已经监听在8088端口;
[root@centos7 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 128 *:8088 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 ::1:60106.假如我们现在不想监听在8088端口,而是想监听在80端口,这时除了可以修改vars中的变量ngxport外,还可以在playbook中调用角色时,直接向角色传递变量(以字典的方式传递),如下:
因为,这里我只是修改了一下配置文件,所以只需要执行配置文件的标签ngxconf跑下配置文件即可,执行之后可以发现,远程主机监听的端口为80端口,说明直接在调用角色是传递变量要比在vars中定义的变量的优先级要高,如下:
------------------------------------------------------------------------------
[root@centos7 ansible]# ansible-playbook --check --list-tags nginx.yaml # 列出标签 playbook: nginx.yaml play #1 (ngxsrvs): ngxsrvs TAGS: [] TASK TAGS: [cppkg, ngxconf] [root@centos7 ansible]# ansible-playbook -C -t ngxconf nginx.yaml PLAY [ngxsrvs] ***************************************************************** TASK [setup] ******************************************************************* ok: [10.1.252.156] ok: [10.1.252.161] TASK [nginx : install conf file nginx.conf] ************************************ ok: [10.1.252.156] ok: [10.1.252.161] TASK [nginx : install conf file default.conf] ********************************** changed: [10.1.252.156] # 只运行配置文件default.conf changed: [10.1.252.161] RUNNING HANDLER [nginx : reload nginx service] ********************************* changed: [10.1.252.156] # 因为配置文件发生了改变,notify通知触发脚本 changed: [10.1.252.161] PLAY RECAP ********************************************************************* 10.1.252.156 : ok=4 changed=2 unreachable=0 failed=0 10.1.252.161 : ok=4 changed=2 unreachable=0 failed=0===========================================================================
如上,就是整个定义nginx角色并调用的整个过程,如果我们以后要在多台主机上安装运行nginx,只需要把/etc/ansible/roles/nginx目录拷贝过去然后定义主机,运行ansible-playbook就可以了,大大减轻了我们的工作量!
===========================================================================
注意:
ansible默认一次只可以调用5个主机,如果我们系统主机的性能比较好,可以根据
实际情况适当的调大一些,-f forks 也可以定义一批处理几个主机;
============================================================================
2.定义memcached角色,并调用:
1.首先定义memcached角色的任务目录,
[root@centos7 ansible]# vim roles/memcached/tasks/main.yml 1 - name: install memcached 2 yum: name=memcached state=latest 3 - name: install conf file 4 template: src=http://www.mamicode.com/memcached.j2 dest=/etc/sysconfig/memcached>2.我们这里要定义memcached的可用缓存空间为系统可用空间的四分之一,所以要调用template模块文件,定义如下:
[root@centos7 ansible]# cp /etc/sysconfig/memcached roles/memcached/templates/memcached.j2 [root@centos7 ansible]# vim roles/memcached/templates/memcached.j2 1 PORT="11211" 2 USER="memcached" 3 MAXCONN="1024" 4 CACHESIZE="{{ ansible_memtotal_mb // 4 }}" 5 OPTIONS=""3.定义notify通知的触发器handler模块文件
[root@centos7 ansible]# vim roles/memcached/handlers/main.yml 1 - name: reload memcached 2 service: name=memcached state=restarted4.如上,memcached的角色就已经定义好了,现在我们在之前定义好的roles.yaml的剧本中添加上memecached角色,就可以直接调用了,如下:
[root@centos7 ansible]# vim roles.yaml 1 - hosts: ngxsrvs 2 remote_user: root 3 roles: 4 - { role: nginx, ngxport: 80 } 5 - { role: memcached,tags: mechd }5.列出tags,检查语法,运行如下:
[root@centos7 ansible]# ansible-playbook --syntax-check roles.yaml # 检测语法 playbook: roles.yaml [root@centos7 ansible]# ansible-playbook -C --list-tags roles.yaml # 列出tags playbook: roles.yaml play #1 (ngxsrvs): ngxsrvs TAGS: [] TASK TAGS: [cppkg, mcconf, mechd, ngx, ngxconf] [root@centos7 ansible]# ansible-playbook -C -t mechd roles.yaml # 运行如下 PLAY [ngxsrvs] ***************************************************************** TASK [setup] ******************************************************************* ok: [10.1.252.161] ok: [10.1.252.156] TASK [memcached : install memcached] ******************************************* changed: [10.1.252.156] changed: [10.1.252.161] TASK [memcached : install conf file] ******************************************* changed: [10.1.252.156] changed: [10.1.252.161] TASK [memcached : start memcached] ********************************************* changed: [10.1.252.156] changed: [10.1.252.161] PLAY RECAP ********************************************************************* 10.1.252.156 : ok=4 changed=0 unreachable=0 failed=0 10.1.252.161 : ok=4 changed=0 unreachable=0 failed=06.在远程主机查看端口11211,并查看memcached缓存大小如下:
[root@centos7 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:11211 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 :::11211 :::* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 ::1:6010 :::* [root@centos7 ~]# cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="244" OPTIONS=""
======================================================================================
3.定义mysql角色,并调用:
1)定义mysql角色的任务目录,
[root@centos7 ansible]# vim roles/mysql/tasks/main.yml 1 - name: isntall mysql-server 2 yum: name=mysql-server state=latest 3 when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6" 4 - name: isntall mariadb-server 5 yum: name=mariadb-server state=latest 6 when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7" 7 - name: start mysql service 8 service: name=mysqld state=started 9 when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6" 10 - name: start mariadb service 11 service: name=mariadb state=started 12 when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"2)调用mysql如下:
[root@centos7 ansible]# vim mysql.yaml 1 - hosts: dbsrvs 2 remote_user: root 3 roles: 4 - mysql3)运行如下:
[root@centos7 ansible]# ansible-playbook mysql.yaml PLAY [dbsrvs] ****************************************************************** TASK [setup] ******************************************************************* ok: [10.1.252.205] ok: [10.1.252.161] TASK [mysql : isntall mysql-server] ******************************************** skipping: [10.1.252.161] changed: [10.1.252.205] TASK [mysql : isntall mariadb-server] ****************************************** skipping: [10.1.252.205] ok: [10.1.252.161] TASK [mysql : start mysql service] ********************************************* skipping: [10.1.252.161] # when语句,不符合就跳过 changed: [10.1.252.205] TASK [mysql : start mariadb service] ******************************************* skipping: [10.1.252.205] changed: [10.1.252.161] PLAY RECAP ********************************************************************* 10.1.252.161 : ok=3 changed=1 unreachable=0 failed=0 10.1.252.205 : ok=3 changed=2 unreachable=0 failed=0
总结:
nginx角色的定义
memcached角色定义:
nginx和memcached的角色调用:
mysql角色定义:
roles目录中个角色的模块文件概览
[root@centos7 ansible]# tree roles roles ├── httpd │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ ├── templates │ └── vars ├── memcached │ ├── default │ ├── files │ ├── handlers │ │ └── main.yml │ ├── meta │ ├── tasks │ │ └── main.yml │ ├── templates │ │ └── memcached.j2 │ └── vars ├── mysql │ ├── default │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ │ └── main.yml │ ├── templates │ └── vars └── nginx ├── default ├── files │ └── nginx-1.10.0-1.el7.ngx.x86_64.rpm ├── handlers │ └── main.yml ├── meta ├── tasks │ └── main.yml ├── templates │ ├── default.conf.j2 │ └── nginx.conf.j2 └── vars └── main.yml 32 directories, 10 files
如上就是整个ansible中角色roles的定义和调用。。。
Ansible上篇文章链接:http://1992tao.blog.51cto.com/11606804/1871264
Ansible 的角色定义及调用