Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/vcenter_vm_tools_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_info
12 short_description: Get the properties of VMware Tools.
13 description: Get the properties of VMware Tools.
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: Wait until my VM is ready
112 vmware.vmware_rest.vcenter_vm_tools_info:
113 vm: '{{ my_vm.id }}'
114 register: vm_tools_info
115 until:
116 - vm_tools_info is not failed
117 - vm_tools_info.value.run_state == "RUNNING"
118 retries: 10
119 delay: 5
120
121 - name: Wait until my VM is ready
122 vmware.vmware_rest.vcenter_vm_tools_info:
123 vm: '{{ my_vm.id }}'
124 register: vm_tools_info
125 until:
126 - vm_tools_info is not failed
127 - vm_tools_info.value.run_state == "RUNNING"
128 retries: 60
129 delay: 5
130
131 - name: Look up the VM called test_vm1 in the inventory
132 register: search_result
133 vmware.vmware_rest.vcenter_vm_info:
134 filter_names:
135 - test_vm1
136
137 - name: Collect information about a specific VM
138 vmware.vmware_rest.vcenter_vm_info:
139 vm: '{{ search_result.value[0].vm }}'
140 register: test_vm1_info
141
142 - name: Wait until my VM is ready
143 vmware.vmware_rest.vcenter_vm_tools_info:
144 vm: '{{ test_vm1_info.id }}'
145 register: vm_tools_info
146 until:
147 - vm_tools_info is not failed
148 - vm_tools_info.value.run_state == "RUNNING"
149 retries: 60
150 delay: 5
151 """
152 RETURN = r"""
153 # content generated by the update_return_section callback# task: Wait until my VM is ready
154 value:
155 description: Wait until my VM is ready
156 returned: On success
157 sample:
158 auto_update_supported: 0
159 install_attempt_count: 0
160 install_type: OPEN_VM_TOOLS
161 run_state: RUNNING
162 upgrade_policy: MANUAL
163 version: '10346'
164 version_number: 10346
165 version_status: UNMANAGED
166 type: dict
167 """
168
169
170 # This structure describes the format of the data expected by the end-points
171 PAYLOAD_FORMAT = {
172 "get": {"query": {}, "body": {}, "path": {"vm": "vm"}}
173 } # pylint: disable=line-too-long
174
175 from ansible.module_utils.basic import env_fallback
176 import os
177
178 if os.getenv("VMWARE_ENABLE_TURBO", False):
179 try:
180 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
181 AnsibleTurboModule as AnsibleModule,
182 )
183
184 AnsibleModule.collection_name = "vmware.vmware_rest"
185 except ImportError:
186 from ansible.module_utils.basic import AnsibleModule
187 else:
188 from ansible.module_utils.basic import AnsibleModule
189
190 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
191 gen_args,
192 open_session,
193 session_timeout,
194 update_changed_flag,
195 )
196
197
198 def prepare_argument_spec():
199 argument_spec = {
200 "vcenter_hostname": dict(
201 type="str",
202 required=True,
203 fallback=(env_fallback, ["VMWARE_HOST"]),
204 ),
205 "vcenter_username": dict(
206 type="str",
207 required=True,
208 fallback=(env_fallback, ["VMWARE_USER"]),
209 ),
210 "vcenter_password": dict(
211 type="str",
212 required=True,
213 no_log=True,
214 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
215 ),
216 "vcenter_validate_certs": dict(
217 type="bool",
218 required=False,
219 default=True,
220 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
221 ),
222 "vcenter_rest_log_file": dict(
223 type="str",
224 required=False,
225 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
226 ),
227 "session_timeout": dict(
228 type="float",
229 required=False,
230 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
231 ),
232 }
233
234 argument_spec["vm"] = {"type": "str"}
235
236 return argument_spec
237
238
239 async def main():
240 required_if = list([])
241
242 module_args = prepare_argument_spec()
243 module = AnsibleModule(
244 argument_spec=module_args, required_if=required_if, supports_check_mode=True
245 )
246 if not module.params["vcenter_hostname"]:
247 module.fail_json("vcenter_hostname cannot be empty")
248 if not module.params["vcenter_username"]:
249 module.fail_json("vcenter_username cannot be empty")
250 if not module.params["vcenter_password"]:
251 module.fail_json("vcenter_password cannot be empty")
252 try:
253 session = await open_session(
254 vcenter_hostname=module.params["vcenter_hostname"],
255 vcenter_username=module.params["vcenter_username"],
256 vcenter_password=module.params["vcenter_password"],
257 validate_certs=module.params["vcenter_validate_certs"],
258 log_file=module.params["vcenter_rest_log_file"],
259 )
260 except Exception as err:
261 if hasattr(err, "get_message"):
262 module.fail_json(err.get_message())
263 else:
264 module.fail_json(str(err))
265 result = await entry_point(module, session)
266 module.exit_json(**result)
267
268
269 # template: info_no_list_module.j2
270 def build_url(params):
271 return ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/tools").format(**params)
272
273
274 async def entry_point(module, session):
275 return await _info(module.params, session)
276
277
278 async def _info(params, session):
279 payload_format = list(PAYLOAD_FORMAT.values())[0]
280 _in_query_parameters = payload_format["query"].keys()
281 _url = ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/tools").format(
282 **params
283 ) + gen_args(params, _in_query_parameters)
284 async with session.get(_url, **session_timeout(params)) as resp:
285 try:
286 if resp.headers["Content-Type"] == "application/json":
287 _json = await resp.json()
288 except KeyError:
289 _json = {}
290 return await update_changed_flag(_json, resp.status, "get")
291
292
293 if __name__ == "__main__":
294 import asyncio
295
296 current_loop = asyncio.new_event_loop()
297 try:
298 asyncio.set_event_loop(current_loop)
299 current_loop.run_until_complete(main())
300 finally:
301 current_loop.close()