# stop task on all hosts when task is finished in any of them

**URL:** <https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011>\
**Category:** Ansible Project\
**Created:** [November 13, 2017, 10:49pm UTC](https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011 "2017-11-13T22:49:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivo\_Jimenez](https://avatars.discourse-cdn.com/v4/letter/i/c0e974/32.png) [@Ivo\_Jimenez](https://forum.ansible.com/u/Ivo_Jimenez)\
**Post date:** [November 13, 2017, 10:49pm UTC](https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011/1 "2017-11-13T22:49:01Z")

</div>

I’ve been trying to find a way of accomplishing this (what the subject says). I thought on using blocks, for example:

`

- block:
- name: run task  
command:   
register: command\_result  
failed\_when: command\_result.changed  
rescue:
- name: stop task on all other nodes  
command:   
delegate\_to: ‘{{ item }}’  
with\_items: ‘{{ groups.all }}’  
`

Can you think of any other alternative?

Thanks!

---

<div class="post-metadata">

**Author:** ![Ivo\_Jimenez](https://avatars.discourse-cdn.com/v4/letter/i/c0e974/32.png) [@Ivo\_Jimenez](https://forum.ansible.com/u/Ivo_Jimenez)\
**Post date:** [November 14, 2017, 12:06am UTC](https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011/2 "2017-11-14T00:06:51Z")

</div>

Forgot to mention that the reason what the above doesn’t work is because the `rescue` tasks are executed only after _all_ the hosts have finished executing the task

---

<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, 2017, 2:41pm UTC](https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011/3 "2017-11-14T14:41:10Z")

</div>

Maybe strategy: free will help.  
[https://docs.ansible.com/ansible/latest/playbooks\_strategies.html#strategies](https://docs.ansible.com/ansible/latest/playbooks_strategies.html#strategies)

---

<div class="post-metadata">

**Author:** ![Ivo\_Jimenez](https://avatars.discourse-cdn.com/v4/letter/i/c0e974/32.png) [@Ivo\_Jimenez](https://forum.ansible.com/u/Ivo_Jimenez)\
**Post date:** [November 14, 2017, 6:14pm UTC](https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011/4 "2017-11-14T18:14:08Z")

</div>

Thanks for the suggestion. The problem is that I have other tasks (like local\_action) that are not compatible with ‘strategy: free’

---

<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, 2017, 7:32pm UTC](https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011/5 "2017-11-14T19:32:48Z")

</div>

To my knowledge strategy free and local\_action is not a problem, do mean that you task is not compatible?

If so you are pretty much out of luck since strategy linear work by running all task on all host before advancing to the next.  
One thing could be to split the play in three.  
One play with linear, one with free that only run that particular task, and then the last play with linear again.

---

<div class="post-metadata">

**Author:** ![Ivo\_Jimenez](https://avatars.discourse-cdn.com/v4/letter/i/c0e974/32.png) [@Ivo\_Jimenez](https://forum.ansible.com/u/Ivo_Jimenez)\
**Post date:** [November 14, 2017, 9:26pm UTC](https://forum.ansible.com/t/stop-task-on-all-hosts-when-task-is-finished-in-any-of-them/26011/6 "2017-11-14T21:26:10Z")

</div>

Sorry, I meant ‘run\_once’ (it’s incompatible because every host is going on independently). I think I’ll go with having multiple playbooks. Thanks!
