如何从 vmware_guest_disk_facts 获取字典条目 [英] How to get entry of a dictionary from vmware_guest_disk_facts

查看:27
本文介绍了如何从 vmware_guest_disk_facts 获取字典条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取特定硬盘的数据存储名称,但我未能成功找出在列表中选择一个条目.

I'm trying to get the datastore name for a specific hard disk but I haven't been successful in being able to figuring out choose an entry in the list.

此输出来自 ansible 模块vmware_guest_disk_facts"我将此输出保存到名为vm_info"的变量中.

This output is from the ansible module "vmware_guest_disk_facts" I save this output to a variable called "vm_info".

   "guest_disk_facts": {
        "0": {
            "backing_filename": "stuffstuff",
            "capacity_in_kb": 106954752,
            "backing_eagerlyscrub": false,
            "backing_datastore": "WHAT I REALLY WANT",
            "backing_writethrough": false,
            "label": "Hard disk 1",
            "backing_type": "FlatVer2",
            "key": 2000,
            "capacity_in_bytes": 109521666048,
            "backing_thinprovisioned": false,
            "controller_key": 1000,
            "summary": "106,954,752 KB",
            "unit_number": 0,
            "backing_uuid": "info"
        },
        "1": {
            "backing_filename": "stuffstuff",
            "capacity_in_kb": 15728640,
            "backing_eagerlyscrub": false,
            "backing_datastore": "DON'T CARE OF ABOUT THIS ONE",
            "backing_writethrough": false,
            "label": "Hard disk 2",
            "backing_type": "FlatVer2",
            "key": 2001,
            "capacity_in_bytes": 16106127360,
            "backing_thinprovisioned": false,
            "controller_key": 1000,
            "summary": "15,728,640 KB",
            "unit_number": 1,
            "backing_uuid": "info"
        }

    - debug:
        msg: "{{ item.guest_disk_facts | json_query(query) }}"
      with_items: "{{ vm_info.results }}"
      vars:
        query: "guest_disk_facts.0.backing_datastore" #done w/ & w/o quotes around 0

我还尝试了以下查询,但我觉得此时我已经用尽了所有选项.

I've also tried the following queries and I feel like I've exhausted all options at this point.

    query: "guest_disk_facts.[0].backing_datastore"#done w/ & w/o quotes around 0

    query: "guest_disk_facts[0].backing_datastore" #done w/ & w/o quotes around 0

    query: "guest_disk_facts.*.backing_datastore" #will give me backing_datastore entries for both dictionaries in this case

我只想为这个字典列表中的一个条目获取 backing_datastore

I would like to just get backing_datastore for one entry in this list of dictionaries

msg:我真正想要的"

msg: "WHAT I REALLY WANT"

但到目前为止,我返回的是这个错误:

but so far I'm returned with either this error:

预期:['quoted_identifier', 'unquoted_identifier', 'lbracket', 'lbrace'],得到:number:第 17 列解析错误,标记 \"0\" (NUMBER),用于表达式

Expecting: ['quoted_identifier', 'unquoted_identifier', 'lbracket', 'lbrace'], got: number: Parse error at column 17, token \"0\" (NUMBER), for expression

味精:""

味精:[

0",

]

推荐答案

下面的任务给出了你真正想要的"

The task below gives "WHAT YOU REALLY WANT"

- debug:
    msg: "{{ guest_disk_facts['0'|quote].backing_datastore }}"

重点是引用带引号的键.键 '0' 和 '1' 不是 有效变量并且必须被引用.

The point is quoting the quoted key. The keys '0' and '1' are not valid variables and must be quoted.

下面的循环

- debug:
    msg: "{{ guest_disk_facts[item|quote].backing_datastore }}"
  loop: "{{ guest_disk_facts.keys() }}"

给予

ok: [localhost] => (item=1) => 
  msg: DON'T CARE OF ABOUT THIS ONE
ok: [localhost] => (item=0) => 
  msg: WHAT I REALLY WANT

这篇关于如何从 vmware_guest_disk_facts 获取字典条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆