Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/vcenter_vm_guest_identity_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_guest_identity_info
12 short_description: Return information about the guest.
13 description: Return information about the guest.
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: Look up the VM called test_vm1 in the inventory
79 register: search_result
80 vmware.vmware_rest.vcenter_vm_info:
81 filter_names:
82 - test_vm1
83
84 - name: Collect information about a specific VM
85 vmware.vmware_rest.vcenter_vm_info:
86 vm: '{{ search_result.value[0].vm }}'
87 register: test_vm1_info
88
89 - name: Get guest identity information
90 vmware.vmware_rest.vcenter_vm_guest_identity_info:
91 vm: '{{ test_vm1_info.id }}'
92 register: _result
93 """
94 RETURN = r"""
95 # content generated by the update_return_section callback# task: Get guest identity information
96 value:
97 description: Get guest identity information
98 returned: On success
99 sample:
100 family: LINUX
101 full_name:
102 args: []
103 default_message: Red Hat Fedora (64-bit)
104 id: vmsg.guestos.fedora64Guest.label
105 host_name: localhost.localdomain
106 name: FEDORA_64
107 type: dict
108 """
109
110
111 # This structure describes the format of the data expected by the end-points
112 PAYLOAD_FORMAT = {
113 "get": {"query": {}, "body": {}, "path": {"vm": "vm"}}
114 } # pylint: disable=line-too-long
115
116 from ansible.module_utils.basic import env_fallback
117 import os
118
119 if os.getenv("VMWARE_ENABLE_TURBO", False):
120 try:
121 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
122 AnsibleTurboModule as AnsibleModule,
123 )
124
125 AnsibleModule.collection_name = "vmware.vmware_rest"
126 except ImportError:
127 from ansible.module_utils.basic import AnsibleModule
128 else:
129 from ansible.module_utils.basic import AnsibleModule
130
131 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
132 gen_args,
133 open_session,
134 session_timeout,
135 update_changed_flag,
136 )
137
138
139 def prepare_argument_spec():
140 argument_spec = {
141 "vcenter_hostname": dict(
142 type="str",
143 required=True,
144 fallback=(env_fallback, ["VMWARE_HOST"]),
145 ),
146 "vcenter_username": dict(
147 type="str",
148 required=True,
149 fallback=(env_fallback, ["VMWARE_USER"]),
150 ),
151 "vcenter_password": dict(
152 type="str",
153 required=True,
154 no_log=True,
155 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
156 ),
157 "vcenter_validate_certs": dict(
158 type="bool",
159 required=False,
160 default=True,
161 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
162 ),
163 "vcenter_rest_log_file": dict(
164 type="str",
165 required=False,
166 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
167 ),
168 "session_timeout": dict(
169 type="float",
170 required=False,
171 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
172 ),
173 }
174
175 argument_spec["vm"] = {"type": "str"}
176
177 return argument_spec
178
179
180 async def main():
181 required_if = list([])
182
183 module_args = prepare_argument_spec()
184 module = AnsibleModule(
185 argument_spec=module_args, required_if=required_if, supports_check_mode=True
186 )
187 if not module.params["vcenter_hostname"]:
188 module.fail_json("vcenter_hostname cannot be empty")
189 if not module.params["vcenter_username"]:
190 module.fail_json("vcenter_username cannot be empty")
191 if not module.params["vcenter_password"]:
192 module.fail_json("vcenter_password cannot be empty")
193 try:
194 session = await open_session(
195 vcenter_hostname=module.params["vcenter_hostname"],
196 vcenter_username=module.params["vcenter_username"],
197 vcenter_password=module.params["vcenter_password"],
198 validate_certs=module.params["vcenter_validate_certs"],
199 log_file=module.params["vcenter_rest_log_file"],
200 )
201 except Exception as err:
202 if hasattr(err, "get_message"):
203 module.fail_json(err.get_message())
204 else:
205 module.fail_json(str(err))
206 result = await entry_point(module, session)
207 module.exit_json(**result)
208
209
210 # template: info_no_list_module.j2
211 def build_url(params):
212 return ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/guest/identity").format(
213 **params
214 )
215
216
217 async def entry_point(module, session):
218 return await _info(module.params, session)
219
220
221 async def _info(params, session):
222 payload_format = list(PAYLOAD_FORMAT.values())[0]
223 _in_query_parameters = payload_format["query"].keys()
224 _url = ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/guest/identity").format(
225 **params
226 ) + gen_args(params, _in_query_parameters)
227 async with session.get(_url, **session_timeout(params)) as resp:
228 try:
229 if resp.headers["Content-Type"] == "application/json":
230 _json = await resp.json()
231 except KeyError:
232 _json = {}
233 return await update_changed_flag(_json, resp.status, "get")
234
235
236 if __name__ == "__main__":
237 import asyncio
238
239 current_loop = asyncio.new_event_loop()
240 try:
241 asyncio.set_event_loop(current_loop)
242 current_loop.run_until_complete(main())
243 finally:
244 current_loop.close()