# Maths on variables in tasks/playbooks

**URL:** <https://forum.ansible.com/t/maths-on-variables-in-tasks-playbooks/12570>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [March 24, 2013, 2:27am UTC](https://forum.ansible.com/t/maths-on-variables-in-tasks-playbooks/12570 "2013-03-24T02:27:34Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [March 24, 2013, 2:27am UTC](https://forum.ansible.com/t/maths-on-variables-in-tasks-playbooks/12570/1 "2013-03-24T02:27:34Z")

</div>

You can do things like this trivially in Jinaj2 if you are using templates.

I am considering for Ansible 1.2 allowing {{ foo }} syntax in  
playbooks (as the preferred form, actually... but it may not actually  
be Jinja2, in which case, yes, this will be possible.

What you ask for is basically an "$EVAL" lookup plugin, which is  
something I've been hesistant to add because it needs to be very well  
sandboxed and I want to make sure the syntax is decent.

In general, I dislike the look of lookup macros, so this is why I'm  
pushing for {{ foo / 2 }} being possible in 1.2.

I understand the syntax you've shown is somewhat suboptimal, but it's  
what you have for now.

I would of course be very tempted to not use Lineinfile -- I \*ALWAYS\*  
try not to, and just use a template, in which case this becomes easy  
and you can use math trivally.

--Michael

---

<div class="post-metadata">

**Author:** ![matt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matt/32/1267_2.png) [@matt](https://forum.ansible.com/u/matt)\
**Post date:** [March 25, 2013, 10:55pm UTC](https://forum.ansible.com/t/maths-on-variables-in-tasks-playbooks/12570/2 "2013-03-25T22:55:23Z")

</div>

> You can do things like this trivially in Jinaj2 if you are using templates.
> 
> I am considering for Ansible 1.2 allowing {{ foo }} syntax in  
> playbooks (as the preferred form, actually… but it may not actually  
> be Jinja2, in which case, yes, this will be possible.

{{ foo }} syntax was what I was hoping for. It opens a whole bunch of possibilities and is consistant. Even better (from my user point of view) if it’s Jinja2 as what works in templates would work in playbooks.

> What you ask for is basically an “$EVAL” lookup plugin, which is  
> something I’ve been hesistant to add because it needs to be very well  
> sandboxed and I want to make sure the syntax is decent.

Simple template-like function is enough. I don’t really want to be shelling out to do this stuff.

> I understand the syntax you’ve shown is somewhat suboptimal, but it’s  
> what you have for now.

I can dig that. It’s more a matter of aesthetics 🙂

> I would of course be very tempted to not use Lineinfile – I _ALWAYS_  
> try not to, and just use a template, in which case this becomes easy  
> and you can use math trivally.

The nice thing about lineinfile is I can leave everything at the OS defaults apart from the bits I want to modify which is useful where configs include paths and the like, though I take the point about templates.

Matt
