使用ansible playbook从VMware数据存储中获取最大的可用空间之一

问题描述 投票:0回答:1

我试图从 VMware 数据存储中获取最大的可用空间之一,然后使用 datastores.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

示例数据存储结果

    "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/"
        },
        {
          .........................
        }

任何人都可以帮助从调试结果中获取仅 1 个 datastores.url 吗?

ansible
1个回答
0
投票

一个最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    info:
      datastores: [
        {
            "freeSpace": 5154511257600,
            "name": "LIT013_078",
            "url": "ds:///vmfs/volumes/64e55997-3946cc08-ce62-9440c96b1102/"
        },
        {
            "freeSpace": 5154341388288,
            "name": "LIT013_075",
            "url": "ds:///vmfs/volumes/64e5595d-02d66360-bf73-9440c96b1102/"
        },
        {
            "freeSpace": 5154511257600,
            "name": "LIT013_077",
            "url": "ds:///vmfs/volumes/64e55986-d2d1286a-5c00-9440c96b1102/"
        },
        {
            "freeSpace": 5154079244288,
            "name": "LIT013_067",
            "url": "ds:///vmfs/volumes/60743b62-a519d76c-1d48-9440c96b1102/"
        },
        {
            "freeSpace": 5154511257600,
            "name": "LIT013_074",
            "url": "ds:///vmfs/volumes/64e5594a-07107de8-1823-9440c96b1102/"
        },
        {
            "freeSpace": 5154511257600,
            "name": "LIT013_072",
            "url": "ds:///vmfs/volumes/64e5591a-1ed49f04-2a09-9440c96b1102/"
        },
        {
            "freeSpace": 4399479914496,
            "name": "LIT013_071",
            "url": "ds:///vmfs/volumes/64e558fc-6a916ba4-82d3-9440c96b1102/"
        },
        {
            "freeSpace": 3060709457920,
            "name": "LIT013_070",
            "url": "ds:///vmfs/volumes/6346c446-6219e6bc-f0a7-9440c96b1102/"
        }
      ]

  tasks:

    - name: This is
      debug:
        msg: "{{ info.datastores | type_debug }}"

    - name: with elements of
      debug:
        msg: "{{ info.datastores | first | type_debug }}"

展示

TASK [This is] ***************
ok: [localhost] =>
  msg: list

TASK [with elements of] ******
ok: [localhost] =>
  msg: dict

你有一个字典列表。因此,一种方法可以是简单地提取与多个字典列表中的最小/最大值相关的键,意味着

sort
它并使用
last
最大值/元素。

    - name: and max entry
      debug:
        msg: "{{ info.datastores | sort(attribute='freeSpace') | last }}"

TASK [and max entry] *******************************************
ok: [localhost] =>
  msg:
    freeSpace: 5154511257600
    name: LIT013_072
    url: ds:///vmfs/volumes/64e5591a-1ed49f04-2a09-9440c96b1102/

要获取 URL,仅在示例中使用

"{{ (info.datastores | sort(attribute='freeSpace') | last).url }}"

进一步阅读

© www.soinside.com 2019 - 2024. All rights reserved.