Ansible - joining two nested lists without the same key
Ansible - joining two nested lists without the same key
I am currently trying to combine two different dictionaries together. The thing that ties them together is the login field, but this is not the "key" in both dicts.
---
- hosts: localhost
gather_facts: no
vars:
userlist:
tom123:
guid: "tom"
sally:
guid: "sally"
userteamlist:
tom:
teams: "group4, group6, group5"
sally:
teams: "group1, group2, group3"
How do I get these two lists to merge based on the "guid"? The "guid" is the key in one list (userteamlist) and a "value" (guid) in the other list (userlist).
I have used this Stackoverflow link for joining two dicts with the same key, but have not figured out to do this with key+subelement.
Thoughts?
1 Answer
1
In a short dictionary comprehension:
userlist = {'tom123': {'guid': 'tom'}, 'sally': {'guid': 'sally'}}
userteamlist = {
'tom': {'teams': 'group4 group6 group5'.split()},
'sally': {'teams': 'group1 group2 group3'.split()}
}
merged = {
name: {**user, **userteamlist[user['guid']]}
for name, user in userlist.items()
}
I assume this is python? I just need to figure out hot that translates to ansible?
– Ben Ptacek
14 hours ago
@BenPtacek You tagged your question with python, so I assumed you wanted to use python
– Jesse Bakker
14 hours ago
my apologies... this is for ansible which incorporates python
– Ben Ptacek
33 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Apologies all, I am looking strictly for an Ansible solution to this problem.
– Ben Ptacek
30 mins ago