Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible filter to extract specific keys from a dict into another dict

Using the nios lookup modules, I can get a list of dicts of records

- set_fact:
    records: "{{ lookup('community.general.nios', 'record:a', filter={'name~': 'abc.com'}) }}"

This returns something like

- ref: record:a/someBase64:name/view
  name: abc.com
  ipv4addr: 1.2.3.4
  view: default
- ref: record:a/someBase64:name/view
  name: def.abc.com
  ipv4addr: 1.2.3.5
  view: default
- ref: record:a/someBase64:name/view
  name: ghi.abc.com
  ipv4addr: 1.2.3.6
  view: default

I want to convert this into a dict of dicts of {name}: a: {ipv4addr}

abc.com:
  a: 1.2.3.4
def.abc.com:
  a: 1.2.3.5
ghi.abc.com:
  a: 1.2.3.6

So that I can then run a similar lookup to get other record types (e.g. cname) and combine them into the same dict. The items2dict filter seems halfway there, but I want the added a: key underneath.

like image 429
Gordon Avatar asked Sep 07 '25 05:09

Gordon


1 Answers

If you just wanted a dictionary that maps name to an ipv4 address, like:

{
  "abc.com": "1.2.3.4",
  ...
}

You could use a simple json_query expression. Take a look at the set_fact task in the following example:

- hosts: localhost
  gather_facts: false
  vars:
    data:
      - ref: record:a/someBase64:name/view
        name: abc.com
        ipv4addr: 1.2.3.4
        view: default
      - ref: record:a/someBase64:name/view
        name: def.abc.com
        ipv4addr: 1.2.3.5
        view: default
      - ref: record:a/someBase64:name/view
        name: ghi.abc.com
        ipv4addr: 1.2.3.6
        view: default
  tasks:
    - set_fact:
        name_map: "{{ dict(data|json_query('[].[name, ipv4addr]')) }}"

    - debug:
        var: name_map

Running that playbook will output:

PLAY [localhost] ***************************************************************

TASK [set_fact] ****************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "name_map": {
        "abc.com": "1.2.3.4",
        "def.abc.com": "1.2.3.5",
        "ghi.abc.com": "1.2.3.6"
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

You could use a similar structure to extract other data (e.g. cname records). This would get you dictionary per type of data, rather than merging everything together in a single dictionary as you've requested, but this might end up being easier to work with.


To get exactly the structure you want, you can use set_fact in a loop, like this:

- hosts: localhost
  vars:
    data:
      - ref: record:a/someBase64:name/view
        name: abc.com
        ipv4addr: 1.2.3.4
        view: default
      - ref: record:a/someBase64:name/view
        name: def.abc.com
        ipv4addr: 1.2.3.5
        view: default
      - ref: record:a/someBase64:name/view
        name: ghi.abc.com
        ipv4addr: 1.2.3.6
        view: default

  gather_facts: false
  tasks:
    - set_fact:
        name_map: "{{ name_map|combine({item.name: {'a': item.ipv4addr}}) }}"
      loop: "{{ data }}"
      vars:
        name_map: {}

    - debug:
        var: name_map

This will produce:

PLAY [localhost] ***************************************************************

TASK [set_fact] ****************************************************************
ok: [localhost] => (item={'ref': 'record:a/someBase64:name/view', 'name': 'abc.com', 'ipv4addr': '1.2.3.4', 'view': 'default'})
ok: [localhost] => (item={'ref': 'record:a/someBase64:name/view', 'name': 'def.abc.com', 'ipv4addr': '1.2.3.5', 'view': 'default'})
ok: [localhost] => (item={'ref': 'record:a/someBase64:name/view', 'name': 'ghi.abc.com', 'ipv4addr': '1.2.3.6', 'view': 'default'})

TASK [debug] *******************************************************************
ok: [localhost] => {
    "name_map": {
        "abc.com": {
            "a": "1.2.3.4"
        },
        "def.abc.com": {
            "a": "1.2.3.5"
        },
        "ghi.abc.com": {
            "a": "1.2.3.6"
        }
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

like image 161
larsks Avatar answered Sep 11 '25 00:09

larsks