How to Use Rescue Module When Package Fails in Ansible Playbook

ansibleansible-playbook

I have simple playbook where i am installing a bunch of packages in local system, expecting my playbook to come out from the loop when there is any failure during installation and go to rescue module for recovery. But my ansible playbook is trying to deploy all packages even tough there is an failure and playbook is calling the rescue module after iterating all packages.

It shouldn't iterate the second package if there is any failure in the first package.

install_deb.yml -> Playbook

---
- 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"

Current Output:

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

Expecting Output:
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

Best Answer

When i Modified the playbook as below, then it worked as expected.

---
- 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"

Tested Output:

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