Easier to write conditional statements with "when" in 0.9! Now available!

So we've had a way to conditionally execute tasks (decide whether to
execute them or not) in "only_if" for a while.

Only_if is troublesome as that, because it is a powerful python
expression, you have to know how to quote everything just right in
order to use it, and this becomes
weird as we substitute variables in and so forth. I previously
proposed making a "when" operator to simplify this syntax greatly, and
have now done so.

Here is one simple example:

     - name: "do this if my favcolor is not blue, and my dog is named fido"
       action: shell /bin/true
       when_string: $favcolor != 'blue' and $dog == 'fido'

Many more examples, and all that is possible:

https://github.com/ansible/ansible/blob/devel/examples/playbooks/conditionals_part2.yml

And it will be in the actual documentation as well closer to release.

There are some cases where when is not going to work for you, like if
one of your variables is a list and you want to use Python's "in"
operator. I imagine over time we'll extend
the 'when' command to have a few more variants, but this should be
sufficient for most everyone.

The "when" keyword still compiles down to an "only_if" statement, it
just allows you to not think about quoting rules (annoying! I know!)
and type casting (also annoying!), and you also
don't have to quote the whole expression either.

Care should be taken about one thing though. You have to surround any
variables you use with whitespace, or the preprocessor will not see
them, and it most likely will not do what you want.

only_if is not going to be deprecated, but using it will probably be
discouraged, as 'when' is tons more readable.

Possible gotchas:

You can not presently use "only_if" with "when", as the "when" will
overwrite the only_if -- don't try :slight_smile:

You can't currently set a "when" at play level. I want to make this
possible though, where it will join with any existing "when" clauses
using and "and" operator and parenthesis, i.e.
    ( when clause from playbook) AND ( when clause from task)

Comments and questions welcome!

--Michael