# only\_if syntax suggestion

**URL:** <https://forum.ansible.com/t/only-if-syntax-suggestion/11503>\
**Category:** Ansible Project\
**Created:** [May 5, 2012, 1:57pm UTC](https://forum.ansible.com/t/only-if-syntax-suggestion/11503 "2012-05-05T13:57:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Art\_Zemon](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@Art\_Zemon](https://forum.ansible.com/u/Art_Zemon)\
**Post date:** [May 5, 2012, 1:57pm UTC](https://forum.ansible.com/t/only-if-syntax-suggestion/11503/1 "2012-05-05T13:57:51Z")

</div>

Michael,

Your conditionals\_part2.yml contains this example line:

only\_if: ‘not ($is\_favcolor\_blue)’

Since you do not allow binary operators in the value of only\_if, it seems odd that you allow the unary operator. Personally, I would not find

only\_if: ‘“$var” == “constant”’

hard to read. But if you are going to disallow that construct then I suggest you also disallow the unary “not” operator and instead use this construct:

only\_if\_not: ‘$is\_favcolor\_blue’

As I said, though, I would be happy if I could put an expression instead of just a variable in the value of only\_if.

Cheers,  
– Art Z.

---

<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:** [May 5, 2012, 2:11pm UTC](https://forum.ansible.com/t/only-if-syntax-suggestion/11503/2 "2012-05-05T14:11:58Z")

</div>

> Michael,
> 
> Your conditionals\_part2.yml contains this example line:
> 
> only\_if: ‘not ($is\_favcolor\_blue)’
> 
> Since you do not allow binary operators in the value of only\_if, it seems odd that you allow the unary operator. Personally, I would not find
> 
> only\_if: ‘“$var” == “constant”’

I don’t allow or disallow anything, it’s a Python expression. Where does it say that?

> As I said, though, I would be happy if I could put an expression instead of just a variable in the value of only\_if.

I’m confused by this, they are expressions.

---

<div class="post-metadata">

**Author:** ![Art\_Zemon](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@Art\_Zemon](https://forum.ansible.com/u/Art_Zemon)\
**Post date:** [May 7, 2012, 12:46pm UTC](https://forum.ansible.com/t/only-if-syntax-suggestion/11503/3 "2012-05-07T12:46:27Z")

</div>

> > Michael,
> > 
> > Your conditionals\_part2.yml contains this example line:
> > 
> > only\_if: ‘not ($is\_favcolor\_blue)’
> > 
> > Since you do not allow binary operators in the value of only\_if, it seems odd that you allow the unary operator. Personally, I would not find
> > 
> > only\_if: ‘“$var” == “constant”’
> 
> I don’t allow or disallow anything, it’s a Python expression. Where does it say that?
> 
> > As I said, though, I would be happy if I could put an expression instead of just a variable in the value of only\_if.
> 
> I’m confused by this, they are expressions.

Michael,

I guess I misunderstood this comment:

> # Not only can we assign variables for reuse, but we can also assign conditional
> 
> # expressions. By keeping these in ‘vars’, the task section remains
> 
> # extraordinarily clean, and not littered with programming language
> 
> # constructs – so it’s easily skimmed by humans.

as meaning that ansible required the only\_if argument to be a single variable.

– Art Z.

---

<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:** [May 7, 2012, 1:08pm UTC](https://forum.ansible.com/t/only-if-syntax-suggestion/11503/4 "2012-05-07T13:08:57Z")

</div>

> > 
> 
> as meaning that ansible required the only\_if argument to be a single variable.

It can either be a string Python expression or something that evaluates to a string Python expression.
