# When Condition check is not working in a list as expected.

**URL:** <https://forum.ansible.com/t/when-condition-check-is-not-working-in-a-list-as-expected/29798>\
**Category:** Ansible Project\
**Created:** [February 28, 2019, 12:52pm UTC](https://forum.ansible.com/t/when-condition-check-is-not-working-in-a-list-as-expected/29798 "2019-02-28T12:52:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [February 28, 2019, 12:52pm UTC](https://forum.ansible.com/t/when-condition-check-is-not-working-in-a-list-as-expected/29798/1 "2019-02-28T12:52:31Z")

</div>

When Condition check is not working in a list as expected.

My requirement is

Playbook should not fail If all the three mentioned variables have “no\_share” value.  
But it’s failing not sure what causing this issue.

- debug: var=open\_shares\_info

- debug: var=linked\_dirs\_list

- debug: var=nfs\_hard\_mounts

- name: Fail Playbook Execution  
fail:  
msg: “Make sure to unshare/unmount all NFS shares in NFS clients”  
when: (“‘no\_share’ not in open\_shares\_info”) and (“‘no\_share’ not in linked\_dirs\_list”) and and (“‘no\_share’ not in nfs\_hard\_mounts”)

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [rchadm01] =\> {  
“open\_shares\_info”: “no\_share”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [rchadm01] =\> {  
“linked\_dirs\_list”: “no\_share”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [rchadm01] =\> {  
“nfs\_hard\_mounts”: “no\_share”  
}

TASK [Fail Playbook Execution] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [rchadm01]: FAILED! =\> {“changed”: false, “msg”: “Make sure to unshare/unmount all NFS shares in NFS clients”}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 28, 2019, 7:20pm UTC](https://forum.ansible.com/t/when-condition-check-is-not-working-in-a-list-as-expected/29798/2 "2019-02-28T19:20:26Z")

</div>

In Jinja template that when uses, quotes, single and double, means this is a string.  
So when you put a double quote around the hole expressing you are making it a sting.

In you when its

&nbsp;&nbsp;a\_string and a\_string and a\_string

Remove the double quotes and it should work. (and also remove the double and)

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [March 1, 2019, 4:01pm UTC](https://forum.ansible.com/t/when-condition-check-is-not-working-in-a-list-as-expected/29798/3 "2019-03-01T16:01:23Z")

</div>

Thank you, It worked.

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [March 1, 2019, 4:06pm UTC](https://forum.ansible.com/t/when-condition-check-is-not-working-in-a-list-as-expected/29798/4 "2019-03-01T16:06:17Z")

</div>

One more Requirement Kai,

I need to test the condition If stdout\_lines is not defined or the stdout\_lines length is 0, then open\_shares\_info variable should have the no\_share value.  
Could you check below syntax is correct, I am getting an error when I run it.

- set\_fact:  
open\_shares\_info: “{{ nfs\_open\_share.stdout\_lines if nfs\_open\_share.stdout\_lines|length != 0 or nfs\_open\_share.stdout\_lines is defined else ‘no\_share’ }}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 4, 2019, 11:12am UTC](https://forum.ansible.com/t/when-condition-check-is-not-working-in-a-list-as-expected/29798/5 "2019-03-04T11:12:15Z")

</div>

Why don't you just test it and see if it works.
