vmware.vmware_rest.folder_moid 查询 – 使用 vCenter REST API 查询 vSphere 文件夹对象的 MoID

注意

此查询插件是 vmware.vmware_rest 集合 (版本 4.3.0) 的一部分。

如果您使用的是 ansible 包,则可能已安装此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install vmware.vmware_rest。您需要其他要求才能使用此查询插件,有关详细信息,请参阅 要求

要在剧本中使用它,请指定:vmware.vmware_rest.folder_moid

vmware.vmware_rest 2.1.0 中的新增功能

概要

  • 返回指定路径中包含的 vSphere 文件夹对象的管理对象引用 (MoID)。

要求

以下要求是在执行此查询的本地控制器节点上所需的。

  • vSphere 7.0.3 或更高版本

  • python >= 3.6

  • aiohttp

术语

参数

注释

术语

字符串 / 必需

您要查找的对象的绝对文件夹路径。

文件夹路径始终以数据中心名称开头,然后是对象类型(主机、虚拟机、网络、数据存储)。

如果对象位于子文件夹中,则应在对象类型之后添加子文件夹路径(例如 /my_dc/vm/some/sub_folder/vm_name_to_lookup)。

输入在 vCenter GUI 中看到的对象或文件夹名称。不要转义空格或特殊字符。

关键字参数

这描述了查询的关键字参数。这些是在以下示例中的值 key1=value1key2=value2 等等:lookup('vmware.vmware_rest.folder_moid', key1=value1, key2=value2, ...)query('vmware.vmware_rest.folder_moid', key1=value1, key2=value2, ...)

参数

注释

object_type

字符串

不应由用户设置,在使用特定查询插件时会在内部设置。

描述要查找的对象类型。例如,群集、数据中心、数据存储等。

默认值: "cluster"

vcenter_hostname

字符串 / 必需

vSphere vCenter 的主机名或 IP 地址。

配置

vcenter_password

字符串 / 必需

vSphere vCenter 密码。

配置

vcenter_rest_log_file

字符串

您可以使用此可选参数来设置日志文件的位置。

此文件将用于记录 HTTP REST 交互。

该文件将存储在运行模块的主机上。

配置

vcenter_username

字符串 / 必需

vSphere vCenter 用户名。

配置

vcenter_validate_certs

布尔值

允许在 SSL 证书无效时连接。当证书不受信任时,设置为 false

选项

  • false

  • true ← (默认)

配置

备注

注意

  • 当同时使用关键字参数和位置参数时,位置参数必须列在关键字参数之前:lookup('vmware.vmware_rest.folder_moid', term1, term2, key1=value1, key2=value2)query('vmware.vmware_rest.folder_moid', term1, term2, key1=value1, key2=value2)

  • 查找插件运行在 Ansible 控制器上,用于从外部资源查找信息。参见 https://docs.ansible.org.cn/ansible/latest/plugins/lookup.html#lookup-plugins

  • 此集合的插件允许您快速收集 VMware 资源标识符,并将其存储或使用,而无需使用多个模块和任务来完成相同的事情。请参见示例部分以进行比较。

示例

#
#
# The examples below assume you have a datacenter named 'my_dc' and a host folder structure like path/to/my_folder.
# Replace these values as needed for your environment.
#
#

#
# Authentication / Connection Arguments
#
# You can explicitly set the connection arguments in each lookup. This may be clearer for some use cases
- name: Pass In Connection Arguments Explicitly
  ansible.builtin.debug:
    msg: >-
      {{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/path/to/my_folder',
      vcenter_hostname="vcenter.test",
      vcenter_username="[email protected]",
      vcenter_password="1234") }}

# Alternatively, you can add the connection arguments to a dictionary variable, and then pass that variable to the
# lookup plugins. This makes the individual lookup plugin calls simpler
- name: Example Playbook
  hosts: all
  vars:
    connection_args:
      vcenter_hostname: "vcenter.test"
      vcenter_username: "[email protected]"
      vcenter_password: "1234"
  tasks:
    # Add more tasks or lookups as needed, referencing the same connection_args variable
    - name: Lookup MoID of the object
      ansible.builtin.debug:
        msg: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/path/to/my_folder', **connection_args) }}"

# Finally, you can also leverage the environment variables associated with each connection arg, and avoid passing
# extra args to the lookup plugins
- name: Use a lookup plugin with VMWARE_* environment variables set
  ansible.builtin.debug:
    msg: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/path/to/my_folder') }}"

#
# Folder Search Path Examples
#
# Due to a known issue, duplicate named folders in a datacenter are not searchable with this lookup.
# For example, you  cannot reliably get a host folder named 'test_folder' when a vm folder named 'test_folder'
# exists.
#
# https://github.com/ansible-collections/vmware.vmware_rest/issues/500
- name: Lookup Folder Named 'my_folder' in Datacenter 'my_dc'
  ansible.builtin.debug:
    msg: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/my_folder') }}"

#
# Usage in Playbooks
#
#
# The lookup plugin can be used to simplify your playbook. Here is an example of how you might use it.
#
# Without the lookup, this takes two modules which both run on the remote host. This can slow down execution
# and adds extra steps to the playbook:
- name: Retrieve details about a folder named 'my_folder'
  vmware.vmware_rest.vcenter_cluster_info:
    names:
      - my_folder
  register: my_foler_info

- name: Create a VM
  vmware.vmware_rest.vcenter_vm:
    placement:
      folder: "{{ my_foler_info.value[0].folder }}"
    name: test_vm1
    guest_OS: RHEL_7_64
    hardware_version: VMX_11
    memory:
      size_MiB: 1024
    disks:
      - type: SATA
        new_vmdk:
          name: first_disk
          capacity: 3200

# With the lookup, playbooks are shorter, quicker, and more intuitive:
- name: Create a VM
  vmware.vmware_rest.vcenter_vm:
    placement:
      folder: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/path/to/my_folder') }}"
    name: test_vm1
    guest_OS: RHEL_7_64
    hardware_version: VMX_11
    memory:
      size_MiB: 1024
    disks:
      - type: SATA
        new_vmdk:
          name: first_disk
          capacity: 3200

返回值

描述

返回值

字符串

vSphere 文件夹对象的 MoID

返回: 成功

示例: "group-v1029"

作者

  • Alina Buzachis (@alinabuzachis)

提示

每种条目类型的配置项具有从低到高的优先级顺序。例如,列表中较低的变量将覆盖较高的变量。