Ansible:如何从列表字典中提取值

anu*_*anu 5 jinja2 ansible

我正在尝试从 dict 列表中获取值,但无法获得所需的准确输出

使用安装了 ansible 2.7.5 和 jinja2 2.7.2 版本的 Linux 服务器。

下面是字典值的列表。

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
Run Code Online (Sandbox Code Playgroud)

到目前为止尝试使用以下代码:

- debug:
      msg: "KEY: {{ item.key }}, VALUE: {{ item.value }}"
    loop: "{{ lookup('dict', DOMAIN_GROUPS_ASSIGNMENT) }}"
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

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}]
Run Code Online (Sandbox Code Playgroud)

所需输出:

结果应采用字典格式,并应存储在一个变量中。

我期望列表或字典格式如下所示:

CACHE01:真,CACHE02:真,SCACHE01:假,SCACHE02:假

上述值应存储在一个变量中。

JGK*_*JGK 1

您必须value为您的结果选择正确的:

tasks:
- name: DEBUG
  debug:
    msg: 'KEY: {{ item.key }}, VALUE: {{ item.value.0.is_active }}'
  loop: '{{ lookup("dict", DOMAIN_GROUPS_ASSIGNMENT) }}'
Run Code Online (Sandbox Code Playgroud)