command/shell + with_items ignores failures

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
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?

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.

Thanks a lot, that seems to work!