# Checking for multiple conditions using "when"

**URL:** <https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163>\
**Category:** Ansible Project\
**Created:** [May 5, 2014, 8:57pm UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163 "2014-05-05T20:57:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [May 5, 2014, 8:57pm UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163/1 "2014-05-05T20:57:30Z")

</div>

I have a list of modules I would like to deploy. With certain modules, I want to run a specific command against it. For example, if the module is a PHP or PERL module, I want to modify the ownership of the folder. If it’s anything else, ignore the task.

The task below fails

- name: Modify ownership  
shell: chown www:www {{ module\_path }}  
with\_dict: deploy\_modules  
when: ‘"{{ item.key }} == “php” or {{ item.key }} == “perl”’

TASK: [web | Modify ownership] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [web01] =\> error while evaluating conditional: "php == “perl” or php == “php”  
fatal: [web02] =\> error while evaluating conditional: "php == “perl” or php == “php”

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 6, 2014, 1:18am UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163/2 "2014-05-06T01:18:28Z")

</div>

When using “when:”, you don’t have to wrap variables in braces. Just do this:

when: item.key == “php” or item.key == “perl”

For example, the following works for me:

$ cat test\_with\_dict.yml

- hosts: localhost  
connection: local  
gather\_facts: no  
vars:
- values:  
foo:  
val1: “this is foo value 1”  
val2: “this is foo value 2”  
bar:

val1: “this is bar value 1”  
val2: “this is bar value 2”  
baz:

val1: “this is baz value 1”  
val2: “this is baz value 2”  
bam:

val1: “this is bam value 1”  
val2: “this is bam value 2”  
tasks:

- name: do loop over values dictionary  
debug: var=item.value.val1  
with\_dict: values  
when: item.key == ‘foo’ or item.key == ‘bar’

---

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [May 7, 2014, 8:27pm UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163/3 "2014-05-07T20:27:36Z")

</div>

Still not working.

TASK: [web | Modify ownership] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [oqn-qc-web01] =\> error while evaluating conditional: item.key == “php” or item.key == “perl”’  
fatal: [oqn-qc-web02] =\> error while evaluating conditional: item.key == “php” or item.key == “perl”’

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [May 7, 2014, 8:39pm UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163/4 "2014-05-07T20:39:12Z")

</div>

How and where did you define **deploy\_modules**?

---

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [May 7, 2014, 8:42pm UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163/5 "2014-05-07T20:42:37Z")

</div>

Here’s the full thing,

# This is from my group var file.

# cat groups\_vars/deploy\_list

deploy\_modules:

php:  
tag: ‘abc123’  
svn\_path: ‘path\_to\_svn’  
document\_root: ‘/tmp’

perl:  
tag: ‘abc987’  
svn\_path: ‘path\_to\_svn’  
document\_root: ‘/tmp’

# end file

- name: Deploying Modules  
action: shell svn export --username myuser --password mypassword {{ repository }}/{{ item.value.svn\_path }}/tags/{{ item.value.tag }} /tmp/{{ item.key }}.{{revision\_no}}  
with\_dict: deploy\_modules

- name: Modify ownership  
shell: chown www:www {{ module\_path }}  
with\_dict: deploy\_modules  
when: item.key == “php” or item.key == “perl”’

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 7, 2014, 8:52pm UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163/6 "2014-05-07T20:52:34Z")

</div>

You have an errant trailing ’ at the end of your ‘when’ line:

> when: item.key == “php” or item.key == “perl”’

Should instead be:

> when: item.key == “php” or item.key == “perl”

I tested with that change and everything works as expected.

One potential improvement:

---

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [May 7, 2014, 8:57pm UTC](https://forum.ansible.com/t/checking-for-multiple-conditions-using-when/15163/7 "2014-05-07T20:57:46Z")

</div>

Wow … thanks a lot. That was it. Also, great tip, I was hoping ansible would allow me to use the “in” command.
