I have a variable named myvar, which is defined as follows
- debug: var=myvar
ok: [127.0.0.1] => {
"myvar": {
"name": "george"
"description": "blah blah"
}
}
I want to search for that variable based on the name attribute. For example, I want to provide the name "george" and retrieve the upper level myvar variable.
- name: search
set_fact:
list_of_matched_vars: "{{ vars | json_query(query) }}"
vars:
query: "[?[].name=='george']"
The above returns an empty result. I need to somehow represent that I want to search for any variable name, which has the attribute "name" matching to the value "george". In fact I want to search for a list of matching values, like "george, helen", however, I guess this would be even trickier so lets take the simpler case first.
How can I search for the above myvar variable based on it's 'name' attribute?
I am not sure this is the best way, but here is how I ended up doing it
- name: search
set_fact:
list_of_matched_vars: "{{ vars | dict2items | selectattr('value.name', 'defined') | selectattr('value.name', 'in', list_of_names_to_search_for) | list }}"
Let me know if anyone can think of a better way of doing this.