Search value in a dictionary with multiple value

Hi All,

I’ve tried to find a Network UUID where an IP is a member of pool.

Example:

i’ve an IP with variable [search_ip_address].

"search_ip_address": "192.168.1.11"

I know that this IP is a member of Network UUID in he variable [tmp_network_uuid]

"tmp_network_uuid": [
  "AAAAAAAAA-BBBBB-CCCCC-DDDDDD-EEEEEE",
  "FFFFFFFFF-GGGGG-HHHHH-IIIIII-JJJJJJ"
]

But this ip is configured only in one pool of network.
“AAAAAAAAA-BBBBB-CCCCC-DDDDDD-EEEEEE” or “FFFFFFFFF-GGGGG-HHHHH-IIIIII-JJJJJJ” UUID not in these network at same time.

I want to search in which network uuid the ip [search_ip_address] is a pool member in the variable [tmp_network_vlan_ips] below :

"tmp_network_vlan_ips": [
  {
    "network_name": "NET_A01",
    "network_uuid": "XXXXXX-YYYYY-XXXXXXX-XXXXX-YYYYYYYY",
    "ips_pool": [
      "192.168.1.1",
      "192.168.1.2"
    ]
  },
  {
    "network_name": "NET_B01",
    "network_uuid": "AAAAAAAAA-BBBBB-CCCCC-DDDDDD-EEEEEE",
    "ips_pool": [
      "192.168.1.10",
      "192.168.1.11"
    ]
  },
  {
    "network_name": "NET_C01",
    "network_uuid": "FFFFFFFFF-GGGGG-HHHHH-IIIIII-JJJJJJ",
    "ips_pool": [
      "192.168.1.20",
      "192.168.1.21"
    ]
  }
]

In the example above, i need to find the nework_name [NET_B01].

Do you know how can i do that ?

Thanks for your help,
Matt

You can use selectattr filter with contains filter, like so:

- ansible.builtin.debug:
    msg: "{{ found_network.network_name }}"
  vars:
    found_network: "{{ tmp_network_vlan_ips | selectattr('ips_pool', 'contains', search_ip_address) | first }}"
1 Like

Hi @kristianheljas ,

Thanks for your reply.

With your solution i’m able to find the Network but i need to add a filter on variable [tmp_network_uuid].
How can i add the filter on this variable which contains two values ?

Thanks for your help,
Matt

Use another selectattr filter with in test, like so:

tmp_network_vlan_ips | selectattr('network_uuid', 'in', tmp_network_uuid) | selectattr('ips_pool', 'contains', search_ip_address) | first
1 Like