Parse json output for a specific key in Ansible variable
Parse json output for a specific key in Ansible variable
I have a specific JSON output shown below using dummy data loaded inside a variable in Ansible:
"nodes":
"100.29.28.153":
"agent": "UP",
"db": "UP",
"info": " ",
"type": "Master",
"xlog": "6C\/2400A990",
"xloginfo": ""
,
"100.29.28.154":
"agent": "UP",
"db": "UP",
"info": " ",
"type": "Standby",
"xlog": "6C\/2400A990",
"xloginfo": ""
,
"100.29.28.155":
"agent": "UP",
"db": "N\/A",
"info": " ",
"type": "Witness"
,
"100.29.28.163":
"agent": "UP",
"db": "UP",
"info": " ",
"type": "Standby",
"xlog": "6C\/2400A990",
"xloginfo": ""
,
"100.29.28.165":
"agent": "UP",
"db": "N\/A",
"info": " ",
"type": "Witness"
How do I parse this in Ansible and register a new variable using the type=Master as a means of selecting that nodes IP address? In the example above, the new Ansible variable should be set to "100.29.28.153" since that IP address is the Master node
1 Answer
1
For example by combining dict2items
filter with JMESPath:
dict2items
- set_fact:
my_var: " json_query('[?value.type==`Master`] "
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
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.