如何从 cli 获取 VMware 文件夹

how to get VMware folder from cli

我正在编写一个 ansible 脚本来在不需要时关闭多个虚拟机。当我知道虚拟机所在的位置时,我就能做到这一点。问题是,如果 VM 移动到不同的主机,它可能会移动到不同的文件夹。根据下面的文章,我需要知道可能再次更改的 VM 文件夹。不确定如何自动获取文件夹。有没有办法用 Ansible 和/或 Python 做到这一点,然后将其输入脚本?

https://docs.ansible.com/ansible/latest/modules/vmware_guest_powerstate_module.html

- name: Set the state of a virtual machine to poweroff
  vmware_guest_powerstate:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: no
    folder: "/{{ datacenter_name }}/vm/my_folder"
    name: "{{ guest_name }}"
    state: powered-off
  delegate_to: localhost
  register: deploy

首先使用 vmware_guest_info 模块获取有关 VM 的信息,然后像这样在下一个任务中传递文件夹,

- name: Gather info about the vmware guest vm
  vmware_guest_info:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter: "{{ datacenter_name }}"
    validate_certs: no
    name: "{{ guest_name }}"
  delegate_to: localhost
  register: vm_info



- name: Set the state of a virtual machine to poweroff
  vmware_guest_powerstate:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: no
    folder: "{{ vm_info['hw_folder']}}"
    name: "{{ guest_name }}"
    state: powered-off
  delegate_to: localhost
  register: deploy