I create the dict vmoff2 using the following:
- name: create Not Activated VM list
set_fact:
vmoff: >-
{{ vmoff | default([])
+ [{
'sec_sys': item.item.SEC_SYS,
'vm_na': dict(item.stdout_lines
| from_yaml
| select()
| map('split', ',')
| list)
}]
}}
loop: "{{ output.results }}"
loop_control:
label: "{{ item.item.SEC_SYS }}"
And eliminate the duplicate output with:
- name: eliminate duplicate
set_fact:
vmoff2: "{{ vmoff2 | default([]) + [{'sec_sys': item.1.0.sec_sys, 'vm_na': item.1.0.vm_na }] }}"
loop: "{{ vmoff|groupby('sec_sys') }}"
Finally i have the following dict:
"vmoff2": [
{
"sec_sys": "CONT1",
"vm_na": {
"vm1": "Running",
"vm2": "Running",
"vm3": "Not Activated",
"vm4": "Not Activated",
"vm5": "Running",
"vm6": "Not Activated"
}
},
{
"sec_sys": "CONT2",
"vm_na": {
"vm1": "Not Activated",
"vm2": "Not Activated",
"vm3": "Running",
"vm4": "Running",
"vm5": "Not Activated",
"vm6": "Running"
}
}
]
---------------
"vmprof2": [
{
"proc": {
"vm1": "0.5",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.5",
"vm5": "0.5",
"vm6": "0.5"
},
"sec_sys": "CONT1"
},
{
"proc": {
"vm1": "1.0",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.7",
"vm5": "0.5",
"vm6": "0.7"
},
"sec_sys": "CONT2"
}
]
I want to sum the proc of the Not activated VM, and have the following output:
proc_steal:
- server: CONT1
proc: 1.7
- server: CONT2
proc: 2.4
I tried the following:
- name: create proc_steal list
set_fact:
proc_steal: >-
{{ proc_steal | default([])
+ [{
'sec_sys': item.sec_sys,
'proc': vmprof2
| selectattr('sec_sys', '==', item.sec_sys)
| selectattr('proc', '==', item.vm_na)
| sum(attribute="value")
| float
}]
}}
loop: "{{ vmoff2 }}"
loop_control:
label: "{{ item.sec_sys }}"
but i get:
"proc_steal": [
{
"proc": 0.0,
"sec_sys": "CONT1"
},
{
"proc": 0.0,
"sec_sys": "CONT2"
}
]
}
Is not making the sum, maybe because is not float, but when i tried to put float i have an error, any guess?
solved How to separate the output of register into dict