# How to execute a task only if a list of hosts haven't failed yet

**URL:** <https://forum.ansible.com/t/how-to-execute-a-task-only-if-a-list-of-hosts-havent-failed-yet/14884>\
**Category:** Ansible Project\
**Created:** [March 28, 2014, 1:37pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-only-if-a-list-of-hosts-havent-failed-yet/14884 "2014-03-28T13:37:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [March 28, 2014, 1:37pm UTC](https://forum.ansible.com/t/how-to-execute-a-task-only-if-a-list-of-hosts-havent-failed-yet/14884/1 "2014-03-28T13:37:53Z")

</div>

In out common role we have a task which creates a script from a template to create ssh tunnel between a certain number of hosts in the inventory. The template looks like this:

`  
{% for host in ssh\_tunnel %}  
{% if host != inventory\_hostname %}  
{% if hostvars[host][‘ansible\_default\_ipv4’] is defined %}  
{% for ports in hostvars[host][‘ssh\_tunnel\_ports’] %}  
su {{tunnel\_user\_name}} -c “autossh -p 22 -2 -fN -M 0 -L {{ports.local}}:127.0.0.1:{{ports.remote}} {{tunnel\_user\_name}}@{{hostvars[host][‘ansible\_default\_ipv4’][‘address’]}}”  
{% endfor %}  
{% endif %}  
{% endif %}  
{% endfor %}

`

So the current host should be tunnelling to all hosts in the hash ssh\_tunnel except to itself.

We’ve now had the scenario that from one mis-configured PC, one of the hosts wasn’t reachable in the Ansible run which caused a warning but of course the rest of the playbook just went ahead - as it should do. As that one host wasn’t reachable, the above loop excluded that host from the ssh tunnel script and that caused some services to crash, i.e. those who required that tunnel.

My question: the tunnel should not be updated if one of the hosts ins ssh\_tunnel already had failed. How can I include that in a “when:” statement in the task or is there a better way of achieving that level of reliability?
