# Calculating with Variables?

**URL:** <https://forum.ansible.com/t/calculating-with-variables/17177>\
**Category:** Ansible Project\
**Created:** [January 4, 2015, 1:02am UTC](https://forum.ansible.com/t/calculating-with-variables/17177 "2015-01-04T01:02:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ulrich\_Hochholdinger](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@Ulrich\_Hochholdinger](https://forum.ansible.com/u/Ulrich_Hochholdinger)\
**Post date:** [January 4, 2015, 1:02am UTC](https://forum.ansible.com/t/calculating-with-variables/17177/1 "2015-01-04T01:02:03Z")

</div>

Hi,  
I just started playing around with ansible and tried to achieve a simple operations task with it:

Iterate over all nodes of my Xen-Cluster – get free\_memory of every node and sum up those values.  
next step would be making a decision e.g. spawning a new VM without overprovisioning the cluster.

So I wrote a simple module which collects “xm info” values as ansible\_facts. This was easy…

Next step: write a playbook which sums up one of the collected fact (free\_memory) – here I got stuck and have no more ideas how to solve this…

This is a not working idea:

# vim:ft=ansible:

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 5, 2015, 1:48pm UTC](https://forum.ansible.com/t/calculating-with-variables/17177/2 "2015-01-05T13:48:39Z")

</div>

Hi Ulrich

How about a ‘for’ loop (in Jinja2) contained as a parameter to the set\_fact module, iterating the facts you gathered as presented in the `{{ hostvars }}` magic variable?

Regards  
Tom

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [January 5, 2015, 2:10pm UTC](https://forum.ansible.com/t/calculating-with-variables/17177/3 "2015-01-05T14:10:14Z")

</div>

for loops in Jinja2 can’t be used (with any definition of sanity) in Ansible playbooks.

They can be used in templates used by the template module.

Let’s step back a moment…

Can you remove the recursive reference and just call it “actual\_free\_mem” or something, and see where you are from there.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Hochholdinger](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@Ulrich\_Hochholdinger](https://forum.ansible.com/u/Ulrich_Hochholdinger)\
**Post date:** [January 6, 2015, 12:26am UTC](https://forum.ansible.com/t/calculating-with-variables/17177/4 "2015-01-06T00:26:53Z")

</div>

Hi,  
Many thanks for these ideas, since this posting was held back for moderation (it was my first one in this group) I asked the question on irc channel and got the tip: simply write a filter to collect the necessary data to a list which can be summed up.“ktosiek” from irc channel wrote me the start of a filter which I modified to solve my problem 😃

this is the Filter:

def multiget(dictionary, keys):  
return [int(dictionary[k][‘xm\_infos’][‘free\_memory’]) for k in keys]

which can then be used in a playbook like this:

- name: show the sum  
debug: msg=“{{ hostvars | multiget(keys=groups.Cluster)|sum }}”

The first Idea was to have the filter more generally to use it like this:  
debug: msg=“{{ hostvars | multiget(keys=groups.Cluster)|sum(attribute=xm\_infos.free\_memory) }}”  
But the values inside the list must be casted to int and I had no other Idea than to do this inside the generation of the list.

Cheers  
Ulli

PS: I like ansible more and more especially the really cool and helpfull community ;-D
