I'm looking to return value based on query result, so far I'm not getting the desired result. json looks like this and is coming from ansible var
{
"header": {
"nodes": {
"node1": {
"last_shutdown": "date",
"level": {
"kind_node": {}
},
"state": {
"running": "true",
"more": {
"type": "admin"
}
}
},
"node2": {
"last_shutdown": "date",
"level": {
"kind_node": {}
},
"state": {
"running": "true",
"more": {
"type": "engine"
}
}
},
"node3": {
"last_shutdown": "date",
"level": {
"kind_node": {}
},
"state": {
"running": "true",
"more": {
"type": "engine"
}
}
}
}
}
}
So far I've tried to use dict2items but no luck. Currently, I'm getting an empty list.
set_fact:
my_var: "{{ nodes | dict2items | json_query('[?value.state.more.type==`admin`]') }}"
Basically, this task should set my_var ==> node1 since it's the admin. Any help is greatly appreciated.
You don't need json_query for this. It would work but I strongly suggest you use it only when default core ansible filters cannot do the job at all.
Moreover, your jmespath expression (which I did not test to validate) would return a full list of elements where the parameter is matched, not simply a node name.
In a nutshell (note: updated so that it will work even if some element do not contain the more attribute inside the state key)
- set_fact:
my_var: >-
{{
nodes
| dict2items
| selectattr('value.state.more', 'defined')
| selectattr('value.state.more.type', '==', 'admin')
| map(attribute='key')
| first
}}
In other words:
key/value pairsvalue.state.more defined"admin" in the value.state.more.type parameterkey parameter for each elementIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With