json 在 ansible 中使用条件查询
json query in ansible with conditions
在 Ansible 中,我使用 json_query 和循环。
我想在 'contentId' 为“2565845434839sdsfc9we”时获取 'controllerKey'。
尝试了几件事,其中 none 似乎有效。有可能吗?
result": {
"hardware": {
"_vimtype": "vim.vm.VirtualHardware",
"device": [
{
"_vimtype": "vim.vm.device.VirtualDisk",
"backing": {
"contentId": "2565845434839sdsfc9we",
"writeThrough": false
},
"controllerKey": 1000,
},
{
"_vimtype": "vim.vm.device.VirtualDisk",
"backing": {
"contentId": "5264578434839sdsfc9rt",
"writeThrough": false
},
"controllerKey": 1001,
}
],
"memoryMB": 16384,
"numCPU": 2,
"numCoresPerSocket": 1,
"virtualICH7MPresent": false,
"virtualSMCPresent": false
}
下面的任务
- debug:
msg: "{{ result.hardware.device|
json_query('[?backing.contentId==`2565845434839sdsfc9we`].controllerKey')|
first }}"
给予
"msg": "1000"
也可以重复查询。下面的任务
- debug:
msg: "contentId: {{ item }}
controllerKey: {{ result.hardware.device|
json_query(query)|
first }}"
vars:
query: "[?backing.contentId=='{{ item }}'].controllerKey"
loop:
- '2565845434839sdsfc9we'
给予
"msg": "contentId: 2565845434839sdsfc9we controllerKey: 1000"
在 Ansible 中,我使用 json_query 和循环。 我想在 'contentId' 为“2565845434839sdsfc9we”时获取 'controllerKey'。 尝试了几件事,其中 none 似乎有效。有可能吗?
result": {
"hardware": {
"_vimtype": "vim.vm.VirtualHardware",
"device": [
{
"_vimtype": "vim.vm.device.VirtualDisk",
"backing": {
"contentId": "2565845434839sdsfc9we",
"writeThrough": false
},
"controllerKey": 1000,
},
{
"_vimtype": "vim.vm.device.VirtualDisk",
"backing": {
"contentId": "5264578434839sdsfc9rt",
"writeThrough": false
},
"controllerKey": 1001,
}
],
"memoryMB": 16384,
"numCPU": 2,
"numCoresPerSocket": 1,
"virtualICH7MPresent": false,
"virtualSMCPresent": false
}
下面的任务
- debug:
msg: "{{ result.hardware.device|
json_query('[?backing.contentId==`2565845434839sdsfc9we`].controllerKey')|
first }}"
给予
"msg": "1000"
也可以重复查询。下面的任务
- debug:
msg: "contentId: {{ item }}
controllerKey: {{ result.hardware.device|
json_query(query)|
first }}"
vars:
query: "[?backing.contentId=='{{ item }}'].controllerKey"
loop:
- '2565845434839sdsfc9we'
给予
"msg": "contentId: 2565845434839sdsfc9we controllerKey: 1000"