Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/lookup/cluster_moid.py

raw
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: cluster_moid
12 short_description: Look up MoID for vSphere cluster objects using vCenter REST API
13 description:
14 - Returns Managed Object Reference (MoID) of the vSphere cluster 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' and a cluster named 'my_cluster'.
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.cluster_moid', '/my_dc/host/my_cluster',
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.cluster_moid', '/my_dc/host/my_cluster', **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.cluster_moid', '/my_dc/host/my_cluster') }}"
69
70 #
71 # Cluster Search Path Examples
72 #
73 # Clusters are located under the 'host' folder in a datacenter.
74 # The basic path for a cluster should look like '/<datacenter-name>/host/<cluster-name>'
75 - name: Lookup Cluster Named 'my_cluster' in Datacenter 'my_dc'
76 ansible.builtin.debug:
77 msg: "{{ lookup('vmware.vmware_rest.cluster_moid', '/my_dc/host/my_cluster') }}"
78
79 #
80 # Usage in Playbooks
81 #
82 #
83 # The lookup plugin can be used to simplify your playbook. Here is an example of how you might use it.
84 #
85 # Without the lookup, this takes two modules which both run on the remote host. This can slow down execution
86 # and adds extra steps to the playbook:
87 - name: Retrieve details about a cluster named 'my_cluster'
88 vmware.vmware_rest.vcenter_cluster_info:
89 names:
90 - my_cluster
91 register: my_cluster_info
92
93 - name: Create a VM
94 vmware.vmware_rest.vcenter_vm:
95 placement:
96 cluster: "{{ my_cluster_info.value[0].cluster }}"
97 name: test_vm1
98 guest_OS: RHEL_7_64
99 hardware_version: VMX_11
100 memory:
101 size_MiB: 1024
102 disks:
103 - type: SATA
104 new_vmdk:
105 name: first_disk
106 capacity: 3200
107
108 # With the lookup, playbooks are shorter, quicker, and more intuitive:
109 - name: Create a VM
110 vmware.vmware_rest.vcenter_vm:
111 placement:
112 cluster: "{{ lookup('vmware.vmware_rest.cluster_moid', '/my_dc/host/my_cluster') }}"
113 name: test_vm1
114 guest_OS: RHEL_7_64
115 hardware_version: VMX_11
116 memory:
117 size_MiB: 1024
118 disks:
119 - type: SATA
120 new_vmdk:
121 name: first_disk
122 capacity: 3200
123 """
124
125
126 RETURN = r"""
127 _raw:
128 description: MoID of the vSphere cluster object
129 type: str
130 sample: domain-c1007
131 """
132
133
134 from ansible.errors import AnsiblePluginError
135
136 try:
137 from ansible_collections.cloud.common.plugins.plugin_utils.turbo.lookup import (
138 TurboLookupBase as LookupBase,
139 )
140 except ImportError:
141 raise AnsiblePluginError(
142 message="This plugin requires the cloud.common collection."
143 )
144
145 from ansible_collections.vmware.vmware_rest.plugins.plugin_utils.lookup import Lookup
146
147
148 class LookupModule(LookupBase):
149 async def _run(self, terms, variables, **kwargs):
150 self.set_options(var_options=variables, direct=kwargs)
151 self.set_option("object_type", "cluster")
152 result = await Lookup.entry_point(terms, self._options)
153 return [result]
154
155 run = _run if not hasattr(LookupBase, "run_on_daemon") else LookupBase.run_on_daemon