'Convert ansible variables with wildcard sub-keys into a list

I'm being passed the extra_vars following payload:

pool_servers:
    '0':
      ip_address: 111.111.111.1
    '1':
      ip_address: 111.111.111.2

My problem is the 0 and 1 keys, I would have much rather received ip_address objects directly under pool_servers without the 0,1, etc. I don't know how many of these could potentially be passed.

Is there some way I can wildcard lookup all 'ip_address'es?

Something like...

set_stats:
      data:
        nodes: "{{ nodes  + item.[*].ip_address }}"
      loop: "{{ pool_servers }}"

This obviously isn't working but hopefully explains the end goal.

Ideally want a var like this:

nodes:
  - 111.111.111.1
  - 111.111.111.2


Solution 1:[1]

Either use dict2items to convert the dictionary into a list and select the attribute

nodes: "{{ pool_servers|dict2items|map(attribute='value.ip_address')|list }}"

,or use json_query

nodes: "{{ pool_servers|json_query('*.ip_address') }}"

Both options give the same result

nodes:
  - 111.111.111.1
  - 111.111.111.2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vladimir Botka