Ansible:如何从列表字典中提取值 [英] Ansible: How to extract values from a dictionary of lists

查看:480
本文介绍了Ansible:如何从列表字典中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从字典列表中获取值,但无法获得确切的输出,这是必需的

I'm trying to fetch the values from list of dict but unable to get the exact output, which is required

将Linux服务器与已安装的ansible 2.7.5& jinja2 2.7.2.

Using Linux server with installed versions of 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

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

So far trying with the below code:

- 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格式,并应存储在一个变量中.

Result should be in dict format and should be stored in one variable.

我希望列表或字典格式的内容如下所示:

I expect something like below in list or dict format:

CACHE01:是,CACHE02:是,SCACHE01:否,SCACHE02:否

以上值应存储在一个变量中.

The above values should get stored in one variable.

推荐答案

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

You have to select the right value for your result:

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

这篇关于Ansible:如何从列表字典中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆