Debian Codegraph @

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