vmware.vmware_rest.network_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
。您需要其他要求才能使用此查找插件,有关详细信息,请参阅 要求。
要在 playbook 中使用它,请指定:vmware.vmware_rest.network_moid
。
vmware.vmware_rest 2.1.0 中的新增功能
概要
返回指定路径中包含的 vSphere 网络对象的托管对象引用 (MoID)。
此查找无法区分在多个交换机中定义的具有相同名称的多个网络,因为 vSphere REST API 不支持此操作;网络名称在给定的数据中心/文件夹路径中必须是唯一的。
要求
以下要求需要在执行此查找的本地控制器节点上满足。
vSphere 7.0.3 或更高版本
python >= 3.6
aiohttp
术语
参数 |
注释 |
---|---|
要查找的对象的绝对文件夹路径。 文件夹路径始终以数据中心名称开头,然后是对象类型(主机、虚拟机、网络、数据存储)。 如果对象位于子文件夹中,则应在对象类型后添加子文件夹路径(例如 /my_dc/vm/some/sub_folder/vm_name_to_lookup)。 输入在 VCenter GUI 中看到的对象或文件夹名称。不要转义空格或特殊字符。 |
关键字参数
这描述了查找的关键字参数。这些是在以下示例中 key1=value1
、key2=value2
等值:lookup('vmware.vmware_rest.network_moid', key1=value1, key2=value2, ...)
和 query('vmware.vmware_rest.network_moid', key1=value1, key2=value2, ...)
参数 |
注释 |
---|---|
不应由用户设置,在使用特定的查找插件时在内部设置。 描述要查找的对象类型。例如,集群、数据中心、数据存储等。 默认值: |
|
说明
注意
当关键字和位置参数一起使用时,位置参数必须在关键字参数之前列出:
lookup('vmware.vmware_rest.network_moid', term1, term2, key1=value1, key2=value2)
和query('vmware.vmware_rest.network_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 network named 'my_network'.
# 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.network_moid', '/my_dc/network/my_network',
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.network_moid', '/my_dc/network/my_network', **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.network_moid', '/my_dc/network/my_network') }}"
#
# Network Search Path Examples
#
# Networks are located under the 'network' folder in a datacenter.
# The basic path for a datastore should look like '/<datacenter-name>/network/<network-name>'
- name: Lookup Network Named 'my_network' in Datacenter 'my_dc'
ansible.builtin.debug:
msg: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/my_network') }}"
# If the network is in a user created 'network' type folder, the path shoud just include the
# datacenter and folder name.
- name: Lookup Network Named 'my_network' in Datacenter 'my_dc' in a Network folder 'production'
ansible.builtin.debug:
msg: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/production/my_network') }}"
#
# 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 the network named 'my_network'
vmware.vmware_rest.vcenter_network_info:
names:
- my_network
register: my_network_info
- name: Create a VM
vmware.vmware_rest.vcenter_vm:
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
nics:
- backing:
type: STANDARD_PORTGROUP
network: "{{ my_network_info.value[0].network }}"
# With the lookup, playbooks are shorter, quicker, and more intuitive:
- name: Create a VM
vmware.vmware_rest.vcenter_vm:
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
nics:
- backing:
type: STANDARD_PORTGROUP
network: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/my_network') }}"
返回值
键 |
描述 |
---|---|
vSphere 网络对象的 MoID 返回: 成功 示例: |