# Getting sum of gathered facts

**URL:** <https://forum.ansible.com/t/getting-sum-of-gathered-facts/34404>\
**Category:** Ansible Project\
**Created:** [January 6, 2021, 2:13pm UTC](https://forum.ansible.com/t/getting-sum-of-gathered-facts/34404 "2021-01-06T14:13:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [January 6, 2021, 2:13pm UTC](https://forum.ansible.com/t/getting-sum-of-gathered-facts/34404/1 "2021-01-06T14:13:09Z")

</div>

A few weeks ago, you guys helped me get the sum of gluster healings occurring on our gluster volumes. So now I have 3 facts that have sums of each of our 3 volumes. I’d like to sum those now as its own fact, but its giving me the output as a string. How can I actually sum the 3 values?

- name: Set volume healing facts  
set\_fact:  
home\_sum: “{{ home\_heal\_status|json\_query(‘glusterfs.heal\_info.no\_of\_entries’)|map(‘int’)|sum }}”  
nas\_sum: “{{ nas\_heal\_status|json\_query(‘glusterfs.heal\_info.no\_of\_entries’)|map(‘int’)|sum }}”  
projects\_sum: “{{ projects\_heal\_status|json\_query(‘glusterfs.heal\_info.no\_of\_entries’)|map(‘int’)|sum }}”

- name: Get healing total  
set\_fact:  
total: “{{ home\_sum|int }} + {{ nas\_sum|int }} + {{ projects\_sum|int }}”

home\_sum is 2, nas\_sum is 0, and projects\_sum is 0. So I’d assume that the sum would be 2, however what is being calculated for the fact “total” is “2+0+0”. I need the actual sum.

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 6, 2021, 2:42pm UTC](https://forum.ansible.com/t/getting-sum-of-gathered-facts/34404/2 "2021-01-06T14:42:29Z")

</div>

> &nbsp;&nbsp;- name: Get healing total  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total: "{{ home\_sum|int }} + {{ nas\_sum|int }} + {{ projects\_sum|int }}"

You go in and out of jinja templating, but the entire expression is  
still a string.

> home\_sum is 2, nas\_sum is 0, and projects\_sum is 0. So I'd assume that the sum would be 2, however what is being calculated for the fact "total" is "2+0+0". I need the actual sum.

Doing the calculation entirely in jinja should work, i.e.:

total: "{{ home\_sum|int + nas\_sum|int + projects\_sum|int }}"

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 6, 2021, 2:42pm UTC](https://forum.ansible.com/t/getting-sum-of-gathered-facts/34404/3 "2021-01-06T14:42:40Z")

</div>

> A few weeks ago, you guys helped me get the sum of gluster healings occurring on our gluster volumes. So now I have 3  
> facts that have sums of each of our 3 volumes. I'd like to sum those now as its own fact, but its giving me the output  
> as a string. How can I actually sum the 3 values?
> 
> - name: Set volume healing facts  
> set\_fact:  
> home\_sum: "{{ home\_heal\_status|json\_query('glusterfs.heal\_info.no\_of\_entries')|map('int')|sum }}"  
> nas\_sum: "{{ nas\_heal\_status|json\_query('glusterfs.heal\_info.no\_of\_entries')|map('int')|sum }}"  
> projects\_sum: "{{ projects\_heal\_status|json\_query('glusterfs.heal\_info.no\_of\_entries')|map('int')|sum }}"
> 
> - name: Get healing total  
> set\_fact:  
> total: "{{ home\_sum|int }} + {{ nas\_sum|int }} + {{ projects\_sum|int }}"
> 
> home\_sum is 2, nas\_sum is 0, and projects\_sum is 0. So I'd assume that the sum would be 2, however what is being  
> calculated for the fact "total" is "2+0+0". I need the actual sum.

You are asking for separate calculations by using curly braces for each sum.

So please try something like: "{{ home\_sum|int + nas\_sum|int + projects\_sum|int }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 6, 2021, 2:46pm UTC](https://forum.ansible.com/t/getting-sum-of-gathered-facts/34404/4 "2021-01-06T14:46:44Z")

</div>

Try this

&nbsp;&nbsp;- name: Get healing total  
&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total: "{{ home\_sum|int + nas\_sum|int + projects\_sum|int }}"

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [January 6, 2021, 2:54pm UTC](https://forum.ansible.com/t/getting-sum-of-gathered-facts/34404/5 "2021-01-06T14:54:19Z")

</div>

Thanks! That worked. I thought I had done that but I guess my syntax was just a little off. Works now.

I appreciate the quick responses!  
Harry
