自动化运维(四)Ansible Playbook Roles的使用
ansible roles 角色
适合大规模使用
playbook如果文件较多的情况,不清楚哪些主机执行了哪些状态的yml文件
roles能清楚哪些主机应用哪些角色
1.roles官方目录的结构
[root@m01 roles]# mkdir {nfs,rsync,web}/{vars,tasks,templates,handlers,files} -p [root@m01 roles]# tree . ├── nfs #角色名称 │ ├── files #存放文件 │ ├── handlers #触发任务 │ ├── tasks #具体任务 │ ├── templates #模板文件 │ └── vars #存放变量
2.roles变量定义方式
1.在vars目录中创建一个main.yml定义
2.在roles目录下创建一个group_vars定义,可以是主机组的名称,也可以是all(所有)
3.Playbook Roles实战
rsync模块
1.使用roles创建Rsync服务, 目录结构如下
[root@m01 roles]# tree /etc/ansible/roles/ /etc/ansible/roles/ ├── hosts ├── rsync │ ├── files │ │ ├── rsyncd.conf │ │ └── rsync.passwd │ ├── handlers │ │ └── main.yml │ ├── tasks │ │ └── main.yml │ ├── templates │ └── vars ├── site.yml
2.定义roles主机清单
[root@m01 roles]# cat /etc/ansible/roles/hosts [backup] 172.16.1.41
3.指定backup主机组,执行那个roles
[root@m01 roles]# cat /etc/ansible/roles/site.yml - hosts: backup remote_user: root roles: - rsync
4.查看rsync角色的tasks任务
[root@m01 roles]# 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: "rsyncd.conf", dest: "rsyncd.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
5.查看rsync角色的handlers
[root@m01 roles]# cat /etc/ansible/roles/rsync/handlers/main.yml - name: Restart Rsync Server service: name=rsyncd state=restarted
6.查看rsync角色的files目录
[root@m01 roles]# ll /etc/ansible/roles/rsync/files/ total 8 -rw-r--r-- 1 root root 322 Nov 16 18:49 rsyncd.conf -rw------- 1 root root 20 Nov 16 18:30 rsync.passwd
NFS模块
1.使用roles创建Nfs服务, 目录结构如下
[root@m01 roles]# tree /etc/ansible/roles ├── group_vars │ └── all ├── hosts ├── nfs │ ├── files │ ├── handlers │ │ └── main.yml │ ├── tasks │ │ └── main.yml │ ├── templates │ │ └── exports │ └── vars ├── site.yml
2.定义roles主机清单
[root@m01 roles]# cat /etc/ansible/roles/hosts [nfs] 172.16.1.31
3.指定nfs主机组,执行那个roles
[root@m01 roles]# cat /etc/ansible/roles/site.yml - hosts: nfs remote_user: root roles: - nfs tags: nfs
4.查看nfs角色的tasks任务
[root@m01 roles]# cat /etc/ansible/roles/nfs/tasks/main.yml - name: Install 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
5.查看nfs角色的handlers
[root@m01 roles]# cat /etc/ansible/roles/nfs/handlers/main.yml - name: Restart Nfs-Server service: name=nfs state=restarted
6.查看nfs角色的files目录
[root@m01 roles]# cat /etc/ansible/roles/nfs/templates/exports {{ share_dir }} {{ share_ip }}(rw,sync,all_squash,anonuid=666,anongid=666)
7.nfs对应的变量定义
[root@m01 roles]# cat /etc/ansible/roles/group_vars/all #nfs share_dir: /data share_ip: 172.16.1.31