# when: syntax

**URL:** <https://forum.ansible.com/t/when-syntax/12874>\
**Category:** Ansible Project\
**Created:** [May 27, 2013, 6:01am UTC](https://forum.ansible.com/t/when-syntax/12874 "2013-05-27T06:01:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 27, 2013, 6:01am UTC](https://forum.ansible.com/t/when-syntax/12874/1 "2013-05-27T06:01:13Z")

</div>

Dear List,

We are trying this:

> * * *
> 
> ######################################
> 
> - hosts: $omgeving  
> sudo: true  
> vars\_files:
> - ~/playbooks/vars/global.yml
> - [“$vars/${ansible\_distribution}.yml”, “$vars/defaults.yml”]
> 
> tasks:
> 
> - include: $basedir/europort/ep\_stop\_start/tasks/ep\_stop.yml  
> when: $running == False or $running == None
> 
> - include: $basedir/europort/ep\_stop\_start/tasks/ep\_patch\_aix.yml  
> when: “aix” in $ansible\_hostname
> 
> - include: $basedir/europort/ep\_stop\_start/tasks/ep\_patch\_eib.yml  
> when: “eib” in $ansible\_hostname
> 
> - include: $basedir/europort/ep\_stop\_start/tasks/finttpci\_start.yml  
> when: $running == True or $running == None

But none of this is working… Am I going about this the wrong way, or have I simply got the syntax wrong?

Especially the “when: “eib” in $ansible\_hostname” bit is troubling. Does the “when:” statement actually support the “in” test with a variable?

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [May 27, 2013, 1:15pm UTC](https://forum.ansible.com/t/when-syntax/12874/2 "2013-05-27T13:15:10Z")

</div>

Hi,

First thing is to inspect nice examples here [https://github.com/ansible/ansible-examples/blob/master/language\_features/conditionals\_part2.yml](https://github.com/ansible/ansible-examples/blob/master/language_features/conditionals_part2.yml)  
So, no $ for variables and “is defined” instead of “None” I guess in your case

Regards,  
Dima

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 27, 2013, 3:20pm UTC](https://forum.ansible.com/t/when-syntax/12874/3 "2013-05-27T15:20:35Z")

</div>

Ok, so it’s usually best to do what the documentation does and try to be consistent. You’re mixing a bit of 1.1-isms and 1.2-isms. Not understanding what “doesn’t work” means, I’ll err on the side of too much information 🙂

(A)

If you are using “when” you should first make sure you are on 1.2, as when is a 1.2 feature.

It will raise an error and “not work” when you are using less than 1.2

(B)

If on 1.2, it’s best to be consistent.

Don’t use $foo in when expressions, as the whole point of 1.2 is to eliminate the need for the legacy ${perl\_like} syntaxes

With when, all you have to do is say:

when: foo == 2

And so on.

If you put $foo in a when statement, it’s going to evaluate that variable to a string and lose it’s type information. You definitely don’t want that in the case where you are passing in a list or integer something.

(C)

If you want to use in, you are probably starting your YAML expression with a string and have to quote the whole line to keep YAML happy:

when: ‘“asdf” in bar’

(D)

To avoid weird quoting from having to start the line, this is often just as nice:

when: bar.find(“asdf”) != -1

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 28, 2013, 5:42am UTC](https://forum.ansible.com/t/when-syntax/12874/4 "2013-05-28T05:42:04Z")

</div>

Hi Michael,

Yeah, you are right, it was a baaaaaad weekend, and it impaired my reasoning and questioning skills.

Anyhow, your exemplary reply basicly cleared away the fog now 😉

Thanks!  
Mark
