# How can I do multiple tests within a single when: statement

**URL:** <https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432>\
**Category:** Ansible Project\
**Created:** [October 4, 2014, 6:42pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432 "2014-10-04T18:42:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Wedaa](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Eric\_Wedaa](https://forum.ansible.com/u/Eric_Wedaa)\
**Post date:** [October 4, 2014, 6:42pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/1 "2014-10-04T18:42:05Z")

</div>

I’ve looked and looked but I can’t find it.

How do I do multiple tests within a single when statement.

For instance  
when: var1 == “X” AND var2 == “Y”

And while I’m at it, how can I do OR statements along with multiple tests, such as  
when: (var1 == “X” AND var2 == “Y” ) OR var3 == “Q”

I’ve tried guessing at a few formats but I’m just not getting it to work.

Thanks!

---

<div class="post-metadata">

**Author:** ![Fran\_Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fran_garcia/32/4573_2.png) [@Fran\_Garcia](https://forum.ansible.com/u/Fran_Garcia)\
**Post date:** [October 4, 2014, 8:23pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/2 "2014-10-04T20:23:34Z")

</div>

when: ansible\_lsb.id == 'RedHat' and ansible\_lsb.major\_release|int == 6

when: ansible\_hostname != 'localhost'

I haven't been able to find any example with "and/or" grouping ... no  
idea if the parenthesis works 🙂

---

<div class="post-metadata">

**Author:** ![Eric\_Wedaa](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Eric\_Wedaa](https://forum.ansible.com/u/Eric_Wedaa)\
**Post date:** [October 4, 2014, 8:24pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/3 "2014-10-04T20:24:09Z")

</div>

Dang, I found part of it finally

```auto
 when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6

```

And it seems like I can parenthesize them too.

---

<div class="post-metadata">

**Author:** ![Eric\_Wedaa](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Eric\_Wedaa](https://forum.ansible.com/u/Eric_Wedaa)\
**Post date:** [October 5, 2014, 2:07am UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/4 "2014-10-05T02:07:33Z")

</div>

I’m still testing but it APPEARS that parenthesis work, but I need to test more.

I have NO idea what the | character does though, although in the example it turns a string into an integer.

Does anybody have some pointers to some better documentation about these gory details?

---

<div class="post-metadata">

**Author:** ![Fran\_Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fran_garcia/32/4573_2.png) [@Fran\_Garcia](https://forum.ansible.com/u/Fran_Garcia)\
**Post date:** [October 5, 2014, 3:42pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/5 "2014-10-05T15:42:28Z")

</div>

Hi Eric,

the "|int" bit is a Jinja2 filter :

[http://jinja.pocoo.org/docs/dev/templates/#builtin-filters](http://jinja.pocoo.org/docs/dev/templates/#builtin-filters)

---

<div class="post-metadata">

**Author:** ![Fran\_Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fran_garcia/32/4573_2.png) [@Fran\_Garcia](https://forum.ansible.com/u/Fran_Garcia)\
**Post date:** [October 5, 2014, 3:51pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/6 "2014-10-05T15:51:22Z")

</div>

I'd better link the actual Ansible documentation for more meaningful  
examples 🙂

[http://docs.ansible.com/playbooks\_variables.html#jinja2-filters](http://docs.ansible.com/playbooks_variables.html#jinja2-filters)

---

<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:** [October 5, 2014, 4:51pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/7 "2014-10-05T16:51:52Z")

</div>

Parenthesize work for grouping without a problem, I used them in many more complex conditions.

---

<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:** [October 5, 2014, 4:56pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/8 "2014-10-05T16:56:35Z")

</div>

\*Parentheses, sry for the typo.

I tried searching [docs.ansible.com](http://docs.ansible.com) and I couldn’t find any examples with them. Documentation should probably be updated to include an example of an advance condition with parentheses.

---

<div class="post-metadata">

**Author:** ![Eric\_Wedaa](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Eric\_Wedaa](https://forum.ansible.com/u/Eric_Wedaa)\
**Post date:** [October 5, 2014, 4:56pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/9 "2014-10-05T16:56:49Z")

</div>

But that’s the ansible docs that weren’t helpful enough. ☹

What are the operators? I know “and”, and “or” seems to work. What other operators do I have? How do I specify “not”? Can I really use parenthesis? and what the heck does the “|” really do? Is there a “contains” operator? Let me look around for an example in another language to point out what I am really looking for.

---

<div class="post-metadata">

**Author:** ![Eric\_Wedaa](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Eric\_Wedaa](https://forum.ansible.com/u/Eric_Wedaa)\
**Post date:** [October 5, 2014, 4:59pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/10 "2014-10-05T16:59:36Z")

</div>

Figures, Here’s an example

[http://www.informit.com/articles/article.aspx?p=395035&seqNum=2](http://www.informit.com/articles/article.aspx?p=395035&seqNum=2)

It shows all the operators in perl, string, text, and logical, and implicitly shows that you can bundle them together.

---

<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:** [October 5, 2014, 5:20pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/11 "2014-10-05T17:20:29Z")

</div>

Ansible doesn’t have a document like that, but it probably should.

Francisco pointed you to what the pipe ‘|’ means, I guess you missed it [http://docs.ansible.com/playbooks\_variables.html#jinja2-filters](http://docs.ansible.com/playbooks_variables.html#jinja2-filters). It is basically a filter, e.g:

`when: var1|bool`

would convert var1 to a Boolean even if it is not True or False (e.g. strings ‘yes’, ‘true’, ‘TRUE’,… would be converted to True). This is extremely useful when you need to pass a Boolean over command line using -e ‘var1=true’, since Ansible would parse this var1 as a string.

Here is how not works as well as parentheses:

`when: not var1 and (var2 == 'blue' or var3 == 'red')`

Contains operator for what exactly? String? Array?

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [October 5, 2014, 5:57pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/12 "2014-10-05T17:57:10Z")

</div>

There’s an “in” operator: ‘blah’ in ‘blahblahblah’ and 10 in [1, 7, 10] are both true.

For more information about syntax between {{ }} look for jinja2 docs - they are pretty nice (and probably should be mentioned in some more visible place in Ansible docs)

“Strahinja Kustudić” [strahinjak@nordeus.eu](mailto:strahinjak@nordeus.eu) napisał:

---

<div class="post-metadata">

**Author:** ![Eric\_Wedaa](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Eric\_Wedaa](https://forum.ansible.com/u/Eric_Wedaa)\
**Post date:** [October 5, 2014, 7:42pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/13 "2014-10-05T19:42:12Z")

</div>

For instance, in Perl and Bash, there are different operators for numeric comparisons and string comparisons.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 6, 2014, 12:36pm UTC](https://forum.ansible.com/t/how-can-i-do-multiple-tests-within-a-single-when-statement/16432/14 "2014-10-06T12:36:52Z")

</div>

Jinja2 basically uses mostly-like-Python comparisons, so it’s a lot simpler. And this is what the “when” in Ansible uses to evaluate statements.

We try to not overwhelm folks with all of the Jinja2, in fact, overuse of Jinja2 in Ansible playbooks is something of a problem in user-land.

Rather, the documentation shows some of the basic examples and points towards the Jinja2 documentation for those that wish to learn more.

I want to keep that from being too overwhelming, or also encouraging a new user to use “all” of the Jinja2 too soon, which would lead to some ungainly playbooks.

Anyway, that’s kind of why it is the way it is.

With great templating power comes great responsibility, etc 🙂
