我有一个简单的 playbook,用于在本地系统中安装一堆软件包,希望当安装过程中出现任何故障时,我的 playbook 能够退出循环并转到救援模块进行恢复。但是我的 ansible playbook 试图部署所有软件包,即使出现故障,并且 playbook 在迭代所有软件包后调用救援模块。
如果第一个包出现任何故障,它就不应该迭代第二个包。
install_deb.yml->剧本
---
- hosts: localhost
tasks:
- name: Deploy all packages
block:
- name: installing debian packages
apt:
deb: "{{ item }}"
with_items:
- /home/playbook/sample1.deb
- /home/playbook/sample2.deb
when: ansible_distribution == "Ubuntu"
rescue:
- name: Deployment of debian package is Failed
debug: msg="There was Failure installing Package"
电流输出:
ansible-playbook installdeb.yml
PLAY [SSHFS] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [localhost]
TASK [installing debian packages] *****************************************************************************************************************************************************************************
[WARNING]: Could not find aptitude. Using apt-get instead
failed: [localhost] (item=/home/playbook/sample1.deb) => {"ansible_loop_var": "item", "changed": false, "item": "/home/playbook/sample1.deb", "msg": "Unable to install package: E:read, still have 8 to read but none left"}
failed: [localhost] (item=/home/playbook/sample2.deb) => {"ansible_loop_var": "item", "changed": false, "item": "/home/playbook/sample2.deb", "msg": "Unable to install package: E:read, still have 8 to read but none left"}
TASK [Deployment of debian package is Failed] *****************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "There was Failure installing Package"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
预期输出: ansible-playbook installdeb.yml
PLAY [SSHFS] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [localhost]
TASK [installing debian packages] *****************************************************************************************************************************************************************************
[WARNING]: Could not find aptitude. Using apt-get instead
failed: [localhost] (item=/home/playbook/sample1.deb) => {"ansible_loop_var": "item", "changed": false, "item": "/home/playbook/sample1.deb", "msg": "Unable to install package: E:read, still have 8 to read but none left"}
TASK [Deployment of debian package is Failed] *****************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "There was Failure installing Package"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
答案1
当我按如下方式修改剧本时,它就按预期工作了。
---
- hosts: localhost
tasks:
- name: Deploy all packages
block:
- name: installing debian packages
apt:
deb: "{{ item }}"
register: apt_output
with_items:
- /home/playbook/sample1.deb
- /home/playbook/sample2.deb
when: (not (apt_output |default({})) is failed) and (ansible_distribution == "Ubuntu")
rescue:
- name: Deployment of debian package is Failed
debug: msg="There was Failure installing Package"
测试输出:
ansible-playbook installdeb.yml
PLAY [SSHFS] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [localhost]
TASK [installing debian packages] *****************************************************************************************************************************************************************************
[WARNING]: Could not find aptitude. Using apt-get instead
failed: [localhost] (item=/home/playbook/sample1.deb) => {"ansible_loop_var": "item", "changed": false, "item": "/home/playbook/sample1.deb", "msg": "Unable to install package: E:read, still have 8 to read but none left"}
skipping: [localhost] => (item=/home/playbook/sample2.deb)
TASK [Deployment of debian package is Failed] *****************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Failu in Debian Package"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0