1、block rescue always
playbook中的block块里的任务在执行的时候,如果有任何错误,将执行rescue中的任务;无论在block和rescue中发生或没有发生错误,always部分都运行;若在block加上失败条件判断,将原执行成功之后的结果改为失败,则resuce也会执行。
2、示例
创建LVM
- name: create lvm
hosts: all
tasks:
- name: create logical volume
block:
- name: create lvm 1500m
lvol:
vg: research
lv: data
size: 1500m
rescue:
- name: output fail msg
debug:
msg: Could not create logical volume of that size
- name: create lvm 800m
lvol:
vg: research
lv: data
size: 800m
always:
- name: format lvm
filesystem:
fstype: ext4
dev: /dev/research/data
when: "'research' in ansible_lvm.vgs"
- name: search not exists
debug:
msg: Volume group does not exist
when: "'research' not in ansible_lvm.vgs"
执行结果
PLAY [create lvm] ******************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************
ok: [servera]
ok: [serverb]
ok: [serverd]
ok: [serverc]
ok: [bastion]
TASK [create lvm 1500m] ************************************************************************************************************************************************************
skipping: [bastion]
fatal: [serverd]: FAILED! => {"changed": false, "err": " Volume group \"research\" has insufficient free space (255 extents): 375 required.\n", "msg": "Creating logical volume 'data' failed", "rc": 5}
fatal: [serverc]: FAILED! => {"changed": false, "err": " Volume group \"research\" has insufficient free space (255 extents): 375 required.\n", "msg": "Creating logical volume 'data' failed", "rc": 5}
changed: [serverb]
changed: [servera]
#当block中有任务执行失败后,执行rescue中的任务
TASK [output fail msg] *************************************************************************************************************************************************************
ok: [serverc] => {
"msg": "Could not create logical volume of that size"
}
ok: [serverd] => {
"msg": "Could not create logical volume of that size"
}
TASK [create lvm 800m] *************************************************************************************************************************************************************
changed: [serverc]
changed: [serverd]
#always中的任务无论有无错误都执行
TASK [format lvm] ******************************************************************************************************************************************************************
skipping: [bastion]
changed: [serverd]
changed: [serverc]
changed: [serverb]
changed: [servera]
TASK [search not exists] ***********************************************************************************************************************************************************
ok: [servera] => {
"msg": "Volume group does not exist"
}
ok: [serverb] => {
"msg": "Volume group does not exist"
}
skipping: [bastion]
ok: [serverc] => {
"msg": "Volume group does not exist"
}
ok: [serverd] => {
"msg": "Volume group does not exist"
}
PLAY RECAP *************************************************************************************************************************************************************************
bastion : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
servera : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverb : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverc : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
serverd : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0