# command/shell + with\_items ignores failures

**URL:** <https://forum.ansible.com/t/command-shell-with-items-ignores-failures/28236>\
**Category:** Ansible Developer\
**Created:** [August 2, 2018, 3:01pm UTC](https://forum.ansible.com/t/command-shell-with-items-ignores-failures/28236 "2018-08-02T15:01:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Urushkin](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Sergey\_Urushkin](https://forum.ansible.com/u/Sergey_Urushkin)\
**Post date:** [August 2, 2018, 3:01pm UTC](https://forum.ansible.com/t/command-shell-with-items-ignores-failures/28236/1 "2018-08-02T15:01:34Z")

</div>

Hello,  
I need to run many shell commands through loop (commands are passed through environment variable, separated by commas). The problem is that I need to stop on error immediately without running subsequent commands. I’m talking about  
[https://github.com/ansible/ansible-modules-core/issues/1312](https://github.com/ansible/ansible-modules-core/issues/1312)  
It was closed with the words: “If you want the behaviour of any of the commands failing the play, you need to put them into separate tasks.” . But what if I need to run hundreds of commands with identical environment/parameters, shouldn’t it be easier to support loops in this way?  
Also, another mentioned solution - running tests afterwards isn’t acceptable, since running subsequent commands could completely broke environment (curl test → applying to production), and remember I have hundreds of commands, and they are passed through variable. You could see another examples in the bug report.

So, the question: is there a way to implement this:

- hosts: localhost  
tasks:
- shell: “{{item}}”  
with\_items:
- exit 0
- exit 1
- echo “Should not be run”

And if there is no way, I’m suggesting to add another loop\_control parameter like “immediate\_fail”, which will control this aspect of loops behavior (false by default, as it behaves now). Should I file a bug report than?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 2, 2018, 3:05pm UTC](https://forum.ansible.com/t/command-shell-with-items-ignores-failures/28236/2 "2018-08-02T15:05:25Z")

</div>

I _think_ something like the following will work:

- hosts: localhost  
tasks:
- shell: “{{item}}”  
with\_items:
- exit 0
- exit 1
- echo “Should not be run”  
register: result  
when: result|default({}) is not failed

It should cause everything after the failure to be skipped.

---

<div class="post-metadata">

**Author:** ![Sergey\_Urushkin](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Sergey\_Urushkin](https://forum.ansible.com/u/Sergey_Urushkin)\
**Post date:** [August 2, 2018, 3:10pm UTC](https://forum.ansible.com/t/command-shell-with-items-ignores-failures/28236/3 "2018-08-02T15:10:13Z")

</div>

Thanks a lot, that seems to work!
