block 可以将多个task任务组合在一起执行,支持when判断,支持类似python中的try 语句。
python 语法举例:
try:
print(1/0)
except:
print(2)
finally:
print(3)
block 举例:
rescue 字面意识救援
- hosts: all
tasks:
- block:
- name: restart docker
service:
name: docker
state: restarted
rescue:
- name: 如果失败执行我
debug:
msg: "block 中的任务失败了,现在该执行我rescue"
always:
- name: check the docker service
shell:
systemctl status docker
register: result
when:
- ansible_os_family == "RedHat"
- ansible_distribution_major_version == "7"
- name: debug
debug:
msg: docker 服务在运行
when: "'running' in result.stdout"
标签:name,when,print,docker,block,rescue
From: https://www.cnblogs.com/wangend/p/17066893.html