# only\_if and when and  1.2

**URL:** <https://forum.ansible.com/t/only-if-and-when-and-1-2/12980>\
**Category:** Ansible Project\
**Created:** [June 18, 2013, 4:32pm UTC](https://forum.ansible.com/t/only-if-and-when-and-1-2/12980 "2013-06-18T16:32:04Z")\
**Posts on this page:** 4\
**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:** [June 18, 2013, 4:32pm UTC](https://forum.ansible.com/t/only-if-and-when-and-1-2/12980/1 "2013-06-18T16:32:04Z")

</div>

Hi,  
We've encountered a few things which worked in 1.1 which don't work in  
1.2. Most of them are fairly minor but I'm a bit concerned about other  
things.

So I've been auditing our playbooks looking for potential trouble spots.

I have this conditional

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

Which says "only if the host wasn't up in an earlier task and if the  
variable 'volumes' is defined - then it will run that task.

I have tested it and the above works on 1.2 - but I'm aware that  
only\_if is, perhaps undesirable.

so my question - how would I port the above conditional to a when-based  
world?

Thanks,  
-sv

---

<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:** [June 18, 2013, 4:41pm UTC](https://forum.ansible.com/t/only-if-and-when-and-1-2/12980/2 "2013-06-18T16:41:47Z")

</div>

This:

only\_if: ‘is\_set(“${volumes}”) and “${host\_is\_up.rc}” != “0”’

Becomes:

when: volumes is defined and host\_is\_up.rc != 0

Clearly a huge improvement.

I do concur that listification of legacy template strings in 1.2 is not optimal in all cases, but we had to make the cutover somehow, and I’m hugely pleased by the new ways looking so much cleaner.

---

<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:** [June 18, 2013, 4:43pm UTC](https://forum.ansible.com/t/only-if-and-when-and-1-2/12980/3 "2013-06-18T16:43:28Z")

</div>

Michael, Thank you!

That is much easier to read, I concur. I just wasn't sure how to do  
that. Is there a good example in the docs that i'm missing for compound  
when statements?

thanks,  
-sv

---

<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:** [June 18, 2013, 4:45pm UTC](https://forum.ansible.com/t/only-if-and-when-and-1-2/12980/4 "2013-06-18T16:45:45Z")

</div>

Don’t think so.

I’d welcome upgrades!

Basically anything to “when” is any expression in Jinja2 that evaluates with to True/False, the {{ and }} are just put around it automatically by Ansible. So it’s more or less Python, but not quite 100%.
