# Breaking a loop

**URL:** <https://forum.ansible.com/t/breaking-a-loop/15410>\
**Category:** Ansible Project\
**Created:** [June 5, 2014, 4:04pm UTC](https://forum.ansible.com/t/breaking-a-loop/15410 "2014-06-05T16:04:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dario](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@Dario](https://forum.ansible.com/u/Dario)\
**Post date:** [June 5, 2014, 4:04pm UTC](https://forum.ansible.com/t/breaking-a-loop/15410/1 "2014-06-05T16:04:57Z")

</div>

Hello,

I have a loop that loops over a result from program execution. On every iteration a shell command is executed. However, it may happen that one of these shell commands fails. Is possible to prevent continuing the loop if this happens? From what I can see, other iterations are executed even though one of them failed.

Thanks.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 5, 2014, 4:20pm UTC](https://forum.ansible.com/t/breaking-a-loop/15410/2 "2014-06-05T16:20:07Z")

</div>

That’s odd, the task should fail unless you’re specifying other failed\_when conditions or retries. Could you share an example of the task you’re executing?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [June 5, 2014, 6:36pm UTC](https://forum.ansible.com/t/breaking-a-loop/15410/3 "2014-06-05T18:36:43Z")

</div>

​No, the task as a whole will fail ​if one of the iterations fails, but  
even if one iteration fails, the other iterations in that same task still  
get executed.

I don't think failed\_when will impact on iterations, only the task as a  
whole.

&nbsp;&nbsp;Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 6, 2014, 12:41pm UTC](https://forum.ansible.com/t/breaking-a-loop/15410/4 "2014-06-06T12:41:01Z")

</div>

Serge is correct in the above.

The registration of a loop variable results in a list being stored.
