{% set groups = vlans | map(attribute='name') | select('match', '.+-.+') | map('split', '-') | map('first') | unique -%} {% set prefixes = query('netbox.netbox.nb_lookup', 'prefixes', raw_data=true) %} { {% for vlan in vlans %} {% set vlan_prefixes = prefixes | selectattr('vlan') | selectattr('vlan.id', '==', vlan.id) | map(attribute='prefix') %} "{{ vlan.name }}": { "ip": {{ vlan_prefixes | ipv4 | to_json }}, "ip6": {{ vlan_prefixes | ipv6 | to_json }} }{% if not loop.last or groups %},{% endif +%} {% endfor %} {% for group in groups %} {% set group_prefixes = prefixes | selectattr('vlan') | selectattr('vlan.name', 'match', '^'~group) | map(attribute='prefix') %} "{{ group }}": { "ip": {{ group_prefixes | ipv4 | to_json }}, "ip6": {{ group_prefixes | ipv6 | to_json }} }{% if not loop.last %},{% endif +%} {% endfor %} }