我正在尝试获取具有最多可用空间的 VMware 数据存储,然后使用它的 URL 在 Kubernetes 集群中创建存储类。
我正在使用模块获取信息vmware_datastore_info
,如下所示:
---
- name: Gather info from ESXi datacenter
hosts: localhost
gather_facts: false
tasks:
- name: Gather info from ESXi datastore
vmware_datastore_info:
hostname: '{{ vcenter_server }}'
username: '{{ username }}'
password: '{{ password }}'
datacenter_name: '{{ datacenter_name }}'
validate_certs: false
delegate_to: localhost
register: info
- debug: var=info.datastores
示例info.datastores
结果
{
"msg": [
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "DDA_SPIT_013_002",
"freeSpace": 5154511257600,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_078",
"provisioned": 1596981248,
"type": "VMFS",
"uncommitted": 0,
"url": "ds:///vmfs/volumes/64e55997-3946cc08-ce62-9440c96b1102/"
},
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "DDA_SPIT_013_002",
"freeSpace": 5154341388288,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_075",
"provisioned": 11265900544,
"type": "VMFS",
"uncommitted": 9499049984,
"url": "ds:///vmfs/volumes/64e5595d-02d66360-bf73-9440c96b1102/"
},
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "DDA_SPIT_013_002",
"freeSpace": 5154511257600,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_077",
"provisioned": 1596981248,
"type": "VMFS",
"uncommitted": 0,
"url": "ds:///vmfs/volumes/64e55986-d2d1286a-5c00-9440c96b1102/"
},
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "DDA_SPIT_013_002",
"freeSpace": 5154079244288,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_067",
"provisioned": 2943352832,
"type": "VMFS",
"uncommitted": 914358272,
"url": "ds:///vmfs/volumes/60743b62-a519d76c-1d48-9440c96b1102/"
},
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "DDA_SPIT_013_002",
"freeSpace": 5154511257600,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_074",
"provisioned": 1596981248,
"type": "VMFS",
"uncommitted": 0,
"url": "ds:///vmfs/volumes/64e5594a-07107de8-1823-9440c96b1102/"
},
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "DDA_SPIT_013_002",
"freeSpace": 5154511257600,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_072",
"provisioned": 1596981248,
"type": "VMFS",
"uncommitted": 0,
"url": "ds:///vmfs/volumes/64e5591a-1ed49f04-2a09-9440c96b1102/"
},
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "DDA_SPIT_013_002",
"freeSpace": 4399479914496,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_071",
"provisioned": 1383218544640,
"type": "VMFS",
"uncommitted": 626590220288,
"url": "ds:///vmfs/volumes/64e558fc-6a916ba4-82d3-9440c96b1102/"
},
{
"accessible": true,
"capacity": 5156108238848,
"datastore_cluster": "CZCHO_SPIT_013_002",
"freeSpace": 3060709457920,
"maintenanceMode": "normal",
"multipleHostAccess": true,
"name": "LIT013_070",
"provisioned": 2428559687680,
"type": "VMFS",
"uncommitted": 333160906752,
"url": "ds:///vmfs/volumes/6346c446-6219e6bc-f0a7-9440c96b1102/"
}
]
}
我怎样才能datastores.url
从这个结果中只得到一个?
最小示例剧本
展示
您有一个字典列表。因此,一种方法是简单地提取与多个字典列表中的最小/最大值相关的键,然后
sort
使用last
最大值/元素。和
要获取 URL,仅使用示例
进一步阅读
您可以
max
立即在字典列表上使用 Jinja 的过滤器。max
处理字典时,你只需要提供你想要应用的属性:来源:
max
过滤器文档因此,对于您的具体用例:
其结果为: