I feel like I'm losing my god damn mind about this. It seems so easy but I just can't make it work.
The completely first input data is this:
IPs: [ 'ip1', 'ip2' ]
DICT: { 'ip1': 'reg1', 'ip2': 'reg2', 'ip3': 'reg2', 'ip4': 'reg3', ... }
I managed to make 2 lists out of them which I thought would be easier.
list1: {{ DICT | dict2items() | selectattr('key', 'in', IPs) | list | map(attribute='value') | list }}
list2: {{ DICT | dict2items() | selectattr('key', 'in', IPs) | list | map(attribute='key') | list }}
-----
list1: [ 'reg1', 'reg2', 'reg2' ]
list2: [ 'ip1', 'ip2', 'ip3']
I need it condensed into this format
{ 'reg1': 'ip1', 'reg2': [ 'ip2', 'ip3' ] }
or this is also acceptable
{ 'reg1': [ 'ip1' ], 'reg2': [ 'ip2', 'ip3' ] }
This "gives" me what I want, but the moment I make it a dict it just does not condense on its own.
{{ dict(list1 | zip(list2) | list }}
gives
{ 'reg1': 'ip1', 'reg2': 'ip2' }
The closest I've been so far is this but I just don't know how to convert the values back from the key/value list of dict.
{{ dict(dict(list2 | zip(list1)) | dict2items() | groupby(attribute='value') | list ) }}
gets me this \|/
{ 'reg2': [{'key': 'ip2', 'value': 'reg2'}, {'key': 'ip3', 'value': 'reg2'}], 'reg1': [{'key': 'ip1', 'value': 'reg1'}] }
Is there absolutely no way how to make it in Jinja2 WITHOUT using loop/with_items?