从vmware_guest_disk_info模块输出收集backing_datastore

发布于 2025-02-12 15:40:49 字数 6999 浏览 1 评论 0原文

我正在使用剧本中的以下任务来查找VMware中的备份数据存储,但是我尝试了各种选项,这无助于收集Backing_datastore名称,请建议如何获取:

- name: Take Snapshot
  hosts: patching
  serial: 1
  vars_files:
    - 1credentials.yml
  tasks:
    - name: Gather data of the registered virtual machines
      community.vmware.vmware_vm_info:
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        vm_type: vm
        validate_certs: no
      delegate_to: localhost
      register: vminfo

    - debug:
        msg: "{{ item.datacenter }}"
      loop: "{{ vminfo.virtual_machines}}"
      when: item.ip_address == inventory_hostname

    - name: Gather Disk facts for the server - {{ inventory_hostname }}
      vmware_guest_disk_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ item.datacenter }}"
        uuid: "{{ item.uuid }}"
        validate_certs: False
      delegate_to: localhost
      register: disk_fact
      loop: "{{ vminfo.virtual_machines }}"
      when: item.ip_address == inventory_hostname

    - debug:
        msg: "{{ item.backing_datastore }}"
      loop: "{{ disk_fact }}"

提到注册变量disk_fact的原始输出。下图:

ok: [172.17.92.62] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "item": {
                    "allocated": {},
                    "attributes": {},
                    "cluster": "Training",
                    "datacenter": "opendc-rookie",
                    "datastore_url": [
                        {
                            "name": "Rookie-Core",
                            "url": "/vmfs/volumes/60ebc5fb-2e22fef3-ec42-1402ec6fadc8"
                        },
                        {
                            "name": "ENV08",
                            "url": "/vmfs/volumes/60ebd414-4e6253d6-7e29-1402ec6fadc8"
                        }
                    ],
                    "esxi_hostname": "172.17.138.13",
                    "folder": "/opendc-rookie/vm/ENV08",
                    "guest_fullname": "Microsoft Windows 8.x (64-bit)",
                    "guest_name": "win1",
                    "ip_address": "172.17.92.43",
                    "mac_address": [
                        "00:50:56:81:2e:64"
                    ],
                    "moid": "vm-968",
                    "power_state": "poweredOn",
                    "tags": [],
                    "uuid": "4201e3dd-0cf5-1027-7eaf-d2b8804761d9",
                    "vm_network": {
                        "00:50:56:81:2e:64": {
                            "ipv4": [
                                "172.17.92.43"
                            ],
                            "ipv6": []
                        }
                    }
                },
                "skip_reason": "Conditional result was False",
                "skipped": true
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "guest_disk_info": {
                    "0": {
                        "backing_datastore": "ENV04",
                        "backing_disk_mode": "persistent",
                        "backing_diskmode": "persistent",
                        "backing_eagerlyscrub": false,
                        "backing_filename": "[ENV04] ansible_automation_platofrm_RHEL8/ansible_automation_platofrm_RHEL8-000005.vmdk",
                        "backing_thinprovisioned": false,
                        "backing_type": "FlatVer2",
                        "backing_uuid": "6000C294-c77a-56fa-180c-d17ac9693433",
                        "backing_writethrough": false,
                        "capacity_in_bytes": 25769803776,
                        "capacity_in_kb": 25165824,
                        "controller_bus_number": 0,
                        "controller_key": 1000,
                        "controller_type": "paravirtual",
                        "key": 2000,
                        "label": "Hard disk 1",
                        "summary": "25,165,824 KB",
                        "unit_number": 0
                    }
                },
                "invocation": {
                    "module_args": {
                        "datacenter": "opendc-rookie",
                        "folder": null,
                        "hostname": "172.17.168.212",
                        "moid": null,
                        "name": null,
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "port": 443,
                        "proxy_host": null,
                        "proxy_port": null,
                        "use_instance_uuid": false,
                        "username": "[email protected]",
                        "uuid": "42013f12-5142-2b83-1ce7-6c334958afd5",
                        "validate_certs": false
                    }
                },
                "item": {
                    "allocated": {},
                    "attributes": {},
                    "cluster": "Training",
                    "datacenter": "opendc-rookie",
                    "datastore_url": [
                        {
                            "name": "Rookie-Core",
                            "url": "/vmfs/volumes/60ebc5fb-2e22fef3-ec42-1402ec6fadc8"
                        },
                        {
                            "name": "ENV04",
                            "url": "/vmfs/volumes/60ebd318-7c586ef0-5838-1402ec6fadc8"
                        }
                    ],
                    "esxi_hostname": "172.17.138.13",
                    "folder": "/opendc-rookie/vm/Pugaz",
                    "guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
                    "guest_name": "ansible_automation_platofrm_RHEL8",
                    "ip_address": "172.17.92.62",
                    "mac_address": [
                        "00:50:56:81:c6:e3"
                    ],
                    "moid": "vm-836",
                    "power_state": "poweredOn",
                    "tags": [],
                    "uuid": "42013f12-5142-2b83-1ce7-6c334958afd5",
                    "vm_network": {
                        "00:50:56:81:c6:e3": {
                            "ipv4": [
                                "172.17.92.62"
                            ],
                            "ipv6": [
                                "fe80::250:56ff:fe81:c6e3"
                            ]
                        }
                    }
                }
            }

请建议如何从上述输出中获取Backing_datastore。

I'm using the below tasks in the playbook to find the backing datastore in the vmware but I tried various options, which is not helping to gather the backing_datastore name, kindly suggest how to fetch:

- name: Take Snapshot
  hosts: patching
  serial: 1
  vars_files:
    - 1credentials.yml
  tasks:
    - name: Gather data of the registered virtual machines
      community.vmware.vmware_vm_info:
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        vm_type: vm
        validate_certs: no
      delegate_to: localhost
      register: vminfo

    - debug:
        msg: "{{ item.datacenter }}"
      loop: "{{ vminfo.virtual_machines}}"
      when: item.ip_address == inventory_hostname

    - name: Gather Disk facts for the server - {{ inventory_hostname }}
      vmware_guest_disk_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ item.datacenter }}"
        uuid: "{{ item.uuid }}"
        validate_certs: False
      delegate_to: localhost
      register: disk_fact
      loop: "{{ vminfo.virtual_machines }}"
      when: item.ip_address == inventory_hostname

    - debug:
        msg: "{{ item.backing_datastore }}"
      loop: "{{ disk_fact }}"

The raw output of the registered variable disk_fact is mentioned below:

ok: [172.17.92.62] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "item": {
                    "allocated": {},
                    "attributes": {},
                    "cluster": "Training",
                    "datacenter": "opendc-rookie",
                    "datastore_url": [
                        {
                            "name": "Rookie-Core",
                            "url": "/vmfs/volumes/60ebc5fb-2e22fef3-ec42-1402ec6fadc8"
                        },
                        {
                            "name": "ENV08",
                            "url": "/vmfs/volumes/60ebd414-4e6253d6-7e29-1402ec6fadc8"
                        }
                    ],
                    "esxi_hostname": "172.17.138.13",
                    "folder": "/opendc-rookie/vm/ENV08",
                    "guest_fullname": "Microsoft Windows 8.x (64-bit)",
                    "guest_name": "win1",
                    "ip_address": "172.17.92.43",
                    "mac_address": [
                        "00:50:56:81:2e:64"
                    ],
                    "moid": "vm-968",
                    "power_state": "poweredOn",
                    "tags": [],
                    "uuid": "4201e3dd-0cf5-1027-7eaf-d2b8804761d9",
                    "vm_network": {
                        "00:50:56:81:2e:64": {
                            "ipv4": [
                                "172.17.92.43"
                            ],
                            "ipv6": []
                        }
                    }
                },
                "skip_reason": "Conditional result was False",
                "skipped": true
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "guest_disk_info": {
                    "0": {
                        "backing_datastore": "ENV04",
                        "backing_disk_mode": "persistent",
                        "backing_diskmode": "persistent",
                        "backing_eagerlyscrub": false,
                        "backing_filename": "[ENV04] ansible_automation_platofrm_RHEL8/ansible_automation_platofrm_RHEL8-000005.vmdk",
                        "backing_thinprovisioned": false,
                        "backing_type": "FlatVer2",
                        "backing_uuid": "6000C294-c77a-56fa-180c-d17ac9693433",
                        "backing_writethrough": false,
                        "capacity_in_bytes": 25769803776,
                        "capacity_in_kb": 25165824,
                        "controller_bus_number": 0,
                        "controller_key": 1000,
                        "controller_type": "paravirtual",
                        "key": 2000,
                        "label": "Hard disk 1",
                        "summary": "25,165,824 KB",
                        "unit_number": 0
                    }
                },
                "invocation": {
                    "module_args": {
                        "datacenter": "opendc-rookie",
                        "folder": null,
                        "hostname": "172.17.168.212",
                        "moid": null,
                        "name": null,
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "port": 443,
                        "proxy_host": null,
                        "proxy_port": null,
                        "use_instance_uuid": false,
                        "username": "[email protected]",
                        "uuid": "42013f12-5142-2b83-1ce7-6c334958afd5",
                        "validate_certs": false
                    }
                },
                "item": {
                    "allocated": {},
                    "attributes": {},
                    "cluster": "Training",
                    "datacenter": "opendc-rookie",
                    "datastore_url": [
                        {
                            "name": "Rookie-Core",
                            "url": "/vmfs/volumes/60ebc5fb-2e22fef3-ec42-1402ec6fadc8"
                        },
                        {
                            "name": "ENV04",
                            "url": "/vmfs/volumes/60ebd318-7c586ef0-5838-1402ec6fadc8"
                        }
                    ],
                    "esxi_hostname": "172.17.138.13",
                    "folder": "/opendc-rookie/vm/Pugaz",
                    "guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
                    "guest_name": "ansible_automation_platofrm_RHEL8",
                    "ip_address": "172.17.92.62",
                    "mac_address": [
                        "00:50:56:81:c6:e3"
                    ],
                    "moid": "vm-836",
                    "power_state": "poweredOn",
                    "tags": [],
                    "uuid": "42013f12-5142-2b83-1ce7-6c334958afd5",
                    "vm_network": {
                        "00:50:56:81:c6:e3": {
                            "ipv4": [
                                "172.17.92.62"
                            ],
                            "ipv6": [
                                "fe80::250:56ff:fe81:c6e3"
                            ]
                        }
                    }
                }
            }

Kindly suggest how can I fetch the backing_datastore from the above mentioned output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2025-02-19 15:40:49

结果是列表,因为属性 backing_datastore 可能会有更多项目。例如,

backing_datastores: "{{ disk_fact.results|json_query(_query) }}"
_query: '[].guest_disk_info[].*.backing_datastore'

给出

backing_datastores:
  - - ENV04

第一个

backing_datastore: "{{ backing_datastores|flatten|first }}"

一个

backing_datastore: ENV04

如果要迭代结果,如果要给出第一个 backing_datastore ,如果您要

    - debug:
        msg: "{{ item.guest_disk_info['0'].backing_datastore }}"
      loop: "{{ disk_fact.results }}"
      loop_control:
        label: "{{ item.item.ip_address }}"
      when: not item.skipped|d(false)

,如果您要给出第

TASK [debug] **************************************************
skipping: [localhost] => (item=172.17.92.43) 
ok: [localhost] => (item=172.17.92.62) => 
  msg: ENV04

。想要获取所有磁盘的属性 backing_datastore 在循环中

    - debug:
        msg: "{{ item.guest_disk_info|json_query('*.backing_datastore') }}"
      loop: "{{ disk_fact.results }}"
      loop_control:
        label: "{{ item.item.ip_address }}"
      when: not item.skipped|d(false)

TASK [debug] **************************************************
skipping: [localhost] => (item=172.17.92.43) 
ok: [localhost] => (item=172.17.92.62) => 
  msg:
  - ENV04

使用

- hosts: localhost
  vars_files:
    - disk_fact.json
  vars:
    backing_datastores: "{{ disk_fact.results|json_query(_query) }}"
    _query: '[].guest_disk_info[].*.backing_datastore'
    backing_datastore: "{{ backing_datastores|flatten|first }}"
  tasks:
    - debug:
        var: backing_datastores
    - debug:
        var: backing_datastore
    - debug:
        msg: "{{ item.guest_disk_info['0'].backing_datastore }}"
      loop: "{{ disk_fact.results }}"
      loop_control:
        label: "{{ item.item.ip_address }}"
      when: not item.skipped|d(false)

The result is a list because there might be more items with the attribute backing_datastore. For example,

backing_datastores: "{{ disk_fact.results|json_query(_query) }}"
_query: '[].guest_disk_info[].*.backing_datastore'

gives

backing_datastores:
  - - ENV04

If you want the first one

backing_datastore: "{{ backing_datastores|flatten|first }}"

gives

backing_datastore: ENV04

If you want to iterate results

    - debug:
        msg: "{{ item.guest_disk_info['0'].backing_datastore }}"
      loop: "{{ disk_fact.results }}"
      loop_control:
        label: "{{ item.item.ip_address }}"
      when: not item.skipped|d(false)

gives the attribute backing_datastore of the disk "0"

TASK [debug] **************************************************
skipping: [localhost] => (item=172.17.92.43) 
ok: [localhost] => (item=172.17.92.62) => 
  msg: ENV04

If you want to get the list of attributes backing_datastore of all disks use json_query in the loop

    - debug:
        msg: "{{ item.guest_disk_info|json_query('*.backing_datastore') }}"
      loop: "{{ disk_fact.results }}"
      loop_control:
        label: "{{ item.item.ip_address }}"
      when: not item.skipped|d(false)

gives

TASK [debug] **************************************************
skipping: [localhost] => (item=172.17.92.43) 
ok: [localhost] => (item=172.17.92.62) => 
  msg:
  - ENV04

Example of a complete playbook for testing

- hosts: localhost
  vars_files:
    - disk_fact.json
  vars:
    backing_datastores: "{{ disk_fact.results|json_query(_query) }}"
    _query: '[].guest_disk_info[].*.backing_datastore'
    backing_datastore: "{{ backing_datastores|flatten|first }}"
  tasks:
    - debug:
        var: backing_datastores
    - debug:
        var: backing_datastore
    - debug:
        msg: "{{ item.guest_disk_info['0'].backing_datastore }}"
      loop: "{{ disk_fact.results }}"
      loop_control:
        label: "{{ item.item.ip_address }}"
      when: not item.skipped|d(false)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文