# Evaluation of (Python) expressions only in "only\_if" and not in "vars" section of a playbook?

**URL:** <https://forum.ansible.com/t/evaluation-of-python-expressions-only-in-only-if-and-not-in-vars-section-of-a-playbook/11470>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [April 30, 2012, 5:27pm UTC](https://forum.ansible.com/t/evaluation-of-python-expressions-only-in-only-if-and-not-in-vars-section-of-a-playbook/11470 "2012-04-30T17:27:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank](https://forum.ansible.com/u/Frank)\
**Post date:** [April 30, 2012, 5:27pm UTC](https://forum.ansible.com/t/evaluation-of-python-expressions-only-in-only-if-and-not-in-vars-section-of-a-playbook/11470/1 "2012-04-30T17:27:49Z")

</div>

Hi,

I'm playing around with Ansible for few hours - it is very interesting  
(and feature list is exactly what I was looking for), but it seams to  
be a bit inflexible to me. Maybe, I just don't understand the concepts  
correctly. Therefore, here and in following posts I will ask some  
questions ...

Do I understand it correctly that (Python) expressions are only  
evaluated as the "only\_if" value? I would prefer to have evaluation in  
the "vars" section of a playbook to make it more modular. Similar toi  
this example:

- hosts: foo  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;is\_correct\_platform: "'{{ansible\_product\_name}}' == 'VMware  
Virtual Platform'"  
&nbsp;&nbsp;&nbsp;&nbsp;tmp\_dir: "/tmp/foo"  
&nbsp;&nbsp;&nbsp;&nbsp;tmp\_file: "{{tmp\_dir}}/myfile"  
&nbsp;&nbsp;&nbsp;&nbsp;file\_exists: "os.path.isfile('{{tmp\_file}}')"  
&nbsp;&nbsp;&nbsp;&nbsp;do\_action: "{{is\_correct\_platform}} and not({{file\_exists}})"  
&nbsp;&nbsp;tasks  
&nbsp;&nbsp;- name: foo1  
&nbsp;&nbsp;&nbsp;&nbsp;action: ...  
&nbsp;&nbsp;&nbsp;&nbsp;only\_if: "{{do\_action}}"  
&nbsp;&nbsp;- name: foo2  
&nbsp;&nbsp;&nbsp;&nbsp;action: ...  
&nbsp;&nbsp;&nbsp;&nbsp;only\_if: "{{do\_action}}"

Can I do this in a different way? Are changes scheduled for future  
releases?

Thanks,

Frank

---

<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:** [April 30, 2012, 6:12pm UTC](https://forum.ansible.com/t/evaluation-of-python-expressions-only-in-only-if-and-not-in-vars-section-of-a-playbook/11470/2 "2012-04-30T18:12:04Z")

</div>

You can save expressions in vars and use them in only\_if statements, I included this in previous emails to the list, in fact.

---

<div class="post-metadata">

**Author:** ![Matthew\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Matthew\_Williams](https://forum.ansible.com/u/Matthew_Williams)\
**Post date:** [April 30, 2012, 6:30pm UTC](https://forum.ansible.com/t/evaluation-of-python-expressions-only-in-only-if-and-not-in-vars-section-of-a-playbook/11470/3 "2012-04-30T18:30:21Z")

</div>

Also, I believe you can use jinja expressions (subset of python, basically), inside {{ }} tags, e.g., {{ var1 == var2 }}.

---

<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:** [April 30, 2012, 6:39pm UTC](https://forum.ansible.com/t/evaluation-of-python-expressions-only-in-only-if-and-not-in-vars-section-of-a-playbook/11470/4 "2012-04-30T18:39:48Z")

</div>

For only\_if, yes, as long as they are quoted.
