# Thinking abouta better 'only\_if' ... just not doing it quite yet

**URL:** <https://forum.ansible.com/t/thinking-abouta-better-only-if-just-not-doing-it-quite-yet/11944>\
**Category:** Ansible Project\
**Created:** [September 29, 2012, 12:51pm UTC](https://forum.ansible.com/t/thinking-abouta-better-only-if-just-not-doing-it-quite-yet/11944 "2012-09-29T12:51:17Z")\
**Posts on this page:** 5\
**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:** [September 29, 2012, 12:51pm UTC](https://forum.ansible.com/t/thinking-abouta-better-only-if-just-not-doing-it-quite-yet/11944/1 "2012-09-29T12:51:17Z")

</div>

So only\_if is pretty flexible and it will stay around. We'll need it.

However I've never really been comfortable with asking people to write  
Python expressions in a language that intentionally seeks to AVOID  
such expressions.  
This small corner of ansible is asking folks to understand a  
programming language we obviously want to hide.

As such, I think "easy conditionals" becomes an interesting mini-theme  
for the 0.9 release.

Tentative syntax follows.

Imagine if you could do things without Python or quoting:

- action: ...  
&nbsp;&nbsp;when\_defined: foo

when\_defined: some.var.foo

when\_equal: command.rc 1

when\_successful: command\_register\_result

when\_failed: command\_register\_result

when\_greater\_than: some.var.foo 12

when\_all\_of:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- equal: result 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- find: some\_command.stdout some\_string

when\_one\_of:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- equal: result.rc 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- equal: result2.rc 1

And, like before, you should be able to save 'when' tests in vars, like so:

when: $is\_rhel6

Internally, these would compile down to only\_if expressions, but would  
not require writing Python, quoting, or remembering what data types to  
cast things to. As a result, we keep conditional logic  
simple, and make things easier to read.

--Michael

---

<div class="post-metadata">

**Author:** ![Giuliano\_Barberi](https://avatars.discourse-cdn.com/v4/letter/g/edb3f5/32.png) [@Giuliano\_Barberi](https://forum.ansible.com/u/Giuliano_Barberi)\
**Post date:** [September 29, 2012, 2:48pm UTC](https://forum.ansible.com/t/thinking-abouta-better-only-if-just-not-doing-it-quite-yet/11944/2 "2012-09-29T14:48:42Z")

</div>

This is nice because it’s more readable. Code is powerful though and I think it’s always good to make sure you can drop down to code if you need to. One thing I was trying to do was do an if/else to decide which value to use for a var … e.g.

some\_var: $is\_rhe ? /etc/somepath : /etc/someotherpath

and wasn’t sure if there was a good way to do that or not.

---

<div class="post-metadata">

**Author:** ![fangchin](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@fangchin](https://forum.ansible.com/u/fangchin)\
**Post date:** [September 30, 2012, 4:59am UTC](https://forum.ansible.com/t/thinking-abouta-better-only-if-just-not-doing-it-quite-yet/11944/3 "2012-09-30T04:59:16Z")

</div>

> This is nice because it’s more readable. Code is powerful though and I think it’s always good to make sure you can drop down to code if you need to. One thing I was trying to do was do an if/else to decide which value to use for a var … e.g.
> 
> some\_var: $is\_rhe ? /etc/somepath : /etc/someotherpath
> 
> and wasn’t sure if there was a good way to do that or not.

How about this: use a bash shell script to

- Wrap around all “static” playbooks
- Handle complex conditionals and use the here doc mechanism to create a playbook as needed
- Run the all playbooks  
Regards,

Chin Fang

---

<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:** [September 30, 2012, 11:51am UTC](https://forum.ansible.com/t/thinking-abouta-better-only-if-just-not-doing-it-quite-yet/11944/4 "2012-09-30T11:51:22Z")

</div>

Generating playbooks via bash seems terrible to me.

Completely defeats the point of Ansible being readable and auditable  
and easy to work on.

--Michael

---

<div class="post-metadata">

**Author:** ![Andreas\_Davour](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Andreas\_Davour](https://forum.ansible.com/u/Andreas_Davour)\
**Post date:** [October 1, 2012, 7:30am UTC](https://forum.ansible.com/t/thinking-abouta-better-only-if-just-not-doing-it-quite-yet/11944/5 "2012-10-01T07:30:07Z")

</div>

Those examples looks totally awesome!

Many thanks to all python fluent people who makes ansible grow in  
interesting directions.

/andreas
