| 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_system_globalfips |
| 12 |
short_description: Enable/Disable Global FIPS mode for the appliance |
| 13 |
description: 'Enable/Disable Global FIPS mode for the appliance. ' |
| 14 |
deprecated: |
| 15 |
removed_in: 5.0.0 |
| 16 |
why: This module has been moved to the L(new vmware.vmware collection,https://forum.ansible.com/t/5880) |
| 17 |
alternative: Use M(vmware.vmware.vcsa_settings) instead. |
| 18 |
options: |
| 19 |
enabled: |
| 20 |
description: |
| 21 |
- FIPS setting state. |
| 22 |
- If unset, the value is unchanged. |
| 23 |
type: bool |
| 24 |
session_timeout: |
| 25 |
description: |
| 26 |
- 'Timeout settings for client session. ' |
| 27 |
- 'The maximal number of seconds for the whole operation including connection |
| 28 |
establishment, request sending and response. ' |
| 29 |
- The default value is 300s. |
| 30 |
type: float |
| 31 |
version_added: 2.1.0 |
| 32 |
state: |
| 33 |
choices: |
| 34 |
- present |
| 35 |
default: present |
| 36 |
description: [] |
| 37 |
type: str |
| 38 |
vcenter_hostname: |
| 39 |
description: |
| 40 |
- The hostname or IP address of the vSphere vCenter |
| 41 |
- If the value is not specified in the task, the value of environment variable |
| 42 |
C(VMWARE_HOST) will be used instead. |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
vcenter_password: |
| 46 |
description: |
| 47 |
- The vSphere vCenter password |
| 48 |
- If the value is not specified in the task, the value of environment variable |
| 49 |
C(VMWARE_PASSWORD) will be used instead. |
| 50 |
required: true |
| 51 |
type: str |
| 52 |
vcenter_rest_log_file: |
| 53 |
description: |
| 54 |
- 'You can use this optional parameter to set the location of a log file. ' |
| 55 |
- 'This file will be used to record the HTTP REST interaction. ' |
| 56 |
- 'The file will be stored on the host that runs the module. ' |
| 57 |
- 'If the value is not specified in the task, the value of ' |
| 58 |
- environment variable C(VMWARE_REST_LOG_FILE) will be used instead. |
| 59 |
type: str |
| 60 |
vcenter_username: |
| 61 |
description: |
| 62 |
- The vSphere vCenter username |
| 63 |
- If the value is not specified in the task, the value of environment variable |
| 64 |
C(VMWARE_USER) will be used instead. |
| 65 |
required: true |
| 66 |
type: str |
| 67 |
vcenter_validate_certs: |
| 68 |
default: true |
| 69 |
description: |
| 70 |
- Allows connection when SSL certificates are not valid. Set to C(false) when |
| 71 |
certificates are not trusted. |
| 72 |
- If the value is not specified in the task, the value of environment variable |
| 73 |
C(VMWARE_VALIDATE_CERTS) will be used instead. |
| 74 |
type: bool |
| 75 |
author: |
| 76 |
- Ansible Cloud Team (@ansible-collections) |
| 77 |
version_added: 2.0.0 |
| 78 |
requirements: |
| 79 |
- vSphere 7.0.3 or greater |
| 80 |
- python >= 3.6 |
| 81 |
- aiohttp |
| 82 |
notes: |
| 83 |
- Tested on vSphere 7.0.3 |
| 84 |
""" |
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
""" |
| 88 |
RETURN = r""" |
| 89 |
""" |
| 90 |
|
| 91 |
|
| 92 |
# This structure describes the format of the data expected by the end-points |
| 93 |
PAYLOAD_FORMAT = { |
| 94 |
"update": {"query": {}, "body": {"enabled": "enabled"}, "path": {}} |
| 95 |
} # pylint: disable=line-too-long |
| 96 |
|
| 97 |
from ansible.module_utils.basic import env_fallback |
| 98 |
import os |
| 99 |
|
| 100 |
if os.getenv("VMWARE_ENABLE_TURBO", False): |
| 101 |
try: |
| 102 |
from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( |
| 103 |
AnsibleTurboModule as AnsibleModule, |
| 104 |
) |
| 105 |
|
| 106 |
AnsibleModule.collection_name = "vmware.vmware_rest" |
| 107 |
except ImportError: |
| 108 |
from ansible.module_utils.basic import AnsibleModule |
| 109 |
else: |
| 110 |
from ansible.module_utils.basic import AnsibleModule |
| 111 |
|
| 112 |
from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import ( |
| 113 |
open_session, |
| 114 |
prepare_payload, |
| 115 |
session_timeout, |
| 116 |
update_changed_flag, |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
def prepare_argument_spec(): |
| 121 |
argument_spec = { |
| 122 |
"vcenter_hostname": dict( |
| 123 |
type="str", |
| 124 |
required=True, |
| 125 |
fallback=(env_fallback, ["VMWARE_HOST"]), |
| 126 |
), |
| 127 |
"vcenter_username": dict( |
| 128 |
type="str", |
| 129 |
required=True, |
| 130 |
fallback=(env_fallback, ["VMWARE_USER"]), |
| 131 |
), |
| 132 |
"vcenter_password": dict( |
| 133 |
type="str", |
| 134 |
required=True, |
| 135 |
no_log=True, |
| 136 |
fallback=(env_fallback, ["VMWARE_PASSWORD"]), |
| 137 |
), |
| 138 |
"vcenter_validate_certs": dict( |
| 139 |
type="bool", |
| 140 |
required=False, |
| 141 |
default=True, |
| 142 |
fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]), |
| 143 |
), |
| 144 |
"vcenter_rest_log_file": dict( |
| 145 |
type="str", |
| 146 |
required=False, |
| 147 |
fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]), |
| 148 |
), |
| 149 |
"session_timeout": dict( |
| 150 |
type="float", |
| 151 |
required=False, |
| 152 |
fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]), |
| 153 |
), |
| 154 |
} |
| 155 |
|
| 156 |
argument_spec["enabled"] = {"type": "bool"} |
| 157 |
argument_spec["state"] = { |
| 158 |
"type": "str", |
| 159 |
"choices": ["present"], |
| 160 |
"default": "present", |
| 161 |
} |
| 162 |
|
| 163 |
return argument_spec |
| 164 |
|
| 165 |
|
| 166 |
async def main(): |
| 167 |
required_if = list([]) |
| 168 |
|
| 169 |
module_args = prepare_argument_spec() |
| 170 |
module = AnsibleModule( |
| 171 |
argument_spec=module_args, required_if=required_if, supports_check_mode=False |
| 172 |
) |
| 173 |
if not module.params["vcenter_hostname"]: |
| 174 |
module.fail_json("vcenter_hostname cannot be empty") |
| 175 |
if not module.params["vcenter_username"]: |
| 176 |
module.fail_json("vcenter_username cannot be empty") |
| 177 |
if not module.params["vcenter_password"]: |
| 178 |
module.fail_json("vcenter_password cannot be empty") |
| 179 |
try: |
| 180 |
session = await open_session( |
| 181 |
vcenter_hostname=module.params["vcenter_hostname"], |
| 182 |
vcenter_username=module.params["vcenter_username"], |
| 183 |
vcenter_password=module.params["vcenter_password"], |
| 184 |
validate_certs=module.params["vcenter_validate_certs"], |
| 185 |
log_file=module.params["vcenter_rest_log_file"], |
| 186 |
) |
| 187 |
except Exception as err: |
| 188 |
if hasattr(err, "get_message"): |
| 189 |
module.fail_json(err.get_message()) |
| 190 |
else: |
| 191 |
module.fail_json(str(err)) |
| 192 |
result = await entry_point(module, session) |
| 193 |
module.exit_json(**result) |
| 194 |
|
| 195 |
|
| 196 |
# template: default_module.j2 |
| 197 |
def build_url(params): |
| 198 |
return ("https://{vcenter_hostname}" "/api/appliance/system/global-fips").format( |
| 199 |
**params |
| 200 |
) |
| 201 |
|
| 202 |
|
| 203 |
async def entry_point(module, session): |
| 204 |
|
| 205 |
if module.params["state"] == "present": |
| 206 |
if "_create" in globals(): |
| 207 |
operation = "create" |
| 208 |
else: |
| 209 |
operation = "update" |
| 210 |
elif module.params["state"] == "absent": |
| 211 |
operation = "delete" |
| 212 |
else: |
| 213 |
operation = module.params["state"] |
| 214 |
|
| 215 |
func = globals()["_" + operation] |
| 216 |
|
| 217 |
return await func(module.params, session) |
| 218 |
|
| 219 |
|
| 220 |
async def _update(params, session): |
| 221 |
payload = prepare_payload(params, PAYLOAD_FORMAT["update"]) |
| 222 |
_url = ("https://{vcenter_hostname}" "/api/appliance/system/global-fips").format( |
| 223 |
**params |
| 224 |
) |
| 225 |
async with session.get(_url, **session_timeout(params)) as resp: |
| 226 |
_json = await resp.json() |
| 227 |
if "value" in _json: |
| 228 |
value = _json["value"] |
| 229 |
else: # 7.0.2 and greater |
| 230 |
value = _json |
| 231 |
for k, v in value.items(): |
| 232 |
if k in payload: |
| 233 |
if isinstance(payload[k], dict) and isinstance(v, dict): |
| 234 |
to_delete = True |
| 235 |
for _k in list(payload[k].keys()): |
| 236 |
if payload[k][_k] != v.get(_k): |
| 237 |
to_delete = False |
| 238 |
if to_delete: |
| 239 |
del payload[k] |
| 240 |
elif payload[k] == v: |
| 241 |
del payload[k] |
| 242 |
elif payload[k] == {}: |
| 243 |
del payload[k] |
| 244 |
|
| 245 |
if payload == {} or payload == {"spec": {}}: |
| 246 |
# Nothing has changed |
| 247 |
if "value" not in _json: # 7.0.2 |
| 248 |
_json = {"value": _json} |
| 249 |
_json["id"] = params.get("None") |
| 250 |
return await update_changed_flag(_json, resp.status, "get") |
| 251 |
async with session.put(_url, json=payload, **session_timeout(params)) as resp: |
| 252 |
try: |
| 253 |
if resp.headers["Content-Type"] == "application/json": |
| 254 |
_json = await resp.json() |
| 255 |
except KeyError: |
| 256 |
_json = {} |
| 257 |
if "value" not in _json: # 7.0.2 |
| 258 |
_json = {"value": _json} |
| 259 |
|
| 260 |
# e.g: content_configuration |
| 261 |
if not _json and resp.status == 204: |
| 262 |
async with session.get(_url, **session_timeout(params)) as resp_get: |
| 263 |
_json_get = await resp_get.json() |
| 264 |
if _json_get: |
| 265 |
_json = _json_get |
| 266 |
|
| 267 |
_json["id"] = params.get("None") |
| 268 |
return await update_changed_flag(_json, resp.status, "update") |
| 269 |
|
| 270 |
|
| 271 |
if __name__ == "__main__": |
| 272 |
import asyncio |
| 273 |
|
| 274 |
current_loop = asyncio.new_event_loop() |
| 275 |
try: |
| 276 |
asyncio.set_event_loop(current_loop) |
| 277 |
current_loop.run_until_complete(main()) |
| 278 |
finally: |
| 279 |
current_loop.close() |