| 1 |
# Copyright: (c) 2021, Alina Buzachis <@alinabuzachis> |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
|
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
name: network_moid |
| 12 |
short_description: Look up MoID for vSphere network objects using vCenter REST API |
| 13 |
description: |
| 14 |
- Returns Managed Object Reference (MoID) of the vSphere network object contained in the specified path. |
| 15 |
- >- |
| 16 |
This lookup cannot distinguish between multiple networks with the same name defined in multiple switches |
| 17 |
as that is not supported by the vSphere REST API; network names must be unique within a given datacenter/folder path. |
| 18 |
author: |
| 19 |
- Alina Buzachis (@alinabuzachis) |
| 20 |
version_added: 2.1.0 |
| 21 |
requirements: |
| 22 |
- vSphere 7.0.3 or greater |
| 23 |
- python >= 3.6 |
| 24 |
- aiohttp |
| 25 |
notes: |
| 26 |
- This plugin requires the cloud.common collection, which will not be installed automatically as a dependency. |
| 27 |
extends_documentation_fragment: |
| 28 |
- vmware.vmware_rest.moid |
| 29 |
""" |
| 30 |
|
| 31 |
|
| 32 |
EXAMPLES = r""" |
| 33 |
# |
| 34 |
# |
| 35 |
# The examples below assume you have a datacenter named 'my_dc' and a network named 'my_network'. |
| 36 |
# Replace these values as needed for your environment. |
| 37 |
# |
| 38 |
# |
| 39 |
|
| 40 |
# |
| 41 |
# Authentication / Connection Arguments |
| 42 |
# |
| 43 |
# You can explicitly set the connection arguments in each lookup. This may be clearer for some use cases |
| 44 |
- name: Pass In Connection Arguments Explicitly |
| 45 |
ansible.builtin.debug: |
| 46 |
msg: >- |
| 47 |
{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/my_network', |
| 48 |
vcenter_hostname="vcenter.test", |
| 49 |
vcenter_username="[email protected]", |
| 50 |
vcenter_password="1234") }} |
| 51 |
|
| 52 |
# Alternatively, you can add the connection arguments to a dictionary variable, and then pass that variable to the |
| 53 |
# lookup plugins. This makes the individual lookup plugin calls simpler |
| 54 |
- name: Example Playbook |
| 55 |
hosts: all |
| 56 |
vars: |
| 57 |
connection_args: |
| 58 |
vcenter_hostname: "vcenter.test" |
| 59 |
vcenter_username: "[email protected]" |
| 60 |
vcenter_password: "1234" |
| 61 |
tasks: |
| 62 |
# Add more tasks or lookups as needed, referencing the same connection_args variable |
| 63 |
- name: Lookup MoID of the object |
| 64 |
ansible.builtin.debug: |
| 65 |
msg: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/my_network', **connection_args) }}" |
| 66 |
|
| 67 |
# Finally, you can also leverage the environment variables associated with each connection arg, and avoid passing |
| 68 |
# extra args to the lookup plugins |
| 69 |
- name: Use a lookup plugin with VMWARE_* environment variables set |
| 70 |
ansible.builtin.debug: |
| 71 |
msg: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/my_network') }}" |
| 72 |
|
| 73 |
# |
| 74 |
# Network Search Path Examples |
| 75 |
# |
| 76 |
# Networks are located under the 'network' folder in a datacenter. |
| 77 |
# The basic path for a datastore should look like '/<datacenter-name>/network/<network-name>' |
| 78 |
- name: Lookup Network Named 'my_network' in Datacenter 'my_dc' |
| 79 |
ansible.builtin.debug: |
| 80 |
msg: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/my_network') }}" |
| 81 |
|
| 82 |
# If the network is in a user created 'network' type folder, the path shoud just include the |
| 83 |
# datacenter and folder name. |
| 84 |
- name: Lookup Network Named 'my_network' in Datacenter 'my_dc' in a Network folder 'production' |
| 85 |
ansible.builtin.debug: |
| 86 |
msg: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/production/my_network') }}" |
| 87 |
|
| 88 |
|
| 89 |
# |
| 90 |
# Usage in Playbooks |
| 91 |
# |
| 92 |
# |
| 93 |
# The lookup plugin can be used to simplify your playbook. Here is an example of how you might use it. |
| 94 |
# |
| 95 |
# Without the lookup, this takes two modules which both run on the remote host. This can slow down execution |
| 96 |
# and adds extra steps to the playbook: |
| 97 |
- name: Retrieve details about the network named 'my_network' |
| 98 |
vmware.vmware_rest.vcenter_network_info: |
| 99 |
names: |
| 100 |
- my_network |
| 101 |
register: my_network_info |
| 102 |
|
| 103 |
- name: Create a VM |
| 104 |
vmware.vmware_rest.vcenter_vm: |
| 105 |
name: test_vm1 |
| 106 |
guest_OS: RHEL_7_64 |
| 107 |
hardware_version: VMX_11 |
| 108 |
memory: |
| 109 |
size_MiB: 1024 |
| 110 |
disks: |
| 111 |
- type: SATA |
| 112 |
new_vmdk: |
| 113 |
name: first_disk |
| 114 |
capacity: 3200 |
| 115 |
nics: |
| 116 |
- backing: |
| 117 |
type: STANDARD_PORTGROUP |
| 118 |
network: "{{ my_network_info.value[0].network }}" |
| 119 |
|
| 120 |
# With the lookup, playbooks are shorter, quicker, and more intuitive: |
| 121 |
- name: Create a VM |
| 122 |
vmware.vmware_rest.vcenter_vm: |
| 123 |
name: test_vm1 |
| 124 |
guest_OS: RHEL_7_64 |
| 125 |
hardware_version: VMX_11 |
| 126 |
memory: |
| 127 |
size_MiB: 1024 |
| 128 |
disks: |
| 129 |
- type: SATA |
| 130 |
new_vmdk: |
| 131 |
name: first_disk |
| 132 |
capacity: 3200 |
| 133 |
nics: |
| 134 |
- backing: |
| 135 |
type: STANDARD_PORTGROUP |
| 136 |
network: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/my_network') }}" |
| 137 |
""" |
| 138 |
|
| 139 |
|
| 140 |
RETURN = r""" |
| 141 |
_raw: |
| 142 |
description: MoID of the vSphere network object |
| 143 |
type: str |
| 144 |
sample: network-1017 |
| 145 |
""" |
| 146 |
|
| 147 |
|
| 148 |
from ansible.errors import AnsiblePluginError |
| 149 |
|
| 150 |
try: |
| 151 |
from ansible_collections.cloud.common.plugins.plugin_utils.turbo.lookup import ( |
| 152 |
TurboLookupBase as LookupBase, |
| 153 |
) |
| 154 |
except ImportError: |
| 155 |
raise AnsiblePluginError( |
| 156 |
message="This plugin requires the cloud.common collection." |
| 157 |
) |
| 158 |
|
| 159 |
from ansible_collections.vmware.vmware_rest.plugins.plugin_utils.lookup import Lookup |
| 160 |
|
| 161 |
|
| 162 |
class LookupModule(LookupBase): |
| 163 |
async def _run(self, terms, variables, **kwargs): |
| 164 |
self.set_options(var_options=variables, direct=kwargs) |
| 165 |
self.set_option("object_type", "network") |
| 166 |
result = await Lookup.entry_point(terms, self._options) |
| 167 |
return [result] |
| 168 |
|
| 169 |
run = _run if not hasattr(LookupBase, "run_on_daemon") else LookupBase.run_on_daemon |