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