Search code examples
ansiblejinja2

Ansible: How to extract values from a dictionary of lists


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

Using Linux server with installed versions of ansible 2.7.5 & jinja2 2.7.2 .

The below one is the list of dict value.

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) }}"

Output which I'm getting is:

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}]

Required output:

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

I expect something like below in list or dict format:

CACHE01: True ,CACHE02: True, SCACHE01:False, SCACHE02: False

The above values should get stored in one variable.


Solution

  • 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) }}'