# Help needed regarding when condition

**URL:** <https://forum.ansible.com/t/help-needed-regarding-when-condition/33480>\
**Category:** Ansible Project\
**Created:** [July 24, 2020, 6:59am UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480 "2020-07-24T06:59:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [July 24, 2020, 6:59am UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/1 "2020-07-24T06:59:00Z")

</div>

Hi Ansible Gurus,

I have this output:  
 (1, ‘\r\n{“msg”: “service[component] - [https://myip/myrest/v1/](https://myip/myrest/v1/) fails to due to error 501 - None”, “failed”: true, “invocation”: {“module\_args”: {“url\_password”: null, “tasks”: [{“name”: null, “hostname”: “myapp”, “timeout”: “60”, “my\_post\_action”: “component”, “operation”: "shut, “instance”: “abc”}], “force”: false, “homedir”: null, “service”: null, “url”: null, “force\_basic\_auth”: false, “http\_agent”: “ansible-httpget”, “cluster”: “homedir”, “state”: “present”, “BaseUrl”: “[https://myapp/myrest/api/v1](https://myapp/myrest/api/v1)”, “url\_username”: null, “client\_key”: null, “validate\_certs”: false, “client\_cert”: null, “use\_proxy”: true}}}\r\n’, ‘Shared connection to myapp closed.\r\n’)

But when i put condition on a registered variable  
when : result is not failed ,  
it says :  
The ‘failed’ test expects a dictionary\n Error how can i put this condition then ?

Rahul

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 24, 2020, 7:04am UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/2 "2020-07-24T07:04:22Z")

</div>

> Hi Ansible Gurus,  
> I have this output:  
> \<myapp\> (1, '\r\n{"msg": "service[component] - [https://myip/myrest/v1/](https://myip/myrest/v1/) fails to due to error 501 - None", "failed":  
> true, "invocation": {"module\_args": {"url\_password": null, "tasks": [{"name": null, "hostname": "myapp", "timeout":  
> "60", "my\_post\_action": "component", "operation": "shut, "instance": "abc"}], "force": false, "homedir": null,  
> "service": null, "url": null, "force\_basic\_auth": false, "http\_agent": "ansible-httpget", "cluster": "homedir", "state":  
> "present", "BaseUrl": "[https://myapp/myrest/api/v1&quot;](https://myapp/myrest/api/v1&quot;), "url\_username": null, "client\_key": null, "validate\_certs": false,  
> "client\_cert": null, "use\_proxy": true}}}\r\n', 'Shared connection to myapp closed.\r\n')
> 
> But when i put condition on a registered variable  
> when : result is not failed ,  
> it says :  
> The 'failed' test expects a dictionary\n Error how can i put this condition then ?
> 
> Rahul

Please share the relevant details about your playbook / task / inventory.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [July 24, 2020, 7:07am UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/3 "2020-07-24T07:07:45Z")

</div>

Here it is :

- name: my service shut  
my\_service:  
baseUrl: “{{ base\_rest\_url }}”  
tasks:
- action: service\_shut  
operation: shut  
hostname: “{{ ansible\_fqdn }}”  
instance: “{{ item.instance }}”  
state: present  
register: result  
with\_items: “{{ my\_instances\_config }}”  
when: result is not failed

here my\_service is custom python module !

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 24, 2020, 7:11am UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/4 "2020-07-24T07:11:36Z")

</div>

> Here it is :  
> - name: my service shut  
> my\_service:  
> baseUrl: "{{ base\_rest\_url }}"  
> tasks:  
> - action: service\_shut  
> operation: shut  
> hostname: "{{ ansible\_fqdn }}"  
> instance: "{{ item.instance }}"  
> state: present  
> register: result  
> with\_items: "{{ my\_instances\_config }}"  
> when: result is not failed
> 
> here my\_service is custom python module !

Hello Rahul,

the condition is check for each item in "my\_instances\_config" \*before\* running the task.

So it doesn't make sense to use "result" there as it is registered \*after\* running the task.

What is the idea behind your condition?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [July 24, 2020, 7:14am UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/5 "2020-07-24T07:14:00Z")

</div>

my idea is: i don’t want to stop the execution if this rest call is failed ! what i want that if this task failed , it should be skipped rather than failing entire execution !  
Notice i dont want to use ignore\_error kind of then , i want something conditional !

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 24, 2020, 7:34am UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/6 "2020-07-24T07:34:30Z")

</div>

> my idea is: i don't want to stop the execution if this rest call is failed ! what i want that if this task failed , it  
> should be skipped rather than failing entire execution !  
> Notice i dont want to use ignore\_error kind of then , i want something conditional !

Hello Rahul,

you can override the failed state with failed\_when instead of when, e.g.

failed\_when: false

However that doesn't make sense. You should fix the problem in your API / in calling your API.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [July 24, 2020, 12:22pm UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/7 "2020-07-24T12:22:44Z")

</div>

ok thanks ! I need one more help:

suppose i have a statement like :  
until: result.failed == False and result is succeeded and result.content.allstarted in a task !

But i get error saying dict has no attribute allstarted ! Actually that is true also because this attribute comes only when result.failed == false ! But here until is trying to evaluate all 3 conditions together ! Is there any ways in which we can say that evaluate result.content.allstarted only when result.failed== false in until clause !

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [July 24, 2020, 12:25pm UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/8 "2020-07-24T12:25:07Z")

</div>

sorry evaluate result.content.allstarted only when result.failed == true

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 24, 2020, 1:46pm UTC](https://forum.ansible.com/t/help-needed-regarding-when-condition/33480/9 "2020-07-24T13:46:47Z")

</div>

> sorry evaluate result.content.allstarted only when result.failed == true

As I said, fix the API problems instead of going into a series of rabbit holes with these conditions on the Ansible side.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
