ansible with conditions中的json查询

2024-09-30 12:26:32 发布

您现在位置:Python中文网/ 问答频道 /正文

在Ansible中,我使用的是json查询和循环。 当“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
                }

Tags: jsonfalsedevicevmvimresultansiblehardware
1条回答
网友
1楼 · 发布于 2024-09-30 12:26:32

下面的任务

- 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"

相关问题 更多 >

    热门问题