# Block and Rescue behavior question

**URL:** <https://forum.ansible.com/t/block-and-rescue-behavior-question/31659>\
**Category:** Ansible Project\
**Created:** [November 14, 2019, 3:15pm UTC](https://forum.ansible.com/t/block-and-rescue-behavior-question/31659 "2019-11-14T15:15:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcronenworth](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@mcronenworth](https://forum.ansible.com/u/mcronenworth)\
**Post date:** [November 14, 2019, 3:15pm UTC](https://forum.ansible.com/t/block-and-rescue-behavior-question/31659/1 "2019-11-14T15:15:48Z")

</div>

Is a “rescue” in a “block” supposed to run at the very end of a play?

The documentation and examples would lead you to believe that a rescue is supposed to happen immediately following a block failure. I am not seeing that behavior though.

Example:

- task\_module\_1:

- block:

- task\_module\_2:  
loop: items

rescue:

- task\_module\_3:

- task\_module\_4:

The output is the following:

task\_module\_1  
task\_module\_2 - fails

task\_module\_4  
task\_module\_3

Thanks,  
Michael

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 14, 2019, 3:32pm UTC](https://forum.ansible.com/t/block-and-rescue-behavior-question/31659/2 "2019-11-14T15:32:24Z")

</div>

> Is a "rescue" in a "block" supposed to run at the very end of a play?

No.

> The documentation and examples would lead you to believe that a rescue is  
> supposed to happen immediately following a block failure.

That's correct.

> I am not seeing that behavior though.

I'm do.

playbook:  
- hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="first"  
&nbsp;&nbsp;&nbsp;&nbsp;- block:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="second"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- fail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="third"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="fourth"

Output:

localhost | SUCCESS =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "first"  
}  
localhost | SUCCESS =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "second"  
}  
localhost | FAILED! =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "Failed as requested from task"  
}  
localhost | SUCCESS =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "third"  
}  
localhost | SUCCESS =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "fourth"  
}

---

<div class="post-metadata">

**Author:** ![sdoran](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@sdoran](https://forum.ansible.com/u/sdoran)\
**Post date:** [November 14, 2019, 3:33pm UTC](https://forum.ansible.com/t/block-and-rescue-behavior-question/31659/3 "2019-11-14T15:33:27Z")

</div>

Michael,

That is correct: rescue tasks run immediately after the task that fails. I'm unable to duplicate the behavior you're seeing.

---

<div class="post-metadata">

**Author:** ![mcronenworth](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@mcronenworth](https://forum.ansible.com/u/mcronenworth)\
**Post date:** [November 14, 2019, 7:30pm UTC](https://forum.ansible.com/t/block-and-rescue-behavior-question/31659/4 "2019-11-14T19:30:41Z")

</div>

I’ve been able to reproduce it with a small test case. The issue is with multiple hosts using a real inventory (localhost is fine) and the block/rescue must be inside a role.

inventory:  
[test\_inv]  
hostA  
hostB

playbook:

- hosts: test\_inv  
gather\_facts: false  
vars:  
test\_list:
- echoa
- hecob
- echoc
- echod  
tasks:
- import\_role:  
name: test

role test:

- shell: echo second

- block:

- when:

- not ansible\_check\_mode

- item != ‘echoc’

- inventory\_hostname != ‘hostA’  
shell: “{{ item }}”  
changed\_when: false  
register: block\_var  
loop: “{{ test\_list }}”  
rescue:

- when:

- block\_var.skipped is not defined

- block\_var.failed  
shell: echo fourth  
changed\_when: false

- when:

- item != ‘echoc’

- inventory\_hostname != ‘hostA’  
shell: “{{ item }}”  
loop: “{{ test\_list }}”

- shell: echo sixth

The ‘echo sixth’ task is _not_ the last task that is run. Is this expected when run inside a role?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 15, 2019, 11:41pm UTC](https://forum.ansible.com/t/block-and-rescue-behavior-question/31659/5 "2019-11-15T23:41:52Z")

</div>

It's a bug with import\_role and include\_role.  
I made test code that only run against localhost and somewhat easier to follow.

inventory

---

<div class="post-metadata">

**Author:** ![mcronenworth](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@mcronenworth](https://forum.ansible.com/u/mcronenworth)\
**Post date:** [November 18, 2019, 3:00pm UTC](https://forum.ansible.com/t/block-and-rescue-behavior-question/31659/6 "2019-11-18T15:00:55Z")

</div>

Thanks. I stole your test case and opened a bug report.

[https://github.com/ansible/ansible/issues/64999](https://github.com/ansible/ansible/issues/64999)
