我正在尝试从字典列表中获取值,但无法获得确切的输出,这是必需的
在Linux服务器上使用ansible 2.7.5和jinja2 2.7.2的安装版本。
下面的是dict值列表。
DOMAIN_GROUPS_ASSIGNMENT:
CACHE01:
- domain_group: DG1
is_active: true
- domain_group: DG2
is_active: true
- domain_group: DG3
is_active: true
CACHE02:
- domain_group: DG4
is_active: true
- domain_group: DG5
is_active: true
- domain_group: DG6
is_active: true
SCACHE01:
- domain_group: DG1
is_active: false
- domain_group: DG2
is_active: false
- domain_group: DG3
is_active: true
SCACHE02:
- domain_group: DG4
is_active: false
- domain_group: DG5
is_active: false
- domain_group: DG6
is_active: false
到目前为止,尝试使用以下代码:
- debug:
msg: "KEY: {{ item.key }}, VALUE: {{ item.value }}"
loop: "{{ lookup('dict', DOMAIN_GROUPS_ASSIGNMENT) }}"
我得到的输出是:
TASK [debug] ************************************************************************************************************************************************
task path: /u02/netcracker/reir1015_test/singlesite/test.yml:7
Friday 31 May 2019 08:54:59 -0400 (0:00:00.058) 0:00:00.897 ************
ok: [localhost] => (item={'key': u'CACHE01', 'value': [{u'is_active': True, u'domain_group': u'DG1'}, {u'is_active': True, u'domain_group': u'DG2'}, {u'is_active': True, u'domain_group': u'DG3'}]}) => {}
MSG:
KEY: CACHE01, VALUE: [{u'domain_group': u'DG1', u'is_active': True}, {u'domain_group': u'DG2', u'is_active': True}, {u'domain_group': u'DG3', u'is_active': True}]
ok: [localhost] => (item={'key': u'SCACHE02', 'value': [{u'is_active': False, u'domain_group': u'DG4'}, {u'is_active': False, u'domain_group': u'DG5'}, {u'is_active': False, u'domain_group': u'DG6'}]}) => {}
MSG:
KEY: SCACHE02, VALUE: [{u'domain_group': u'DG4', u'is_active': False}, {u'domain_group': u'DG5', u'is_active': False}, {u'domain_group': u'DG6', u'is_active': False}]
ok: [localhost] => (item={'key': u'SCACHE01', 'value': [{u'is_active': False, u'domain_group': u'DG1'}, {u'is_active': False, u'domain_group': u'DG2'}, {u'is_active': True, u'domain_group': u'DG3'}]}) => {}
MSG:
KEY: SCACHE01, VALUE: [{u'domain_group': u'DG1', u'is_active': False}, {u'domain_group': u'DG2', u'is_active': False}, {u'domain_group': u'DG3', u'is_active': True}]
ok: [localhost] => (item={'key': u'CACHE02', 'value': [{u'is_active': True, u'domain_group': u'DG4'}, {u'is_active': True, u'domain_group': u'DG5'}, {u'is_active': True, u'domain_group': u'DG6'}]}) => {}
MSG:
KEY: CACHE02, VALUE: [{u'domain_group': u'DG4', u'is_active': True}, {u'domain_group': u'DG5', u'is_active': True}, {u'domain_group': u'DG6', u'is_active': True}]
必需的输出:
结果应为dict格式,并应存储在一个变量中。
我希望列表或字典格式的内容如下所示:
CACHE01:是,CACHE02:是,SCACHE01:否,SCACHE02:否
以上值应存储在一个变量中。
答案 0 :(得分:0)
您必须为结果选择正确的struct Base
{
virtual void read(std::istream& is) { ... }
};
std::istream& operator>> (std::istream &in, Base &b)
{
b.read(in);
return in;
}
struct Derived: Base
{
void read(std::istream& is) override { Base::read(is); ... }
};
:
value
答案 1 :(得分:0)
下面的任务
- debug:
msg: "{{ item.key }} {{ item.value|json_query('[].is_active') }}"
loop: "{{ DOMAIN_GROUPS_ASSIGNMENT|dict2items }}"
给予:
"msg": "CACHE01 [True, True, True]"
"msg": "SCACHE02 [False, False, False]"
"msg": "SCACHE01 [False, False, True]"
"msg": "CACHE02 [True, True, True]"
要结合逻辑值,我们创建filter_plugins / bool_utils.py 带有2个过滤器 bool_and 和 bool_or ,分别应用Python函数 all(list)和 any(list)。< / p>
> cat filter_plugins/bool_utils.py
def bool_and(h):
return all(h)
def bool_or(h):
return any(h)
class FilterModule(object):
''' utility filters for operating on list of Boolean '''
def filters(self):
return {
'bool_and' : bool_and
,'bool_or' : bool_or
}
下面的任务带有过滤器 bool_and
- debug:
msg: "{{ item.key }} {{ item.value|json_query('[].is_active')
|bool_and }}"
loop: "{{ DOMAIN_GROUPS_ASSIGNMENT|dict2items }}"
给予
"msg": "CACHE01 True"
"msg": "SCACHE02 False"
"msg": "SCACHE01 False"
"msg": "CACHE02 True"
下面的播放创建列表
- set_fact:
status: "{{ status|default([]) +
[{item.key: item.value|json_query('[].is_active')
|bool_and}] }}"
loop: "{{ DOMAIN_GROUPS_ASSIGNMENT|dict2items }}"
- debug:
var: status
给予
"status": [
{
"CACHE01": true
},
{
"SCACHE02": false
},
{
"SCACHE01": false
},
{
"CACHE02": true
}
]