且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ansible自动化部署之ROLES

更新时间:2022-08-19 21:31:01

一、ROLES 角色

   1、目录层级结构

   2、角色调用

   3、层级结构展示

    示例1:利用ansible角色安装nginx

    示例2:变量调用

    示例3:在playbook调用角色方法:传递变量给角色

    示例4:条件测试角色调用

    示例5:角色安装

    示例6:角色变量调整memcached内存大小


一、ROLES 角色

         对于以上所有的方式有个弊端就是无法实现复用假设在同时部署Web、db、ha 时或不同服务器组合不同的应用就需要写多个yml文件。很难实现灵活的调用。。

         roles 用于层次性、结构化地组织playbook。roles 能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。

1、目录层级结构

1
2
3
4
5
6
7
8
9
roles每个角色中,以特定的层级目录进行组织
  Mysql/  角色
     Files/     #存放有copy或script模块等调用的文件;’
     Tepmlates/ #template模块查找所需要模板文件目录;
     Tasks/     #定义任务;至少应该包含一个名为main.yml的文件;其他的文件需要在此文件中通过include进行包含。
     Handlers/  #定义触发器;至少应该包含一个名为main.yml的文件;其他的文件需要在此文件中通过include进行包含。
     Vars/      #定义变量;至少应该包含一个名为main.yml的文件;其他的文件需要在此文件中通过include进行包含。
     Meta/      #定义变量;至少应该包含一个名为main.yml的文件;定义当前角色的特殊设定及其依赖关系;其他的文件需要在此文件中通过include进行包含。
     Default/   #设定默认变量时使用此目录中的main.yml文件。

2、角色调用

1
2
3
4
5
6
7
8
[root@centos7_1 yaml]# vim roles.yml
   ---
    Hosts:web
   Remote_user:root
   Roles:
   - mysql
   - memchached
   - nginx

 3、层级结构展示

         Ansible自动化部署之ROLES

 

示例1:利用ansible角色安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@centos7_1 ~]# mkdir/etc/ansible/roles/nginx/{files,tasks,templates,handlers,vars, \
default,mata} –pv
#创建固定目录结构
[root@centos7_1 ~]# tree  /etc/ansible/roles/nginx/
/etc/ansible/roles/nginx/
├── default
├── files
├── handlers
├── mata
├── tasks
├── templates
└── vars
 
[root@centos7_1 ~]# cd/etc/ansible/roles/nginx/
[root@centos7_1 nginx]# vim tasks/main.yml  #创建任务
- name: install nginx package
  yum: name=nginx state=present
- name: install conf file
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
 #此处源文件可不写绝对路径,系统自查找。
 #- name: start nginx
 # service: name=nginx state=started
  
[root@centos7_1 ~]# cp /apps/work/files/nginx.conf.c6.j2 ../templates/nginx.conf.j2 
#将配置文件拷贝至templates目录内。
[root@centos7_1 ~]# cd /apps/yaml/
[root@centos7_1 yaml]# cat roles.yml #创建调用文件
---
- hosts: web
  remote_user: root
  roles:
  - nginx
[root@centos7_1 yaml]#ansible-playbook roles.yml  #利用ansible-playbook执行。

示例2:变量调用

1
2
3
4
5
6
7
8
9
利用定义变量使远程主机的nginx服务运行用户变更为daemon
[root@centos7_1 ~]# vim/etc/ansible/roles/nginx/vars/main.yml
username: daemon
[root@centos7_1 ~]# vim/etc/ansible/roles/nginx/templates/nginx.conf.j2
user {{ username }};  #  将此处原有用户修改为变量
[root@centos7_1 ~]# cd/apps/yaml/
[root@centos7_1 yaml]#ansible-playbook  roles.yml
[root@centos7_1 yaml]#ansible-playbook  -e"username=adm"  roles.yml
#也可以直接利用命令行传递变量参数给剧本文件。

示例3:在playbook调用角色方法:传递变量给角色

1
2
3
4
5
6
7
8
9
[root@centos7_1 yaml]vim roles.yml
---
 - hosts:web
   remote_user:root
   roles:
   - {role: nigix, username: nginx } 
  #在调用nginx角色是使用变量username:nginx时服务运行用户为nginx
   键role:用于指定角色名称;后续的键值对用户传递变量给角色
[root@centos7_1yaml]# ansible-playbook roles.yml

示例4:条件测试角色调用

1
2
3
4
5
6
7
8
9
还可以基于条件测试实现角色调用;
[root@centos7_1yaml]vim roles.yml
---
- hosts:web
  remote_user: root
  roles:
  {role: nigix, username: nginx ,when: “ansible_distribution_major_version ==’7’”}
#基于条件测试调用变量赋予nginx。
[root@centos7_1 yaml]#ansible-playbook -t instconf  roles.yml

示例5:角色安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@centos7_1 ~]# mkdir/etc/ansible/roles/memcached/tasks -pv
[root@centos7_1 ~]# vim  /etc/ansible/roles/memcached/tasks/main.yml
- name: install package
  yum: name=memcached state=present
- name: start memecached
  service: name=memcached state=started
     
[root@centos7_1 ~]# cd/apps/yaml/
[root@centos7_1 yaml]# cat mem.yml
---
- hosts: web
  remote_user: root
  roles:
  - { role: nginx,when:ansible_distribution_version == '7' }  
  #系统为centos7时调用执行nginx
  - { role: memcached,when: ansible_hostname =='memcached' }  
  #系统用户名为memcached的主机调用执行角色memcached。

示例6:角色变量调整memcached内存大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
利用变量使远程主机上的Memcahed的缓存大小占用系统内存大小的三分之一。
[root@centos7_1 ~]# cd/etc/ansible/roles/memcached/
[root@centos7_1 memcached]#ls
handlers/  tasks/    templates/
[root@centos7_1 memcached]#mkdir  templates
[root@centos7_1memcached]# scp 172.16.254.216:/etc/sysconfig/memcached \
    ./templates//memcached.j2
[root@centos7_1 memcached]#vim templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ansible_memtotal_mb//3 }}"
 #变量设置内存的3分之一  此变量为远程主机的总内存//3 指除3取商
  便为远程主机的三分之一
[root@centos7_1 memcached]#mkdir handlers/
[root@centos7_1 memcached]#vim handlers/main.yml
- name: restart memcached
  service: name=memcached state=restarted
[root@centos7_1 memcached]#cd /apps/yaml/
root@centos7_1 yaml]#ansible-playbook   mem.yml  #执行剧本

转自:http://lxlxlx.blog.51cto.com/3363989/1894385

本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1967514如需转载请自行联系原作者


qianghong000