Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/vcenter_vm_tools_installer_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2021, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # template: header.j2
6 # This module is autogenerated using the ansible.content_builder.
7 # See: https://github.com/ansible-community/ansible.content_builder
8
9
10 DOCUMENTATION = r"""
11 module: vcenter_vm_tools_installer_info
12 short_description: Get information about the VMware Tools installer.
13 description: Get information about the VMware Tools installer.
14 options:
15 session_timeout:
16 description:
17 - 'Timeout settings for client session. '
18 - 'The maximal number of seconds for the whole operation including connection
19 establishment, request sending and response. '
20 - The default value is 300s.
21 type: float
22 version_added: 2.1.0
23 vcenter_hostname:
24 description:
25 - The hostname or IP address of the vSphere vCenter
26 - If the value is not specified in the task, the value of environment variable
27 C(VMWARE_HOST) will be used instead.
28 required: true
29 type: str
30 vcenter_password:
31 description:
32 - The vSphere vCenter password
33 - If the value is not specified in the task, the value of environment variable
34 C(VMWARE_PASSWORD) will be used instead.
35 required: true
36 type: str
37 vcenter_rest_log_file:
38 description:
39 - 'You can use this optional parameter to set the location of a log file. '
40 - 'This file will be used to record the HTTP REST interaction. '
41 - 'The file will be stored on the host that runs the module. '
42 - 'If the value is not specified in the task, the value of '
43 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
44 type: str
45 vcenter_username:
46 description:
47 - The vSphere vCenter username
48 - If the value is not specified in the task, the value of environment variable
49 C(VMWARE_USER) will be used instead.
50 required: true
51 type: str
52 vcenter_validate_certs:
53 default: true
54 description:
55 - Allows connection when SSL certificates are not valid. Set to C(false) when
56 certificates are not trusted.
57 - If the value is not specified in the task, the value of environment variable
58 C(VMWARE_VALIDATE_CERTS) will be used instead.
59 type: bool
60 vm:
61 description:
62 - Identifier of the virtual machine.
63 - The parameter must be the id of a resource returned by M(vmware.vmware_rest.vcenter_vm_info).
64 Required with I(state=['get'])
65 type: str
66 author:
67 - Ansible Cloud Team (@ansible-collections)
68 version_added: 0.1.0
69 requirements:
70 - vSphere 7.0.3 or greater
71 - python >= 3.6
72 - aiohttp
73 notes:
74 - Tested on vSphere 7.0.3
75 """
76
77 EXAMPLES = r"""
78 - name: Create a VM
79 vmware.vmware_rest.vcenter_vm:
80 placement:
81 cluster: "{{ lookup('vmware.vmware_rest.cluster_moid', '/my_dc/host/my_cluster') }}"
82 datastore: "{{ lookup('vmware.vmware_rest.datastore_moid', '/my_dc/datastore/local') }}"
83 folder: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/vm') }}"
84 resource_pool: "{{ lookup('vmware.vmware_rest.resource_pool_moid', '/my_dc/host/my_cluster/Resources') }}"
85 name: test_vm1
86 guest_OS: RHEL_7_64
87 hardware_version: VMX_11
88 memory:
89 hot_add_enabled: true
90 size_MiB: 1024
91 disks:
92 - type: SATA
93 backing:
94 type: VMDK_FILE
95 vmdk_file: '[local] test_vm1/{{ disk_name }}.vmdk'
96 - type: SATA
97 new_vmdk:
98 name: second_disk
99 capacity: 32000000000
100 cdroms:
101 - type: SATA
102 sata:
103 bus: 0
104 unit: 2
105 nics:
106 - backing:
107 type: STANDARD_PORTGROUP
108 network: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/VM Network') }}"
109 register: my_vm
110
111 - name: Get information about the vm-tools
112 vmware.vmware_rest.vcenter_vm_tools_installer_info:
113 vm: '{{ my_vm.id }}'
114 """
115 RETURN = r"""
116 # content generated by the update_return_section callback# task: Get information about the vm-tools
117 value:
118 description: Get information about the vm-tools
119 returned: On success
120 sample:
121 is_connected: 0
122 type: dict
123 """
124
125
126 # This structure describes the format of the data expected by the end-points
127 PAYLOAD_FORMAT = {
128 "get": {"query": {}, "body": {}, "path": {"vm": "vm"}}
129 } # pylint: disable=line-too-long
130
131 from ansible.module_utils.basic import env_fallback
132 import os
133
134 if os.getenv("VMWARE_ENABLE_TURBO", False):
135 try:
136 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
137 AnsibleTurboModule as AnsibleModule,
138 )
139
140 AnsibleModule.collection_name = "vmware.vmware_rest"
141 except ImportError:
142 from ansible.module_utils.basic import AnsibleModule
143 else:
144 from ansible.module_utils.basic import AnsibleModule
145
146 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
147 gen_args,
148 open_session,
149 session_timeout,
150 update_changed_flag,
151 )
152
153
154 def prepare_argument_spec():
155 argument_spec = {
156 "vcenter_hostname": dict(
157 type="str",
158 required=True,
159 fallback=(env_fallback, ["VMWARE_HOST"]),
160 ),
161 "vcenter_username": dict(
162 type="str",
163 required=True,
164 fallback=(env_fallback, ["VMWARE_USER"]),
165 ),
166 "vcenter_password": dict(
167 type="str",
168 required=True,
169 no_log=True,
170 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
171 ),
172 "vcenter_validate_certs": dict(
173 type="bool",
174 required=False,
175 default=True,
176 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
177 ),
178 "vcenter_rest_log_file": dict(
179 type="str",
180 required=False,
181 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
182 ),
183 "session_timeout": dict(
184 type="float",
185 required=False,
186 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
187 ),
188 }
189
190 argument_spec["vm"] = {"type": "str"}
191
192 return argument_spec
193
194
195 async def main():
196 required_if = list([])
197
198 module_args = prepare_argument_spec()
199 module = AnsibleModule(
200 argument_spec=module_args, required_if=required_if, supports_check_mode=True
201 )
202 if not module.params["vcenter_hostname"]:
203 module.fail_json("vcenter_hostname cannot be empty")
204 if not module.params["vcenter_username"]:
205 module.fail_json("vcenter_username cannot be empty")
206 if not module.params["vcenter_password"]:
207 module.fail_json("vcenter_password cannot be empty")
208 try:
209 session = await open_session(
210 vcenter_hostname=module.params["vcenter_hostname"],
211 vcenter_username=module.params["vcenter_username"],
212 vcenter_password=module.params["vcenter_password"],
213 validate_certs=module.params["vcenter_validate_certs"],
214 log_file=module.params["vcenter_rest_log_file"],
215 )
216 except Exception as err:
217 if hasattr(err, "get_message"):
218 module.fail_json(err.get_message())
219 else:
220 module.fail_json(str(err))
221 result = await entry_point(module, session)
222 module.exit_json(**result)
223
224
225 # template: info_no_list_module.j2
226 def build_url(params):
227 return ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/tools/installer").format(
228 **params
229 )
230
231
232 async def entry_point(module, session):
233 return await _info(module.params, session)
234
235
236 async def _info(params, session):
237 payload_format = list(PAYLOAD_FORMAT.values())[0]
238 _in_query_parameters = payload_format["query"].keys()
239 _url = ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/tools/installer").format(
240 **params
241 ) + gen_args(params, _in_query_parameters)
242 async with session.get(_url, **session_timeout(params)) as resp:
243 try:
244 if resp.headers["Content-Type"] == "application/json":
245 _json = await resp.json()
246 except KeyError:
247 _json = {}
248 return await update_changed_flag(_json, resp.status, "get")
249
250
251 if __name__ == "__main__":
252 import asyncio
253
254 current_loop = asyncio.new_event_loop()
255 try:
256 asyncio.set_event_loop(current_loop)
257 current_loop.run_until_complete(main())
258 finally:
259 current_loop.close()