# Migrating from the use of Python expressions and only\_if to the new style of variables and 'when'?

**URL:** <https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078>\
**Category:** Ansible Project\
**Tags:** fedora, ubuntu\
**Created:** [July 6, 2013, 6:59am UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078 "2013-07-06T06:59:18Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [July 6, 2013, 6:59am UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/1 "2013-07-06T06:59:18Z")

</div>

In an existing playbook of mine, so far I have the following:

vars:  
is\_ubuntu: “‘$ansible\_distribution’ == ‘Ubuntu’”  
is\_rh: “‘$ansible\_distribution’ == ‘Fedora’ or ‘$ansible\_distribution’ == ‘Scientific’ or ‘$ansible\_distribution’ == ‘CentOS’”  
earlier\_than\_precise: “‘$ansible\_distribution’ == ‘Ubuntu’ and ‘$ansible\_distribution\_version’ \< ‘12.04’”  
precise\_or\_later: “‘$ansible\_distribution’ == ‘Ubuntu’ and ‘$ansible\_distribution\_version’ \>= ‘12.04’”

Here is a task using the “old” only\_if:

- name: Ubuntu - Create an account for Test User  
user: name=tset comment=‘Test User’ shell=/bin/bash createhome=yes password=$tset\_password  
only\_if: ‘$is\_ubuntu’
- name: Ubuntu older than precise- Add Test User to the admin group  
user: name=tset groups=admin append=yes  
only\_if: ‘$earlier\_than\_precise’
- name: Ubuntu precise and later - Add Test User to the sudo group  
user: name=tset groups=sudo append=yes  
only\_if: ‘$precise\_or\_later’

To update my playbook, I changed the vars section to the following:

vars:

# Of course, if we use the real Debian, we can expand the following.

is\_ubuntu: “{{ ansible\_distribution }}’ == ‘Ubuntu’”  
is\_rh: “‘{{ ansible\_distribution }}’ == ‘Fedora’ or ‘{{ ansible\_distribution }}’ == ‘Scientific’ or ‘{{ ansible\_distribution }}’ == ‘CentOS’”  
earlier\_than\_precise: “‘{{ ansible\_distribution }}’ == ‘Ubuntu’ and ‘{{ ansible\_distribution\_version }}’ \< ‘12.04’”  
precise\_or\_later: “‘{{ ansible\_distribution }}’ == ‘Ubuntu’ and ‘{{ ansible\_distribution\_version }}’ \>= ‘12.04’”

and the task to the following:

- name: Ubuntu - Create an account for Test User  
user: name=tset comment=‘tset’ shell=/bin/bash createhome=yes password={{ tset\_password }}  
when: is\_ubuntu
- name: Ubuntu older than precise - Add tset to the admin group  
user: name=tset groups=admin append=yes  
when: earlier\_than\_precise
- name: Ubuntu precise or later - Add tset to the sudo group  
user: name=tset groups=sudo append=yes  
when: precise\_or\_later

Testing the above, even on a Ubuntu box running 11.04, the last task still got executed. I must have misunderstood the documentation somehow. Any hints are appreciated.

– Zack

---

<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:** [July 6, 2013, 7:51pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/2 "2013-07-06T19:51:59Z")

</div>

" precise\_or\_later: “‘{{ ansible\_distribution }}’ == ‘Ubuntu’ and ‘{{ ansible\_distribution\_version }}’ \>= ‘12.04’”

Are you intending to do a string comparison with a greater than sign?

The when statement you want is:

when: ansible\_distribution == ‘Ubuntu’ and ansible\_distribution\_version \>= ‘12.04’

Thus, if you want to save that statement, I believe you don’t want to put the handlebars in the saved expression.

---

<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:** [July 6, 2013, 7:52pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/3 "2013-07-06T19:52:37Z")

</div>

Also, “when: precise\_or\_later” is a string.

You would want:

when: “{{ precise\_or\_later }}”

but should of course first fix the “precise\_or\_later” expression you are saving, as it doesn’t need the handle bars in it.

---

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [July 6, 2013, 8:29pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/4 "2013-07-06T20:29:52Z")

</div>

Hi Michael,

> Also, “when: precise\_or\_later” is a string.
> 
> You would want:
> 
> when: “{{ precise\_or\_later }}”
> 
> but should of course first fix the “precise\_or\_later” expression you are saving, as it doesn’t need the handle bars in it.

Thanks for your tips. Below is a simple playbook that I created:

---

<div class="post-metadata">

**Author:** ![Krzysztof\_Warzecha](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@Krzysztof\_Warzecha](https://forum.ansible.com/u/Krzysztof_Warzecha)\
**Post date:** [July 6, 2013, 8:34pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/5 "2013-07-06T20:34:02Z")

</div>

Hello,

> &nbsp;&nbsp;&nbsp;&nbsp;- name: RH - announce the hostname  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/hostname  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "{{ is\_rh }"

You have a typo here. It should be:

when: "{{ is\_rh }}"

> &nbsp;&nbsp;&nbsp;&nbsp;- name: Ubuntu \< 12.04 - announce the hostname  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/hostname  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "{{ earlier\_than\_precise }"

And here too.

---

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [July 6, 2013, 8:36pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/6 "2013-07-06T20:36:31Z")

</div>

BTW, the following works. But this doesn’t help my case since _I would like to re-use variables defined in the vars section rather than repeat these verbose logic conditions for each and every task_:

---

<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:** [July 6, 2013, 8:38pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/7 "2013-07-06T20:38:06Z")

</div>

Actually I got the above wrong, you need both:

when: “{{ saved\_expression }}”

AND the {{ }} around the variables in the saved expression.

---

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [July 6, 2013, 9:02pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/8 "2013-07-06T21:02:00Z")

</div>

Hi Krzysztof,

Thanks for chipping in.

> Hello,
> 
> > - name: RH - announce the hostname  
> > shell: /bin/hostname  
> > when: “{{ is\_rh }”
> 
> You have a typo here. It should be:
> 
> when: “{{ is\_rh }}”
> 
> > - name: Ubuntu \< 12.04 - announce the hostname  
> > shell: /bin/hostname  
> > when: “{{ earlier\_than\_precise }”
> 
> And here too.

Fixing these doesn’t seem to be sufficient. The following is the fixed playbook:

---

<div class="post-metadata">

**Author:** ![Zack\_Perry](https://avatars.discourse-cdn.com/v4/letter/z/54ee81/32.png) [@Zack\_Perry](https://forum.ansible.com/u/Zack_Perry)\
**Post date:** [July 6, 2013, 9:08pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/9 "2013-07-06T21:08:46Z")

</div>

> Actually I got the above wrong, you need both:
> 
> when: “{{ saved\_expression }}”
> 
> AND the {{ }} around the variables in the saved expression.

I took the above tips, and came up the following (back to square one?)

---

<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:** [July 7, 2013, 12:19am UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/10 "2013-07-07T00:19:44Z")

</div>

I’ll see about testing this and giving you an answer later.

Until then, I highly recommend just using group\_by, as it produces superior output for slicing up host by OS type, etc.

---

<div class="post-metadata">

**Author:** ![Krzysztof\_Warzecha](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@Krzysztof\_Warzecha](https://forum.ansible.com/u/Krzysztof_Warzecha)\
**Post date:** [July 7, 2013, 12:24am UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/11 "2013-07-07T00:24:27Z")

</div>

OK, I think you have found a bug. Please consider this playbook:

---

<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:** [July 7, 2013, 12:21pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/12 "2013-07-07T12:21:46Z")

</div>

This isn’t a bug.

When you have a “when” statement, it should look like this:

“when: expression”

Not "when: {{ expression }} "

The whole point was to eliminate the need of the template expressions inside the conditional.

---

<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:** [July 7, 2013, 12:23pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/13 "2013-07-07T12:23:05Z")

</div>

To restate a bit, there may of course be a problem referencing previous conditionals as variables.

Which I’ve already mentioned to Zack I’m going to look into it 🙂

---

<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:** [July 7, 2013, 12:58pm UTC](https://forum.ansible.com/t/migrating-from-the-use-of-python-expressions-and-only-if-to-the-new-style-of-variables-and-when/13078/14 "2013-07-07T12:58:58Z")

</div>

You can track the status of this one here:

[https://github.com/ansible/ansible/issues/3460](https://github.com/ansible/ansible/issues/3460)
