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

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!

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 :slight_smile:

Dang, I found part of it finally

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

And it seems like I can parenthesize them too.

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?

Hi Eric,

the "|int" bit is a Jinja2 filter :

http://jinja.pocoo.org/docs/dev/templates/#builtin-filters

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

http://docs.ansible.com/playbooks_variables.html#jinja2-filters

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

*Parentheses, sry for the typo.

I tried searching 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.

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

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.

Figures, Here’s an example

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.

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. 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?

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 napisał:

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

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 :slight_smile: