# Rescue all hosts if one host fails

**URL:** <https://forum.ansible.com/t/rescue-all-hosts-if-one-host-fails/26062>\
**Category:** Ansible Project\
**Created:** [November 17, 2017, 1:52pm UTC](https://forum.ansible.com/t/rescue-all-hosts-if-one-host-fails/26062 "2017-11-17T13:52:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Falk](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Aaron\_Falk](https://forum.ansible.com/u/Aaron_Falk)\
**Post date:** [November 17, 2017, 1:52pm UTC](https://forum.ansible.com/t/rescue-all-hosts-if-one-host-fails/26062/1 "2017-11-17T13:52:59Z")

</div>

There is no possibility to run the rescue part for all hosts if one host fails. I’m working on my deployment for a website. I want to deploy the site to multiple hosts. If something fails for one host than all hosts should not be deployed. Therefore, a rollback on all hosts should happen. The configuration “any\_errors\_fatal” is not working for it.

I am using ansible 2.4.1.0

Here the steps to reproduce:

`

```auto
- block:
    - name: Update database schema
      shell: cd '{{ ansible_web_deployment_webroot }}/releases/{{ ansible_web_deployment_release }}' && php7.1 console.php orm:schema-tool:update --force
      when: inventory_hostname in groups['schema']
    - name: Deployment
      debug:
        msg: 'Deploying'
  rescue:
    - name: Rollback
      debug:
        msg: 'Rollback'
  any_errors_fatal: true

```

`

Expected results:

`

```auto
PLAY [test] ********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [x1.example.com]
ok: [x2.example.com]
ok: [x3.example.com]
ok: [x4.example.com]

TASK [ansible_web_deployment : Update database schema] *************************
skipping: [x2.example.com]
skipping: [x3.example.com]
skipping: [x4.example.com]
fatal: [x1.example.com]: FAILED! => {"changed": true, "cmd": "cd '/var/www/x.example.com/releases/20171113081452' && php7.1 console.php orm:schema-tool:update --force", "delta": "0:00:00.007578", "end": "2017-11-13 08:15:05.709148", "failed": true, "msg": "non-zero return code", "rc": 2, "start": "2017-11-13 08:15:05.701570", "stderr": "/bin/sh: 1: cd: can't cd to /var/www/x.example.com/releases/20171113081452", "stderr_lines": ["/bin/sh: 1: cd: can't cd to /var/www/x.example.com/releases/20171113081452"], "stdout": "", "stdout_lines": []}

TASK [ansible_web_deployment : Rollback] *****************************
ok: [x1.example.com] => {
    "msg": "Rollback"
}
ok: [x2.example.com] => {
    "msg": "Rollback"
}
ok: [x2.example.com] => {
    "msg": "Rollback"
}
ok: [x4.example.com] => {
    "msg": "Rollback"
}

PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0
x1.example.com : ok=2 changed=1 unreachable=0 failed=1
x2.example.com : ok=2 changed=1 unreachable=0 failed=1
x3.example.com : ok=2 changed=1 unreachable=0 failed=1
x4.example.com : ok=2 changed=1 unreachable=0 failed=1

```

`

Actual results:

`

```auto
PLAY [test] ********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [x1.example.com]
ok: [x2.example.com]
ok: [x3.example.com]
ok: [x4.example.com]

TASK [ansible_web_deployment : Update database schema] *************************
skipping: [x2.example.com]
skipping: [x3.example.com]
skipping: [x4.example.com]
fatal: [x1.example.com]: FAILED! => {"changed": true, "cmd": "cd '/var/www/x.example.com/releases/20171113081452' && php7.1 console.php orm:schema-tool:update --force", "delta": "0:00:00.007578", "end": "2017-11-13 08:15:05.709148", "failed": true, "msg": "non-zero return code", "rc": 2, "start": "2017-11-13 08:15:05.701570", "stderr": "/bin/sh: 1: cd: can't cd to /var/www/x.example.com/releases/20171113081452", "stderr_lines": ["/bin/sh: 1: cd: can't cd to /var/www/x.example.com/releases/20171113081452"], "stdout": "", "stdout_lines": []}

TASK [ansible_web_deployment : Restart services] *****************************
ok: [x2.example.com] => {
    "msg": "Deploying"
}
ok: [x2.example.com] => {
    "msg": "Deploying"
}
ok: [x4.example.com] => {
    "msg": "Deploying"
}

TASK [ansible_web_deployment : Rollback] *****************************
ok: [x1.example.com] => {
    "msg": "Rollback"
})

PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0
x1.example.com : ok=2 changed=1 unreachable=0 failed=1
x2.example.com : ok=2 changed=1 unreachable=0 failed=0
x3.example.com : ok=2 changed=1 unreachable=0 failed=0
x4.example.com : ok=2 changed=1 unreachable=0 failed=0

```

`

How can I achieve that all hosts go in rescue mode when a single host throws an exception?

If you have a look at the [Github issue 14024](https://github.com/ansible/ansible/issues/14024), you’ll see that this should work.  
Unfortunately I was not able to get it to work.

Thank you for your input.

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [November 17, 2017, 9:26pm UTC](https://forum.ansible.com/t/rescue-all-hosts-if-one-host-fails/26062/2 "2017-11-17T21:26:40Z")

</div>

Hi

You can use the following method (sorry I don’t have the playbook ready).

1. Run the upgrade on each host, ignore errors, but register the results and store them in a file on the controller machine (you can also manually - fail: once the error is registered for a given host)
2. Once all upgrades are done on all hosts - check the content of the ‘results’ files on the controller. If any of them show issues - run another role to revert the changes.

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![amit\_ahuja](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@amit\_ahuja](https://forum.ansible.com/u/amit_ahuja)\
**Post date:** [November 20, 2018, 1:06pm UTC](https://forum.ansible.com/t/rescue-all-hosts-if-one-host-fails/26062/3 "2018-11-20T13:06:42Z")

</div>

Hi Aaron,

did you managed to have ansible rollback deployment on all hosts when anyone failed in any task ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 20, 2018, 2:29pm UTC](https://forum.ansible.com/t/rescue-all-hosts-if-one-host-fails/26062/4 "2018-11-20T14:29:14Z")

</div>

I can think of a couple of ways.

You can force a fail at the end of block if any hosts failed:  
- block:  
&nbsp;&nbsp;&nbsp;- name: Update database schema  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: cd '{{ ansible\_web\_deployment\_webroot }}/releases/{{  
ansible\_web\_deployment\_release }}' && php7.1 console.php  
orm:schema-tool:update --f  
orce  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups['schema']  
&nbsp;&nbsp;&nbsp;- name: Deployment  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: 'Deploying'  
&nbsp;&nbsp;&nbsp;- fail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_play\_hosts|length != ansible\_play\_hosts\_all|length  
rescue:  
&nbsp;&nbsp;&nbsp;- name: Rollback  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: 'Rollback'  
any\_errors\_fatal: true

Or delegate rescue to all hosts:

- block:  
&nbsp;&nbsp;&nbsp;- name: Update database schema  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: cd '{{ ansible\_web\_deployment\_webroot }}/releases/{{  
ansible\_web\_deployment\_release }}' && php7.1 console.php  
orm:schema-tool:update --f  
orce  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups['schema']  
&nbsp;&nbsp;&nbsp;- name: Deployment  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: 'Deploying'  
rescue:  
&nbsp;&nbsp;&nbsp;- name: Rollback  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: 'Rollback'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: '{{item}}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: '{{ ansible\_play\_hosts\_all }}'  
any\_errors\_fatal: true
