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

**URL:** <https://forum.ansible.com/t/easier-to-write-conditional-statements-with-when-in-0-9-now-available/12043>\
**Category:** Ansible Project\
**Created:** [October 27, 2012, 10:05pm UTC](https://forum.ansible.com/t/easier-to-write-conditional-statements-with-when-in-0-9-now-available/12043 "2012-10-27T22:05:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 27, 2012, 10:05pm UTC](https://forum.ansible.com/t/easier-to-write-conditional-statements-with-when-in-0-9-now-available/12043/1 "2012-10-27T22:05:12Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: "do this if my favcolor is not blue, and my dog is named fido"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: shell /bin/true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](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 🙂

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.  
&nbsp;&nbsp;&nbsp;&nbsp;( when clause from playbook) AND ( when clause from task)

Comments and questions welcome!

--Michael
