4.1、Roles基本概述
Roles是组织playbook最好的一种方式,它基于一个已知的文件结构,自动的加载vars,tasks以及handlers以便playbook更好的调用,Roles相比playbook的结构更加的清晰有层次,但roles要比playbook稍微麻烦一些
比如:安装任何软件都需要先安装时间同步,那么每个playbook都要编写时间同步服务的task,会显得整个配置比较臃肿,且难以维护
如果使用Roles:我们则可以将时间同步服务task任务编写好,等到需要使用的时候进行调用就行了, 减少重复编写task带来的文件臃肿
4.2、Roles目录结构
roles官方目录结构,必须按如下方式定义。在每个目录中必须有main.yml文件,这些属于强制要用
cd /etc/ansible/roles
mkdir {nfs,rsync,web}/{vars,tasks,templates,handlers,files,meta} -p
nfs #角色名字
files #存放文件
handlers #触发条件
tasks #具体任务
templates #模板文件
vars #定义变量
meta #依赖关系
4.3、Roles依赖关系
roles允许在使用时自动引入其他role,role依赖关系存储在meta/main.yml文件中
例如:安装wordPress项目时:
1、需要先确保nginx与php-fpm的role都能正常运行
2、然后在wordpress的role中定义,依赖关系
3、依赖的role有nginx以及php-fpm
#wordpress依赖nginx与php-fpm的role
cat /root/roles/wordpress/meta/main.yml
---
dependencies:
- { role: nginx }
- { role: php-fpm }
wordpress的role会先执行nginx、php-fpm的role,最后再执行wordpress本身
4.4、Roles编写思路
1、创建roles目录结构,手动创建或者使用ansible-galaxy init test roles
2、编写roles功能,也就是tasks
3、最后playbook引用roles编写好的tasks
4.5、案例1-Roles部署Rsync
1、目录结构
hosts
rsync
files
rsyncd.conf
rsync.passwd
handlers
main.yml
tasks
main.yml
templates
vars
site.yml
2、定义roles主机清单
cat /etc/ansible/roles/hosts
[backup]
192.168.10.131
3、查看rsync角色的tasks任务
cat /etc/ansible/roles/rsync/tasks/main.yml
- name: Install Rsync Server
yum: name=rsync state=present
- name: Configure Rsync Server
copy: src={{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
with_items:
- { src: "rsync.conf",dest: "rsync.conf",mode: "0644" }
- { src: "rsync.passwd",dest: "rsync.passwd",mode: "0600" }
notify: Restart Rsync Server
- name: Start Rsync Server
service: name=rsyncd state=started enabled=yes
4、查看rsync角色的handlers
cat /etc/ansible/roles/rsync/handlers/main.yml
- name: Restart Rsync Server
service: name=rsyncd state=restarted
5、查看rsync角色的file目录
ll /etc/ansible/roles/rsync/files
rsync.conf
rsync.passwd
6、在playbook中使用role,指定backup主机组,执行rsync服务的roles
cat /etc/ansible/roles/site.yml
- hosts: backup
remote_user: root
roles:
- rsync
ansible-playbook -i hosts site.yml
4.6、案例2-Roles部署NFS
1、目录结构如下
tree /etc/ansible/roles
group_vars
all
hosts
nfs
files
handlers
main.yml
tasks
main.yml
templates
exports
vars
site.yml
2、定义roles主机清单
cat /etc/ansible/roles/hosts
[nfs]
192.168.10.132
3、查看nfs角色的tasks任务
cat /etc/ansible/roles/nfs/files/main.yml
- name: Installed NFS-Server
yum: name=nfs-utils state=present
- name: Configure NFS-Server
template: src=exports dest=/etc/exports
notify: Restart NFS-Server
- name: Create Directory Data
file: path={{ share_dir }} state=directory owner=www group=www mode=0755
- name: Start NFS-Server
service: name=nfs state=started enabled=yes
4、查看nfs角色的handlers
cat /etc/ansible/roles/nfs/handlers/main.yml
- name: Restart NFS-Server
service: name=nfs state=restarted
5、查看nfs角色的files目录
cat /etc/ansible/roles/nfs/files/exports
{{ share_dir }} {{ share_ip }}(rw,sync,all_squash,anonuid=666,anongid=666)
6、nfs对应的变量定义
cat /etc/ansible/roles/group_vars/all
#nfs
share_dir: /data
share_ip: 192.168.10.133
7、在playbook中使用role,指定nfs主机组,执行nfs服务的roles
cat /etc/ansible/roles/site.yml
- hosts: nfs
remote_user: root
roles:
- nfs
ansible-playbook -i hosts site.yml
4.7、案例3-Roles部署Memcached
1、目录结构如下
tree /etc/ansible/roles/memcached
tasks
main.yml
start.yml
template.yml
yum.yml
templates
memcached.j2
2、查看memcached的tasks
cat tasks/main.yml
- include: yum.yml
- include: template.yml
- include: start.yml
cat tasks/yum.yml
- name: Install memcached package
yum: name=memcached
cat tasks/template.yml
- name: Copy memcached conf
template: src=memcached.j2 dest=/etc/sysconfig/memcached
cat tasks/start.yml
- name: start memcached
service: name=memcached state=started enabled=yes
cat templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="{{ ansible_memtotal_mb//4 }}"
CACHESIZE="64"
OPTIONS=""
3、在playbook中使用role,执行memcached服务的roles
cat site.yml
- hosts: "{{ host }}"
remote_user: root
roles:
- role: memcached
ansible-playbook site.yml -e "host=192.168.10.133"
标签:rsync,name,roles,ansible,Ansible,nfs,五月,Roles,yml
From: https://blog.51cto.com/u_13236892/6509608