我有一个特定的JSON输出,如下所示,它使用在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"
}
}
我如何在Ansible中对此进行解析,并使用type = Master作为选择该节点IP地址的一种方式注册一个新变量?在上面的示例中,新的Ansible变量应设置为“ 100.29.28.153” ,因为该IP地址是主节点
答案 0 :(得分:1)
例如,将dict2items
过滤器与JMESPath结合使用:
- set_fact:
my_var: "{{ nodes | dict2items | json_query('[?value.type==`Master`] | [0].key') }}"