如何从vmware_guest_disk_facts获取字典条目 [英] How to get entry of a dictionary from 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'],得到:数字:解析第17列的错误,令牌\"0 \"(数字),用于表达式
Expecting: ['quoted_identifier', 'unquoted_identifier', 'lbracket', 'lbrace'], got: number: Parse error at column 17, token \"0\" (NUMBER), for expression
或
msg:"
OR
味精:[
"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屋!