From your question and different comments, I'm not exactly sure what you want to calculate. So here is a very basic example getting a sum and an average over all hosts.
You can have a look a the documentations for jinja2 filter and ansible filters for more info on the expression I used in my playbook.
Please note that jinja2 outputs strings so values passed to set_fact
are converted back to strings. Hence the use of numerous float
filters at different points in the playbook is not an over-precaution but absolutely needed.
I used the following "fake" inventory ìnv.yml
of two hosts all resolving to my local machine.
---
all:
vars:
ansible_connection: local
hosts:
test1:
test2:
And the test.yml
playbook:
---
- hosts: all
gather_facts: false
tasks:
- name: Run ping command
shell: ping -c 1 -W 1 192.168.0.2
register: ping_cmd
changed_when: false
- name: set ping time for each host
set_fact:
ping_time: "{{ ((ping_cmd.stdout_lines[1] | split('='))[3] | split(' '))[0] }}"
- name: set total ping time once for all hosts
set_fact:
total_ping_time: "{{ play_hosts | map('extract', hostvars)
| map(attribute='ping_time') | map('float') | sum }}"
run_once: true
- name: set mean ping time once for all hosts
set_fact:
mean_ping_time: "{{ ((total_ping_time | float) / (play_hosts | length)) }}"
run_once: true
- name: show the result
debug:
msg: "For this host, ping_time is {{ ping_time }}s
for a total of {{ total_ping_time | float | round(3) }}s
and an average of {{ mean_ping_time | float | round(3) }}s"
Which gives:
$ ansible-playbook -i inv.yml test.yml
PLAY [all] ********************************************************************************************************************************************************************************************************
TASK [Run ping command] *******************************************************************************************************************************************************************************************
ok: [test1]
ok: [test2]
TASK [set ping time for each host] ********************************************************************************************************************************************************************************
ok: [test1]
ok: [test2]
TASK [set total ping time once for all hosts] *********************************************************************************************************************************************************************
ok: [test1]
TASK [set mean ping time once for all hosts] **********************************************************************************************************************************************************************
ok: [test1]
TASK [show the result] ********************************************************************************************************************************************************************************************
ok: [test1] => {
"msg": "For this host, ping_time is 0.137s for a total of 0.214s and an average of 0.107s"
}
ok: [test2] => {
"msg": "For this host, ping_time is 0.077s for a total of 0.214s and an average of 0.107s"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
test1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0