Getting sum of gathered facts

23 views
Skip to first unread message

lift...@gmail.com

unread,
Jan 6, 2021, 9:13:10 AM1/6/21
to Ansible Project
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

Dick Visser

unread,
Jan 6, 2021, 9:43:08 AM1/6/21
to ansible...@googlegroups.com
On Wed, 6 Jan 2021 at 15:13, lift...@gmail.com <lift...@gmail.com> wrote:

> - name: Get healing total
> set_fact:
> 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 }}"

--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Stefan Hornburg (Racke)

unread,
Jan 6, 2021, 9:43:13 AM1/6/21
to ansible...@googlegroups.com
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
Racke

> Thanks,
> Harry
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/51c68842-b665-44e0-bbef-a5158cc6ba9bn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/51c68842-b665-44e0-bbef-a5158cc6ba9bn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Vladimir Botka

unread,
Jan 6, 2021, 9:47:11 AM1/6/21
to lift...@gmail.com, ansible...@googlegroups.com
On Wed, 6 Jan 2021 06:13:09 -0800 (PST)
"lift...@gmail.com" <lift...@gmail.com> wrote:

> - name: Get healing total
> set_fact:
> total: "{{ home_sum|int }} + {{ nas_sum|int }} + {{ projects_sum|int

Try this

- name: Get healing total
set_fact:
total: "{{ home_sum|int + nas_sum|int + projects_sum|int }}"

--
Vladimir Botka

lift...@gmail.com

unread,
Jan 6, 2021, 9:54:19 AM1/6/21
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages