# Multiple conditions not working

**URL:** <https://forum.ansible.com/t/multiple-conditions-not-working/27642>\
**Category:** Ansible Project\
**Created:** [May 27, 2018, 3:51pm UTC](https://forum.ansible.com/t/multiple-conditions-not-working/27642 "2018-05-27T15:51:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranay\_Nanda](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pranay\_Nanda](https://forum.ansible.com/u/Pranay_Nanda)\
**Post date:** [May 27, 2018, 3:51pm UTC](https://forum.ansible.com/t/multiple-conditions-not-working/27642/1 "2018-05-27T15:51:15Z")

</div>

I want to run few tasks in shell if they satisfy a condition. Strangely the task runs if the second condition is satisfied even though the first one fails.

name: ods  
shell: “/something {{state}}”

when: ((ods) and (not (ansible\_hostname | search(“demlh\*”))))  
register: sss\_ods\_out  
notify: output ods  
ignore\_errors: yes

Here sss\_ods is a variable defined in a vars file. If I remove the latter condition the task would run but I want both the conditions to be satisfied for the task to run and I can’t understand where am I going wrong.

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [May 27, 2018, 5:00pm UTC](https://forum.ansible.com/t/multiple-conditions-not-working/27642/2 "2018-05-27T17:00:16Z")

</div>

The “when” stanza performs a AND when there are multiple when conditions ([http://docs.ansible.com/ansible/latest/user\_guide/playbooks\_conditionals.html#the-when-statement](http://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement)).

Whenever I encounter a problem with ‘when’ I try to break it up to multiple lines so the numerous parenthesis aren’t causing confusion:

- name: ods  
shell: “/something {{state}}”  
when:

- ods

- not (ansible\_hostname | search(“demlh\*”))

register: sss\_ods\_out  
notify: output ods  
ignore\_errors: yes

With that you might be able to deduce why the “AND” clause isn’t working as expected.

It might also be that the “ansible\_hostname | search(…)” might need to change to “ansible\_hostname is search(…)” (change | to is).

If not, can you provide what “ods” contains? A simple “debug:” before that call should suffice.

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [May 27, 2018, 5:04pm UTC](https://forum.ansible.com/t/multiple-conditions-not-working/27642/3 "2018-05-27T17:04:11Z")

</div>

I just noticed, if you break the when into two clauses, instead of doing “not (…)” you can do this:

- name: ods  
shell: “/something {{state}}”  
when:

- ods

- ansible\_hostname is not search(“demlh\*”)

register: sss\_ods\_out  
notify: output ods  
ignore\_errors: yes

---

<div class="post-metadata">

**Author:** ![Pranay\_Nanda](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pranay\_Nanda](https://forum.ansible.com/u/Pranay_Nanda)\
**Post date:** [May 27, 2018, 5:24pm UTC](https://forum.ansible.com/t/multiple-conditions-not-working/27642/4 "2018-05-27T17:24:00Z")

</div>

You’re a hero! Splitting does the job. I still wonder why previous logic did not work.

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [May 27, 2018, 6:12pm UTC](https://forum.ansible.com/t/multiple-conditions-not-working/27642/5 "2018-05-27T18:12:06Z")

</div>

I’ve run into subtle and hard-to-spot logic bugs (Ansible and elsewhere) that were ‘correct’ but magically solved when rewritten. I agree that your syntax looks spot on, but breaking it out into multiple statements might make it easier to troubleshoot later on too.

Glad it worked!
