# Ansible "failed\_when" not working as desired

**URL:** <https://forum.ansible.com/t/ansible-failed-when-not-working-as-desired/32405>\
**Category:** Ansible Project\
**Created:** [March 6, 2020, 12:46pm UTC](https://forum.ansible.com/t/ansible-failed-when-not-working-as-desired/32405 "2020-03-06T12:46:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [March 6, 2020, 12:46pm UTC](https://forum.ansible.com/t/ansible-failed-when-not-working-as-desired/32405/1 "2020-03-06T12:46:58Z")

</div>

Hi,

I have a task as follows:

- hosts: nodes  
gather\_facts: true  
tasks:
- name: CurlCP Status  
shell: “curl -Is -m 1 [http://localhost:9113/cp/healthcheck](http://localhost:9113/cp/healthcheck)”  
register: cp\_status  
failed\_when: “‘200 OK’ not in cp\_status.stdout\_lines”

It fails even if 200 Ok is in o/p. I want to fail the job only when 200 OK is not in o/p  
Pls help here

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 6, 2020, 1:25pm UTC](https://forum.ansible.com/t/ansible-failed-when-not-working-as-desired/32405/2 "2020-03-06T13:25:46Z")

</div>

Try

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failed\_when: cp\_status.stdout\_lines|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select('search', '200 OK')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length == 0

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 6, 2020, 1:28pm UTC](https://forum.ansible.com/t/ansible-failed-when-not-working-as-desired/32405/3 "2020-03-06T13:28:37Z")

</div>

Why not use the uri module instead of fragile shell + curl ?
