python - 带有条件的ansible中的json查询
问题描述
在 Ansible 中,我使用 json_query 和循环。当“contentId”为“2565845434839sdsfc9we”时,我想获取“controllerKey”。尝试了几件事,它们似乎都不起作用。甚至可能吗?
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"
推荐阅读
- oracle - 授予角色不会坚持
- go - Golang的Marshal转成json的时候,数组部分变成了对象,所以想保留数组
- android - 如何在颤振中为android应用程序制作单独的可安装插件
- ubuntu - 使用 Nginx 进行密码验证不起作用
- acl - 如何使用 ACL(扩展 ACL)阻止奇数地址?
- c# - 对于事件驱动的后台任务,从 ExecuteAsync 返回什么?
- python - 如何使用 backtrader 回测投资组合组合?
- firebase - 从 firebase 获取用户数据并将其存储在静态变量中
- amazon-web-services - 如何以编程方式查询 AWS 服务状态并发布 CURL 命令
- loops - 如何使用 Tokio 在每个周期或间隔以秒为单位触发一个功能?