六ansible-playbook任务控制

落花踏尽游何处,笑入胡姬酒肆中。这篇文章主要讲述六ansible-playbook任务控制相关的知识,希望能为你提供帮助。
一、playbook任务控制1、playbook错误处理
(1)、error????ignore_errors:对错误的处理方式
--True表示忽略错误继续执行
--False 表示遇到错误就停止执行 默认False
?

cat error_test.yaml

---
- hosts: webserver
remote_user: root
tasks:
- shell: /usr/bin/false
#ignore_errors: True
- ping:

?ansible-playbook error_test.yaml -i hosts
六ansible-playbook任务控制

文章图片

报错以后不再执行下面的ping


cat error_test.yaml

---
- hosts: webserver
remote_user: root
tasks:
- shell: /usr/bin/false
ignore_errors: True
- ping:

??ansible-playbook error_test.yaml -i hosts
六ansible-playbook任务控制

文章图片

报错了依旧执行下面的ping
(2)、force_handler通常情况下,当tasks失败,play将会终止,任何在前面已经被tasks notify的handlers都不会被执行,如果在play中设置了force_handlers:yes ,被通知的handlers就会强制执行


- hosts: webserver
remote_user: root
force_handlers: yes ##强制调用handlers
tasks:

2、tags
?打标签,单独运行某个标签或者忽略某个标签


将webserver机器的httpd.cnf拿过来
scp root@192.168.10.128:/etc/httpd/conf/httpd.conf .

cat httpd.conf |grep 8089
Listen 8089 ##配置文件就改了一下端口

cat apache.yaml
---
- hosts: webserver
remote_user: root
tasks:
- name: install the latest version of apache
yum:
name: httpd
state: latest
- copy:
src: httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: apache
group: apache
mode: \'0644\'
tags: testtag ##此处打了个标签
- copy:
src: index.html
dest: /var/www/html/index.html
owner: apache
group: apache
mode: \'0644\'
- service:
name: httpd
state: started
enabled: yes

?ansible-playbook apache.yaml -i hosts -t testtag
六ansible-playbook任务控制

文章图片

ansible-playbook apache.yaml -i hosts -t testtag
或者ansible-playbook apache.yaml -i hosts --tags=testtag
如果想忽略某个tags
ansible-playbook apache.yaml -i hosts --skip-tags=testtag
3、handlers
?当关注的资源发生变化时采取的操作
notify这个action可用于在每个play的最后被触发,这样可以避免有多次改变发生时,每次都执行指定的操作,取而代之仅在所有的变化发生完成以后一次性的执行指定操作
?
无论多少个task通知了相同的handler,handler仅会在所有tasks结束后运行一次
只有tasks发生改变了才会通知handler,没有改变则不会触发handler
不能使用handler替代tasks,因为handler是一个特殊的tasks

?在notify中列出的操作成为handler,即notify调用handler中定义的操作
在2中虽然使用标签单独更换了配置文件,但是没有重启,服务端口依旧使用的8080
cat httpd.conf |grep 8090
Listen 8090 ##刚才试验tag,如果不改端口,两边都是8089,copy过程相当于已经做过不会重复做,不会触发触发器

cat apache.yaml
---
- hosts: webserver
remote_user: root
tasks:
- name: install the latest version of apache
yum:
name: httpd
state: latest
- copy:
src: httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: apache
group: apache
mode: \'0644\'
tags: testtag
notify: apache restart #这个名字必须和触发器定义的名字一致才能找到触发器
- copy:
src: index.html
dest: /var/www/html/index.html
owner: apache
group: apache
mode: \'0644\'
- service:
name: httpd
state: started
enabled: yes
handlers:##触发器模块
- name: apache restart
service:
name: httpd
state: restarted

?ansible-playbook apache.yaml -i hosts --tags=testtag
此时可以直接观察数据
六ansible-playbook任务控制

文章图片

4、when
?有些时候需要在满足特定条件后再触发某一项操作,或在特定的条件下终止某个行为,这个时候需要进行条件判断,when正是解决这个问题的最佳选择,远程中的系统变量facts作为when的条件,可以通过setup模块查看
例1
cat httpd.conf |grep 8091
Listen 8091 ###再换个端口

cat apache.yaml
---
- hosts: webserver
remote_user: root
tasks:
- name: install the latest version of apache
yum:
name: httpd
state: latest
- copy:
src: httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: apache
group: apache
mode: \'0644\'
when: ansible_os_family == "Debian"##此处增加判断,如果操作系统是Debian,就执行这个copy
tags: testtag
notify: apache restart
- copy:
src: index.html
dest: /var/www/html/index.html
owner: apache
group: apache
mode: \'0644\'
- service:
name: httpd
state: started
enabled: yes
handlers:
- name: apache restart
service:
name: httpd
state: restarted

?ansible-playbook apache.yaml -i hosts --tags=testtag
六ansible-playbook任务控制

文章图片

?直接忽略了
此时更改when: ansible_os_family == "Debian"为when: ansible_os_family == "RedHat"
?ansible-playbook apache.yaml -i hosts --tags=testtag
六ansible-playbook任务控制

文章图片

例2 根据条件重启服务

[root@db2 memcache]# cat task3.yaml
- hosts: webserver
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
ignore_errors: yes##如果服务没启动,会报错,这里忽略报错
register: check_httpd

- name: Httpd Restarted
service: name=httpd state=restarted
when: check_httpd.rc == 0

ansible-playbook task3.yaml -i hosts
5、register
?有时候我们还需要更复杂的例子,比如判断前一个命令的执行结果去处理后面的操作,这时候就需要register模块来保存前一个命令的返回状态,在后面进行调用


cat when_test.yaml

---
- hosts: webserver
remote_user: root
tasks:
- shell: uptime |awk \'{printf("%.2f",$(NF-2))}\'
register: result
- debug:
var: result#先打印出来result信息,方便查看下面会不会自动关闭apache服务
- service:
name: httpd
state: stopped
when: result.stdout|float > 0.05 #当负载大于0.05,就关闭apache服务

6、with_items
?是playbook的标准循环,可用于迭代一个列表或字典,通过{{ item }}获取每次迭代的值
例1 批量创建用户
cat user.yaml
---
- hosts: webserver
remote_user: root
tasks:
- name: create user "{{ item.username }}"
user:
name: "{{ item.username }}"
group: "{{ item.group }}"
password: "{{item.password|password_hash( \'sha512\' )}}"
with_items:
-
username: yantai
group: shandong
password: "123456" ###如果为数字,必须加双引号标明为字符串
-
username: weihai
group: shandong
password: "123456"
-
username: weifang
group: shandong
password: abc

【六ansible-playbook任务控制】?ansible-playbook user.yaml -i hosts
六ansible-playbook任务控制

文章图片

例2 重启多个服务webserver机器已经安装apache和memcached
[root@db2 memcache]# cat task_4.yaml
- hosts: webserver
tasks:
- name: Service httpd Restarted
service: name={{ item }} state=restarted
# with_items:
loop:
- httpd
- memcached

ansible-playbook task_4.yaml -i hosts
例3 安装多个软件包
[root@db2 memcache]# cat task5.yaml
- hosts: webserver
tasks:
- name: Installed Httpd Memcached Package
yum: name={{ package }} state=present
vars:
package:
- httpd
- memcached

ansible-playbook task5.yaml -i hosts
7、include和include_tasks
基本一样,include_tasks执行会显示调用的yaml的实际路径,而include是透明的
[root@db2 memcache]# cat restart_httpd.yaml
- name: Restart httpd server
service: name=httpd state=restarted

[root@db2 memcache]# cat a_project.yaml
- hosts: webserver
tasks:
- name: A project command
command: echo "A"

- name: Restart httpdand memcached
include: restart_httpd.yaml

[root@db2 memcache]# cat b_project.yaml
- hosts: webserver
tasks:
- name: B project command
command: echo "B"
- name: Restart httpd memcached
include_tasks: restart_httpd.yaml

六ansible-playbook任务控制

文章图片

8、import_playbook和import_tasks
import_playbook的剧本(文件)必须完整包括hosts和tasks
[root@db2 memcache]# cat import_playbook.yaml
- name: this is import_play
hosts: webserver
tasks:
- name: install httpd
yum: name=httpd state=latest

[root@db2 memcache]# cat playbook.yaml
- name: import
import_playbook: import_playbook.yaml
- name: this is main_play
hosts: webserver
tasks:
- name: restart httpd
service: name=httpd state=restarted enabled=yes

import_tasks的剧本(文件)不需要hosts和tasks
?
[root@db2 memcache]# cat import_tasks.yaml
- name: install httpd
yum: name=httpd state=latest

[root@db2 memcache]# cat playbook.yaml
- name: this is main_play
hosts: webserver
tasks:
- name: import
import_tasks: import_tasks.yaml

9、changed_when
有些时候某些tasks只是去被控端获取一些数据,并不会对被控端做任何修改和变更,执行playbook的时候颜色还是黄色。
设置完后,再次执行,已经设置的tasks会显示绿色
tasks:
- shell: uptime |awk \'{printf("%.2f",$(NF-2))}\'
register: result
changed_when: false

检查nginx -t配置文件状态,如果结果有successful,则执行的时候不显示发生改变,即颜色为绿色
- name: Check nginx configure status
command: /usr/sbin/nginx -t
register: check_nginx
change_when:
- ( check_nginx.stdout.find(\'successful\'))
- false


    推荐阅读