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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)