Debian Codegraph @

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