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