I'd like to store every return values to a single dict with key is hostname and value is output then find the maximum number.
hosts: bg-workers
gather_facts: true
become: yes
tasks:
- name: Find cpu idle
shell: iostat -c | awk '/^ /{print $6}'
register: cpuPerc_idle
- name: Setting CPU facts
set_fact:
cpu_idle: "{{ cpuPerc_idle.stdout }}"
- name: Print CPU idle
ansible.builtin.debug:
msg: "{{ cpu_idle }}"
Currently, the output are store independently
ok: [bg-1] => {
"msg": [
"48.30"
]
}
ok: [bg-2] => {
"msg": [
"47.98"
]
}
ok: [bg-3] => {
"msg": [
"45.03"
]
}
ok: [bg-4] => {
"msg": [
"77.94"
]
}
my desired pattern is
ansible_cpuPerc_idle={ bg-1:'48.30', bg-2:'47.98', bg-3: '45.03', bg-4: '77.94'} and then I can easily find the maximum from its value.
the closet that I can produce is
"cpus_idle": {
"bg-1": "47.8",
"bg-2": "47.8",
"bg-3": "47.8",
"bg-4": "47.8"
}
}
from the following jinja loop
- name: Print %cpu idle
vars:
cpus_idle: |
{%- set o=dict() %}
{%- for i in groups['bg-workers'] %}
{%- set key=i %}
{%- if key in o %}
{%- set _dummy = o.update( {key: o[i]+1} ) %}
{%- else %}
{%- set _dummy = o.update( {key: cpu_idle }) %}
{%- endif %}
{%- endfor %}
{{ o }}
which is always gives the same cpu_idle(result from 1st node) cause it doesn't loop with i variable. I've tried many ways such as {%- set _dummy = o.update( {key: i["cpu_idle"] }) %} for trying to access defined fact, it ends up with "AnsibleUndefined"