# Unexpected behaviour of conditionals

**URL:** <https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181>\
**Category:** Ansible Project\
**Created:** [January 3, 2013, 4:14pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181 "2013-01-03T16:14:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [January 3, 2013, 4:14pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/1 "2013-01-03T16:14:21Z")

</div>

If I want an action to only run on Centos I can use when\_string:-

&nbsp;&nbsp;&nbsp;- name: ensure package absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: yum name=$item state=absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when\_string: $ansible\_distribution == 'Centos'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- fprintd

However if I have a set of things that are specific to an OS, and move them into their own task file, then this syntax does not work:-

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Basic Package setup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include: tasks/basic\_centos\_packages.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when\_string: $ansible\_distribution == 'Centos'

It executes for every host, no matter what OS/distribution.

Is this what is expected?

&nbsp;&nbsp;Nigel.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 3, 2013, 4:31pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/2 "2013-01-03T16:31:03Z")

</div>

includes directives always get executed, not influenced by  
conditionals, you might want to add the when\_ inside the included  
file.

---

<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:** [January 3, 2013, 6:17pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/3 "2013-01-03T18:17:12Z")

</div>

Additionally, the app really should yell at you when you do one of  
these things. I'm not sure why task.py didn't already handle that,  
but it should.

---

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [January 3, 2013, 8:24pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/4 "2013-01-03T20:24:13Z")

</div>

Michael DeHaan wrote:

> Additionally, the app really should yell at you when you do one of  
> these things. I'm not sure why task.py didn't already handle that,  
> but it should.

Warning of things that don't work as a naive user might expect would be good.

I can see \*why\* this is like this - presumably its a case of the initial pass through the playbook expands the whole thing into a single (yaml like) data structure by expanding the includes.

However being able to put conditionals onto some form of grouping rather than adding the conditional to each action would be good - I have just edited a dozen or so actions with the same thing on each which feels messy (and error prone). Maybe a way to have a conditional/loop and a list of further actions under that?

&nbsp;&nbsp;Nigel.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 3, 2013, 8:56pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/5 "2013-01-03T20:56:40Z")

</div>

We discussed this in IRC, it would make a nice feature (conditional  
auto unrolled to tasks on inclusion).

I'll put this on my list, but I have a few things in front, feel free  
to tackle and submit patches.

---

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [January 3, 2013, 9:50pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/6 "2013-01-03T21:50:30Z")

</div>

Brian Coca wrote:

> We discussed this in IRC, it would make a nice feature (conditional  
> auto unrolled to tasks on inclusion).
> 
> I'll put this on my list, but I have a few things in front, feel free  
> to tackle and submit patches.

I'll have a look - although I have something else I am working on with ansible in my queue first.

&nbsp;&nbsp;Nigel.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 3, 2013, 11:18pm UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/7 "2013-01-03T23:18:42Z")

</div>

Nigel Metheringham wrote:

> Michael DeHaan wrote:
> 
> > Additionally, the app really should yell at you when you do one of  
> > these things. I'm not sure why task.py didn't already handle that,  
> > but it should.

I've added this on devel.

> Warning of things that don't work as a naive user might expect would be  
> good.
> 
> I can see \*why\* this is like this - presumably its a case of the initial  
> pass through the playbook expands the whole thing into a single (yaml  
> like) data structure by expanding the includes.
> 
> However being able to put conditionals onto some form of grouping rather  
> than adding the conditional to each action would be good - I have just  
> edited a dozen or so actions with the same thing on each which feels  
> messy (and error prone). Maybe a way to have a conditional/loop and a  
> list of further actions under that?

I've also added this on devel, so task includes and only\_if/when\_\* now works  
as expected.

Daniel

---

<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:** [January 4, 2013, 1:03am UTC](https://forum.ansible.com/t/unexpected-behaviour-of-conditionals/12181/8 "2013-01-04T01:03:26Z")

</div>

awesome, thanks Daniel!
