# multiple when\_ conditionals

**URL:** <https://forum.ansible.com/t/multiple-when-conditionals/12466>\
**Category:** Ansible Project\
**Created:** [March 7, 2013, 5:15am UTC](https://forum.ansible.com/t/multiple-when-conditionals/12466 "2013-03-07T05:15:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [March 7, 2013, 5:15am UTC](https://forum.ansible.com/t/multiple-when-conditionals/12466/1 "2013-03-07T05:15:09Z")

</div>

I was trying out using multiple when\_conditionals

for example:

when\_integer: $register.rc == 1  
when\_set: $myvalues

Should these work as an AND to determine if the action should run?

I was trying them out on 1.0 and it seemed like if the second one  
defined was true then the action was run.

Any thoughts on how to do multiple conditions?

thanks,  
-sv

---

<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:** [March 7, 2013, 5:20am UTC](https://forum.ansible.com/t/multiple-when-conditionals/12466/2 "2013-03-07T05:20:30Z")

</div>

IMHO this should be made to work as if they were all ANDed together.  
It doesn't seem to read any other way .. at least to me.

That being said, when I built it, I didn't see them all being used to  
together, but it totally makes sense that it should be possible.

I'm ok with changing the way this works as we never really definined  
the meaning and I doubt many people are using them together.

I suspect what happens right now is they are overriding the  
conditional with whatever was last in the argument hash order.

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [March 7, 2013, 6:26am UTC](https://forum.ansible.com/t/multiple-when-conditionals/12466/3 "2013-03-07T06:26:42Z")

</div>

Right now to achieve the same results I have to do this monstrosity:

only\_if: 'is\_set("${volumes}") and "${host\_is\_up.rc}" != "0"'

Just in case you were looking for horribly unreadable things.

turning that into  
when\_failed: $host\_is\_up  
when\_set: $volumes

would just be fantastic.

-sv

---

<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:** [March 7, 2013, 6:52am UTC](https://forum.ansible.com/t/multiple-when-conditionals/12466/4 "2013-03-07T06:52:14Z")

</div>

> > IMHO this should be made to work as if they were all ANDed together.  
> > It doesn't seem to read any other way .. at least to me.
> > 
> > That being said, when I built it, I didn't see them all being used to  
> > together, but it totally makes sense that it should be possible.
> > 
> > I'm ok with changing the way this works as we never really definined  
> > the meaning and I doubt many people are using them together.
> > 
> > I suspect what happens right now is they are overriding the  
> > conditional with whatever was last in the argument hash order.
> > 
> > \> I was trying out using multiple when\_conditionals  
> > \>  
> > \> for example:  
> > \>  
> > \> when\_integer: $register.rc == 1  
> > \> when\_set: $myvalues
> 
> Right now to achieve the same results I have to do this monstrosity:
> 
> only\_if: 'is\_set("${volumes}") and "${host\_is\_up.rc}" != "0"'
> 
> Just in case you were looking for horribly unreadable things.
> 
> turning that into  
> when\_failed: $host\_is\_up  
> when\_set: $volumes
> 
> would just be fantastic.

right, please file a ticket if you haven't already 🙂

---

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [March 7, 2013, 6:29pm UTC](https://forum.ansible.com/t/multiple-when-conditionals/12466/5 "2013-03-07T18:29:29Z")

</div>

I’ve tested this and it _doesn’t_ work (i.e. it seemed to ignore the second & subsequent when\_\_  
I was aiming to post this a few days ago, then decided that I had a reasonable enough workaround in my specific case by using is\_set and when\_boolean

An AND will be better, but still won’t be general, in my case I needed an OR.

---

<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:** [March 8, 2013, 4:39am UTC](https://forum.ansible.com/t/multiple-when-conditionals/12466/6 "2013-03-08T04:39:20Z")

</div>

> I've tested this and it \*doesn't\* work (i.e. it seemed to ignore the second  
> & subsequent when\_\_  
> I was aiming to post this a few days ago, then decided that I had a  
> reasonable enough workaround in my specific case by using is\_set and  
> when\_boolean

Yeah, we know it doesn't work -- that's what we were saying earlier.

If you need an or, I'd just go with the "only\_if" in that case. The  
"and" seems like it would  
be the "do what I mean" case at least for 80% of everyone out there.

- modulename: bar=12345  
&nbsp;&nbsp;register: foo

- modulename: asdf=1234  
&nbsp;&nbsp;when\_changed: foo  
&nbsp;&nbsp;when\_string: ${ansible\_distribution} == 'CentOS'

etc
