# block/rescue to find out failed hosts with hostvar and when

**URL:** <https://forum.ansible.com/t/block-rescue-to-find-out-failed-hosts-with-hostvar-and-when/29129>\
**Category:** Ansible Project\
**Created:** [November 27, 2018, 6:49pm UTC](https://forum.ansible.com/t/block-rescue-to-find-out-failed-hosts-with-hostvar-and-when/29129 "2018-11-27T18:49:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hajott](https://avatars.discourse-cdn.com/v4/letter/h/e79b87/32.png) [@hajott](https://forum.ansible.com/u/hajott)\
**Post date:** [November 27, 2018, 6:49pm UTC](https://forum.ansible.com/t/block-rescue-to-find-out-failed-hosts-with-hostvar-and-when/29129/1 "2018-11-27T18:49:49Z")

</div>

Hello,

I try to figure out, how I can find the failed hosts in rescue.

My idea was to use:

- name: add failed hosts to the exclude list  
add\_host:  
name: “{{ item }}”  
groups: failed\_hosts\_in\_block  
when: hostvars:[item][‘ansible\_failed\_result’][‘failed’] == true  
with\_items: “{{ stored\_ansible\_hosts }}”

stored\_ansible\_hosts contains all ansible\_play\_hosts right at the beginning of the play.

The above code does not work, because when does not like the hostvar variable somehow 🙂 “conditional check errors”. I tried a lot of possible combinations

hostvars:[item]ansible\_failed\_result’.failed  
hostvars:[item][‘ansible\_failed\_result’].failed  
hostvars:[item][‘ansible\_failed\_result’][‘failed’]  
.  
.  
even:

- debug:  
var: hostvars[hostname].ansible\_failed\_result.failed

or

- debug:  
msg: “{{ hostvars[hostname].ansible\_failed\_result.failed }}”

gives the result “true”

For me, it is only important to find out, which host failed. There are a lot of tasks in the block statement and it makes no sense to write error handling for each single task, register the return code and take action on this one. Also adding these hostnames to a list failed (thought that failed hosts could not be added to a host group…)

I hope you can help!

Regards
