5.自动化运维工具-Ansible流程控制
孙富阳, 江湖人称没人称。多年互联网运维工作经验,曾负责过孙布斯大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型博客网站运维工作。
1.Playbook条件语句when
不管是 shell 还是各大编程语言中,流程控制,条件判断这些都是必不可少的,在我们使用 Ansible 的过程中,条件判断的使用频率极其高。
例如:
1.我们使用不同的系统的时候,可以通过判断系统来对软件包进行安装。
2.在 nfs 和 rsync 安装过程中,客户端服务器不需要推送配置文件,之前我们都是写多个play,会影响效率。
3.我们在源码安装nginx的时候,执行第二遍就无法执行了,此时我们就可以进行判断是否安装过。
示例一:根据不同的操作系统安装apache
[root@mb01 ~]# cat /server/playbook/when_yum.yml
---
- name: 安装httpd
hosts: 172.16.1.7
tasks:
- name: 开始安装
yum:
name: httpd
state: installed
when: ansible_distribution == "CentOS"###这个就是条件语句
- name: ubuntu安装httpd2
yum:
name: httpd2
state: installed
when: ansible_distribution == "ununb"
案例二:为web主机名的添加nginx仓库,其余的都跳过
[root@mb01 ~]# cat /server/playbook/when_nginx.reop.yml
---
- name: 配置yum源
hosts: lbweb
tasks:
- name: 添加nginx的yum仓库
yum_repository:
name: nginx stable repo
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: 1
enabled: 1
gpgkey: https://nginx.org/keys/nginx_signing.key
description: nginx stable repo
when: ansible_hostname is match "lb"
案例三:根据前者命令执行的结果进行判断
[root@mb01 ~]# cat /server/playbook/zhuce.yml
---
- name: ceshi register
gather_facts: no
hosts: 172.16.1.7
tasks:
- name: 拷贝配置文件
copy:
src: /server/playbook/exports
dest: /opt
- name: 查看nginx的状态
shell: systemctl status nginx
register: nx
ignore_errors: yes
- name: 查看status状态
debug:
msg: "{{ nx.rc }}"
- name: 重载nginx
systemd:
name: nginx
state: reloaded
when: nx.rc == 0
2.Playbook循环语句with_items
有时候我们写playbook的时候发现了很多task都要重复引用某个模块,比如一次启动10个服务,或者一次拷贝10个文件,如果按照传统的写法最 少要写10次,这样会显得playbook很臃肿。如果使用循环的方式来编写playbook,这样可以减少重复使用某个模块。
案例一:使用循环变量启动多个服务
第一种写法with_items
[root@mb01 ~]# cat /server/playbook/xunhuan_yum.yml
---
- name: 安装软件
hosts: 172.16.1.7
tasks:
- name: 执行安装操作
systemd:
name: "{{ item }}"
state: started
with_items:
- nginx
- crond
第二种写法loop
[root@mb01 ~]# cat /server/playbook/xunhuan_yum.yml
---
- name: 安装软件
hosts: 172.16.1.7
tasks:
- name: 执行安装操作
systemd:
name: "{{ item }}"
state: started
loop:
- nginx
- crond
案例二:使用循环变量的字典变量
[root@mb01 ~]# cat /server/playbook/xunhuan_yum.yml
---
- name: 安装软件
hosts: 172.16.1.7
tasks:
- name: 执行安装操作
systemd:
name: "{{ item.pack }}"
state: started
with_items:
- { pack: nginx }
- { pack: crond }
when: ansible_hostname is match "lb"
3.Playbook触发器Handlers
handler 用来执行某些条件下的任务,比如当配置文件发生变化的时候,通过notify触
handler去重启服务。 在saltstack中也有类似的触发器,写法相对Ansible简单,只需要watch,配置文件即可。
例如:只要配置文件发生变更,则会触发handlers执行重启服务操作,如果配置文件不发生任何变化则不重启。 应用场景: notify监控配置文件变化, handlers 实现重启服务/重新挂载..
###notify与具体的任务同级,handlers与tasks同级
[root@mb01 ~]# cat /opt/nfs.yml
---
- name: 服务端搭建nfs服务
hosts: 172.16.1.51
remote_user: root
tasks:
- name: 服务端安装nfs服务
yum:
name: nfs-utils
state: installed
- name: 书写配置文件
copy:
src: /server/playbook/exports
dest: /etc/exports
backup: yes
notify:
- nfs变化
- name: 创建共享目录
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
mode: 0755
- name: 启动服务
systemd:
name: nfs
state: started
enabled: yes
- name: 启动rcp
systemd:
name: rpcbind
state: started
enabled: yes
handlers:
- name: nfs变化,重启nfs
systemd:
name: nfs
state: restarted
enabled: yes
4.Playbook任务标签tag
默认情况下,Ansible在执行一个playbook时,会执行playbook中定义的所有任务。Ansible的标签(Tags)功能可以给单独任务甚至整个playbook打 上标签,然后利用这些标签来指定要运行playbook中的个别任务,或不执行指定的任务
一般应用场景:
用于调试,
运行指定的task
排除指定的task
1.打标签的方式有几种,比如:
对一个task打一个标签、对一个task打多个标签、对多个task打一个标签
2、对task打完标签应该如何使用
-t: 执行指定的tag标签任务
--skip-tags: 执行--skip-tags之外的标签任务
##书写剧本
[root@mb01 ~]# cat /server/playbook/nfs.yml
---
- name: 服务端搭建nfs服务
hosts: 172.16.1.51
remote_user: root
tasks:
- name: 服务端安装nfs服务
yum:
name: nfs-utils
state: installed
- name: 书写配置文件
copy:
content: /data/ 172.16.1.0/24(ro,no_all_squash)
dest: /etc/exports
backup: yes
notify:
- nfs变化
- nfs变化
tags: test########################标签放在这里可以生效
- name: 创建共享目录
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
mode: 0755
- name: 启动服务
systemd:
name: nfs
state: started
enabled: yes
- name: 启动rcp
systemd:
name: rpcbind
state: restarted
enabled: yes
handlers:
- name: nfs变化,重启nfs
systemd:
name: nfs
state: restarted
enabled: yes
listen: nfs变化
tags: test######################一般将标签放在这里无效
只执行某个标签
[root@mb01 ~]# ansible-playbook /server/playbook/nfs.yml -t test
排除,执行标签外的任务
[root@mb01 ~]# ansible-playbook /server/playbook/nfs.yml –skip-tags test
5.Playbook忽略错误ignore_errors
默认playbook会检测task执行的返回状态,如果遇到错误则会立即终止playbook的后续task执行,然鹅有些时候 playbook即使执行错误了也要让其继续执行。
加入参数:ignore_errors:yes 忽略错误
[root@mb01 ~]# cat /server/playbook/nfs.yml
---
- name: 服务端搭建nfs服务
hosts: 172.16.1.51
gather_facts: no
remote_user: root
tasks:
- name: 服务端安装nfs服务
yum:
name: nfs-utils
state: installed
- name: 书写配置文件
copy:
content: /data/ 172.16.1.0/24(ro,no_all_squash)
dest: /etc/exports
backup: yes
notify:
- nfs变化
- nfs变化
tags: test
- name: 创建共享目录
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
mode: 0755
- name: 启动服务
systemd:
name: nft###############nft不存在,一定会报错
state: started
enabled: yes
ignore_errors: yes#########不加此参数,后面的所有任务都不执行。这是忽略错误
- name: 启动rcp
systemd:
name: rpcbind
state: restarted
enabled: yes
handlers:
- name: nfs变化,重启nfs
systemd:
name: nfs
state: restarted
enabled: yes
listen: nfs变化
6.playbook错误处理force_handlers|changed_when|filed_when
当task执行失败时,同时没有设置忽略错误,playbook将不再继续执行,包括如果在task中设置了handler也不会被执行。 但是我们可以采取强制措施force_handlers: yes
[root@mb01 ~]# cat /server/playbook/nfs.yml
---
- name: 服务端搭建nfs服务
hosts: 172.16.1.51
force_handlers: yes
gather_facts: no
remote_user: root
tasks:
- name: 服务端安装nfs服务
yum:
name: nfs-utils
state: installed
- name: 书写配置文件
copy:
content: /data/ 172.16.1.0/24(ro,no_all_squash)
dest: /etc/exports
backup: yes
notify:
- nfs变化
- nfs变化
tags: test
- name: 创建共享目录
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
mode: 0755
- name: 启动服务
systemd:
name: nft
state: started
enabled: yes
- name: 启动rcp
systemd:
name: rpcbind
state: restarted
enabled: yes
handlers:
- name: nfs变化,重启nfs
systemd:
name: nfs
state: restarted
enabled: yes
当被控端状态未发生变化不希望其返回状态为changed,添加changed_when为no
[root@mb01 ~]# cat /server/playbook/nfs.yml
---
- name: 服务端搭建nfs服务
hosts: 172.16.1.51
force_handlers: yes
gather_facts: no
remote_user: root
tasks:
- name: 服务端安装nfs服务
yum:
name: nfs-utils
state: installed
- name: 书写配置文件
copy:
content: /data/ 172.16.1.0/24(ro,no_all_squash)
dest: /etc/exports
backup: yes
changed_when: no
notify:
- nfs变化
- nfs变化
tags: test
- name: 创建共享目录
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
mode: 0755
- name: 启动服务
systemd:
name: nft
state: started
enabled: yes
changed_when: no
- name: 启动rcp
systemd:
name: rpcbind
state: restarted
enabled: yes
handlers:
- name: nfs变化,重启nfs
systemd:
name: nfs
state: restarted
enabled: yes
listen: nfs变化
changed_when检查标准输出结果中是否包含关键字,不包含则抛出错误
[root@mb01 ~]# cat /server/playbook/nfs.yml
---
- name: 服务端搭建nfs服务
hosts: 172.16.1.51
force_handlers: yes
gather_facts: no
remote_user: root
tasks:
- name: 服务端安装nfs服务
yum:
name: nfs-utils
state: installed
- name: 书写配置文件
copy:
content: /data/ 172.16.1.0/24(ro,no_all_squash)
dest: /etc/exports
backup: yes
changed_when: no
notify:
- nfs变化
- nfs变化
tags: test
- name: 创建共享目录
file:
path: /data/
state: directory
owner: nfsnobody
group: nfsnobody
mode: 0755
- name: 启动服务
systemd:
name: nft
state: started
enabled: yes
register: check_nft
changed_when:
- check_nfs.stdout.find('running')####匹配变量注册中标准输出是否有关键字,没有则抛出错误
- name: 启动rcp
systemd:
name: rpcbind
state: restarted
enabled: yes
handlers:
- name: nfs变化,重启nfs
systemd:
name: nfs
state: restarted
enabled: yes
listen: nfs变化
filed_when当匹配到规则后抛出错误,注意如果同时使用ignore_errors会继续向下执行任务
[root@mb01 ~]# vim /server/playbook/zhuce.yml
---
- name: ceshi register
gather_facts: no
hosts: 172.16.1.7
tasks:
- name: 拷贝配置文件
copy:
src: /server/playbook/exports
dest: /opt
- name: 查看nginx的状态
shell: nginx -t
register: nx
failed_when:
- '"successful" in nx.stderr'####匹配到successful则抛出错误
ignore_errors: yes
7.playbook文件复用
在之前写playbook的过程中,我们发现,写多个playbook没有办法,一键执行,这样我们还要单个playbook挨个去执行,很鸡肋。所以在playbook中有一个功能,叫做 include 用来动态调用task任务列表。
#定义阶段
[root@mb01 ~]# cat /server/playbook/fuyong.yum.yml
- name: anz
yum:
name: "{{ soft }}"
state: installed
[root@mb01 ~]# cat /server/playbook/fuyong.yum2.yml
- name: 安装http
yum:
name: httpd
state: installed
#调用阶段
[root@mb01 ~]# cat 1.fuyong.yml
---
- name: 复用
hosts: 172.16.1.7
vars_files:
- /server/playbook/vars/01_vars.yml
tasks:
- name: zho
include: /server/playbook/fuyong.yum.yml
- include_tasks: /server/playbook/fuyong.yum2.yml
评论已关闭