来自 2 个列表的 SUM 值
SUM value from 2 lists
我有以下输出:
{
"vmoff": [
{
"CONT2": [
"vm1",
"vm2",
"vm5"
]
},
{
"CONT1": [
"vm3",
"vm4",
"vm6"
]
}
]
}
和
{
"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"
}
]
}
我想在vmoff中对每个vm的proc值求和,例如:
proc_steal:
- sec_sys: CONT1
proc: 2.0
- sec_sys: CONT2
proc: 2.2
获取未激活vm的proc,我对输出感到困惑,不知道使用list还是dict。
我建议您使用自定义过滤器:
创建一个文件夹 filter_plugins
与您的 playbook 处于同一级别。
我创建了一个文件 myfilter.py
,其中包含一个名为 extractfilter
的过滤器
.
├── filter_plugins
│ └── myfilter.py
└── playbook.yml
内容如下:
#!/usr/bin/python
class FilterModule(object):
def filters(self):
return {
'extractfilter': self.extractfilter
}
def extractfilter(self, l1, l2):
result = []
ky = {}
for k in l1:
ky.update(k)
for k in l2:
s = k['sec_sys']
l = ky[s]
if s in ky:
total = 0.0
proc = k['proc']
for p in proc:
if p in l:
total = total + float(proc[p])
result.append({'sec_sys': s, 'proc': total})
return result
然后是使用自定义过滤器的剧本:
- name: testplaybook jinja2
hosts: localhost
gather_facts: no
vars:
vmoff:
- CONT2:
- vm1
- vm2
- vm5
- CONT1:
- vm3
- vm4
- vm6
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
tasks:
- name: DEFINE VARIABLE SPINE
set_fact:
proc_steal: "{{ vmoff | extractfilter(vmprof2) }}"
- debug: msg="{{ proc_steal }}"
结果:
{
"msg": [
{
"proc": 2.0,
"sec_sys": "CONT1"
},
{
"proc": 2.2,
"sec_sys": "CONT2"
}
]
}
如果您对自定义过滤器有疑问,可以使用 jinja:
- name: "make this working"
hosts: localhost
vars:
vmoff:
- CONT2:
- vm1
- vm2
- vm5
- CONT1:
- vm3
- vm4
- vm6
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
tasks:
- name: list to dict
set_fact:
ky: "{{ ky | d({}) | combine( item ) }}"
loop: "{{ vmoff }}"
- name: list to dict
set_fact:
result: "{{ result| d([]) + [{'sec_sys': s, 'proc': total}] }}"
loop: "{{ vmprof2 }}"
vars:
s: "{{ item['sec_sys'] }}"
l: "{{ ky[s] }}"
total: >-
{%- set st = namespace(tt=0.0) -%}
{%- for p in item['proc'] if p in l -%}
{%- set st.tt = st.tt + (item['proc'][p] | float) -%}
{%- endfor -%}
{{ st.tt | float }}
when: s in ky
- name: list to dict
debug:
msg: "{{ result }}"
我有以下输出:
{
"vmoff": [
{
"CONT2": [
"vm1",
"vm2",
"vm5"
]
},
{
"CONT1": [
"vm3",
"vm4",
"vm6"
]
}
]
}
和
{
"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"
}
]
}
我想在vmoff中对每个vm的proc值求和,例如:
proc_steal:
- sec_sys: CONT1
proc: 2.0
- sec_sys: CONT2
proc: 2.2
获取未激活vm的proc,我对输出感到困惑,不知道使用list还是dict。
我建议您使用自定义过滤器:
创建一个文件夹 filter_plugins
与您的 playbook 处于同一级别。
我创建了一个文件 myfilter.py
,其中包含一个名为 extractfilter
.
├── filter_plugins
│ └── myfilter.py
└── playbook.yml
内容如下:
#!/usr/bin/python
class FilterModule(object):
def filters(self):
return {
'extractfilter': self.extractfilter
}
def extractfilter(self, l1, l2):
result = []
ky = {}
for k in l1:
ky.update(k)
for k in l2:
s = k['sec_sys']
l = ky[s]
if s in ky:
total = 0.0
proc = k['proc']
for p in proc:
if p in l:
total = total + float(proc[p])
result.append({'sec_sys': s, 'proc': total})
return result
然后是使用自定义过滤器的剧本:
- name: testplaybook jinja2
hosts: localhost
gather_facts: no
vars:
vmoff:
- CONT2:
- vm1
- vm2
- vm5
- CONT1:
- vm3
- vm4
- vm6
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
tasks:
- name: DEFINE VARIABLE SPINE
set_fact:
proc_steal: "{{ vmoff | extractfilter(vmprof2) }}"
- debug: msg="{{ proc_steal }}"
结果:
{
"msg": [
{
"proc": 2.0,
"sec_sys": "CONT1"
},
{
"proc": 2.2,
"sec_sys": "CONT2"
}
]
}
如果您对自定义过滤器有疑问,可以使用 jinja:
- name: "make this working"
hosts: localhost
vars:
vmoff:
- CONT2:
- vm1
- vm2
- vm5
- CONT1:
- vm3
- vm4
- vm6
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
tasks:
- name: list to dict
set_fact:
ky: "{{ ky | d({}) | combine( item ) }}"
loop: "{{ vmoff }}"
- name: list to dict
set_fact:
result: "{{ result| d([]) + [{'sec_sys': s, 'proc': total}] }}"
loop: "{{ vmprof2 }}"
vars:
s: "{{ item['sec_sys'] }}"
l: "{{ ky[s] }}"
total: >-
{%- set st = namespace(tt=0.0) -%}
{%- for p in item['proc'] if p in l -%}
{%- set st.tt = st.tt + (item['proc'][p] | float) -%}
{%- endfor -%}
{{ st.tt | float }}
when: s in ky
- name: list to dict
debug:
msg: "{{ result }}"