# using failed\_when in a with\_ loop

**URL:** <https://forum.ansible.com/t/using-failed-when-in-a-with-loop/13883>\
**Category:** Ansible Project\
**Created:** [November 11, 2013, 5:45pm UTC](https://forum.ansible.com/t/using-failed-when-in-a-with-loop/13883 "2013-11-11T17:45:45Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [November 11, 2013, 5:45pm UTC](https://forum.ansible.com/t/using-failed-when-in-a-with-loop/13883/1 "2013-11-11T17:45:45Z")

</div>

Dear List,

I’m looking into implementing a kind of application health check after deployment. My applications are defined per cluster group with a complex var of following kind:

apps:

- port: 8080  
path: /app  
monitors:
- get: /app/healthcheck  
receive: Status up
- …
- …

I already use this to configure checks on the load-balancer, and now want to also use the same data to check it after my ansible deploy playbooks.

I can easily run something like

- task: check  
module: uri … → item.1.get  
with\_subelements:
- apps
- monitors

But ideally I’d like to add a failed\_when: and check that item.1.receive is found in the uri’s returned content string. But that’s not possible, as failed\_when doesn’t work on the separate iterations of the loop, only the consolidated task. What would be the best option to solve this? Right now, I can think of the following options, which need a code update:

- somehow update ansible to (optionally) allow to have failed\_when work on the separate iterations
- register the task, then make the check in a separate task - but this cannot work in current ansible, as I would need to loop through the same subelements _and_ the previously registered variable (this is however another use case for nesting lookup plugins as per my PR #4627)
- extend the uri plugin to succeed or fail not only on the return\_code, but also optionally on a given string to be found in the returned content.

Which of these options are acceptable?  
Does anyone see another option, maybe one that can be implemented with the current ansible code base?

Thanks,

Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 12, 2013, 3:54pm UTC](https://forum.ansible.com/t/using-failed-when-in-a-with-loop/13883/2 "2013-11-12T15:54:07Z")

</div>

I don’t like the idea of having two ways that failed\_when works as that seems confusing to me.

As you just have two tasks here, I’d probably just split these things up into two explicit tasks for now.
