Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/appliance_access_shell_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: appliance_access_shell_info
12 short_description: Get enabled state of BASH, that is, access to BASH from within
13 the controlled CLI.
14 description: Get enabled state of BASH, that is, access to BASH from within the controlled
15 CLI.
16 deprecated:
17 removed_in: 5.0.0
18 why: This module has been moved to the L(new vmware.vmware collection,https://forum.ansible.com/t/5880)
19 alternative: Use M(vmware.vmware.appliance_info) instead.
20 options:
21 session_timeout:
22 description:
23 - 'Timeout settings for client session. '
24 - 'The maximal number of seconds for the whole operation including connection
25 establishment, request sending and response. '
26 - The default value is 300s.
27 type: float
28 version_added: 2.1.0
29 vcenter_hostname:
30 description:
31 - The hostname or IP address of the vSphere vCenter
32 - If the value is not specified in the task, the value of environment variable
33 C(VMWARE_HOST) will be used instead.
34 required: true
35 type: str
36 vcenter_password:
37 description:
38 - The vSphere vCenter password
39 - If the value is not specified in the task, the value of environment variable
40 C(VMWARE_PASSWORD) will be used instead.
41 required: true
42 type: str
43 vcenter_rest_log_file:
44 description:
45 - 'You can use this optional parameter to set the location of a log file. '
46 - 'This file will be used to record the HTTP REST interaction. '
47 - 'The file will be stored on the host that runs the module. '
48 - 'If the value is not specified in the task, the value of '
49 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
50 type: str
51 vcenter_username:
52 description:
53 - The vSphere vCenter username
54 - If the value is not specified in the task, the value of environment variable
55 C(VMWARE_USER) will be used instead.
56 required: true
57 type: str
58 vcenter_validate_certs:
59 default: true
60 description:
61 - Allows connection when SSL certificates are not valid. Set to C(false) when
62 certificates are not trusted.
63 - If the value is not specified in the task, the value of environment variable
64 C(VMWARE_VALIDATE_CERTS) will be used instead.
65 type: bool
66 author:
67 - Ansible Cloud Team (@ansible-collections)
68 version_added: 2.0.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: Check if the Shell is enabled
79 vmware.vmware_rest.appliance_access_shell_info:
80 register: result
81 """
82 RETURN = r"""
83 # content generated by the update_return_section callback# task: Check if the Shell is enabled
84 value:
85 description: Check if the Shell is enabled
86 returned: On success
87 sample:
88 enabled: 0
89 timeout: 0
90 type: dict
91 """
92
93
94 # This structure describes the format of the data expected by the end-points
95 PAYLOAD_FORMAT = {
96 "get": {"query": {}, "body": {}, "path": {}}
97 } # pylint: disable=line-too-long
98
99 from ansible.module_utils.basic import env_fallback
100 import os
101
102 if os.getenv("VMWARE_ENABLE_TURBO", False):
103 try:
104 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
105 AnsibleTurboModule as AnsibleModule,
106 )
107
108 AnsibleModule.collection_name = "vmware.vmware_rest"
109 except ImportError:
110 from ansible.module_utils.basic import AnsibleModule
111 else:
112 from ansible.module_utils.basic import AnsibleModule
113
114 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
115 gen_args,
116 open_session,
117 session_timeout,
118 update_changed_flag,
119 )
120
121
122 def prepare_argument_spec():
123 argument_spec = {
124 "vcenter_hostname": dict(
125 type="str",
126 required=True,
127 fallback=(env_fallback, ["VMWARE_HOST"]),
128 ),
129 "vcenter_username": dict(
130 type="str",
131 required=True,
132 fallback=(env_fallback, ["VMWARE_USER"]),
133 ),
134 "vcenter_password": dict(
135 type="str",
136 required=True,
137 no_log=True,
138 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
139 ),
140 "vcenter_validate_certs": dict(
141 type="bool",
142 required=False,
143 default=True,
144 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
145 ),
146 "vcenter_rest_log_file": dict(
147 type="str",
148 required=False,
149 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
150 ),
151 "session_timeout": dict(
152 type="float",
153 required=False,
154 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
155 ),
156 }
157
158 return argument_spec
159
160
161 async def main():
162 required_if = list([])
163
164 module_args = prepare_argument_spec()
165 module = AnsibleModule(
166 argument_spec=module_args, required_if=required_if, supports_check_mode=True
167 )
168 if not module.params["vcenter_hostname"]:
169 module.fail_json("vcenter_hostname cannot be empty")
170 if not module.params["vcenter_username"]:
171 module.fail_json("vcenter_username cannot be empty")
172 if not module.params["vcenter_password"]:
173 module.fail_json("vcenter_password cannot be empty")
174 try:
175 session = await open_session(
176 vcenter_hostname=module.params["vcenter_hostname"],
177 vcenter_username=module.params["vcenter_username"],
178 vcenter_password=module.params["vcenter_password"],
179 validate_certs=module.params["vcenter_validate_certs"],
180 log_file=module.params["vcenter_rest_log_file"],
181 )
182 except Exception as err:
183 if hasattr(err, "get_message"):
184 module.fail_json(err.get_message())
185 else:
186 module.fail_json(str(err))
187 result = await entry_point(module, session)
188 module.exit_json(**result)
189
190
191 # template: info_no_list_module.j2
192 def build_url(params):
193 return ("https://{vcenter_hostname}" "/api/appliance/access/shell").format(**params)
194
195
196 async def entry_point(module, session):
197 return await _info(module.params, session)
198
199
200 async def _info(params, session):
201 payload_format = list(PAYLOAD_FORMAT.values())[0]
202 _in_query_parameters = payload_format["query"].keys()
203 _url = ("https://{vcenter_hostname}" "/api/appliance/access/shell").format(
204 **params
205 ) + gen_args(params, _in_query_parameters)
206 async with session.get(_url, **session_timeout(params)) as resp:
207 try:
208 if resp.headers["Content-Type"] == "application/json":
209 _json = await resp.json()
210 except KeyError:
211 _json = {}
212 return await update_changed_flag(_json, resp.status, "get")
213
214
215 if __name__ == "__main__":
216 import asyncio
217
218 current_loop = asyncio.new_event_loop()
219 try:
220 asyncio.set_event_loop(current_loop)
221 current_loop.run_until_complete(main())
222 finally:
223 current_loop.close()