Debian Codegraph @

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