I have the following dictionary
grouped:
10000:
- {Id: 10001, Name: North_America, Parent_Id: 10000, Type: Country}
10001:
- {Id: 10011, Name: Maine, Parent_Id: 10001, Type: State}
- {Id: 10012, Name: Colorado, Parent_Id: 10001, Type: State}
- {Id: 10013, Name: Texas, Parent_Id: 10001, Type: State}
10011:
- {Id: 10101, Name: Augusta, Parent_Id: 10011, Type: City}
- {Id: 10102, Name: Portland, Parent_Id: 10011, Type: City}
10012:
- {Id: 10103, Name: Denver, Parent_Id: 10012, Type: City}
10013:
- {Id: 10104, Name: Austin, Parent_Id: 10013, Type: City}
- {Id: 10105, Name: Houston, Parent_Id: 10013, Type: City}
10101:
- {Id: 11001, Name: First_st, Parent_Id: 10101, Type: Street}
- {Id: 11002, Name: Second_st, Parent_Id: 10101, Type: Street}
10102:
- {Id: 11003, Name: First_st, Parent_Id: 10102, Type: Street}
- {Id: 11004, Name: Second_st, Parent_Id: 10102, Type: Street}
10104:
- {Id: 11005, Name: First_st, Parent_Id: 10104, Type: Street}
- {Id: 11006, Name: Second_st, Parent_Id: 10104, Type: Street}
If I'm given a string containing Country-State-City-Street, eg North_America-Texas-Austin-First_st, I want to
- Search the disc for North_America, if it exists collect it's ID.
- Using the ID of North_America, search the Key of the same value and check that it contains a value with name == Texas. If it exists, collect the ID of Texas
- Using the id of Texas, search the key of the same value and check that it contains a value with name == Austin
- Repeat for street...
The end goal, if the search string "North_America-Texas-Austin-Third_st" is provided and it does not exist in the dict, it will be added
First step is to identify the required Key, I've attempted json_queries and selectattr to do this but none work
---
hosts: localhost
gather_facts: false
connection: local
vars:
target: "North_America-Colorado-Denver-First_st"
tasks:
- name: Get the id using json filter and jmespath query
set_fact:
group_id: "{{ grouped | json_query(ID_query) | map(attribute='key') }}"
vars:
ID_query: '[?[*].Name == `North-America`]'
- debug: var=group_id
- name: Get the id using selectattr
set_fact:
group_id2: "{{ grouped | dict2items | selectattr('Name', 'search', 'North_America') }}"
- debug: var=group_id2
Output from the first debug is an empty list
ok: [localhost] => {
"group_id": []
}
Output from the second debug says that the dict object has no attribute Name
Create a dictionary of ids
gives
Then, use it. For example
gives abridged