| 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_infraprofile_configs |
| 12 |
short_description: Exports the desired profile specification. |
| 13 |
description: Exports the desired profile specification. |
| 14 |
options: |
| 15 |
description: |
| 16 |
description: |
| 17 |
- Custom description provided by the user. |
| 18 |
- If unset description will be empty. |
| 19 |
type: str |
| 20 |
encryption_key: |
| 21 |
description: |
| 22 |
- Encryption Key to encrypt/decrypt profiles. |
| 23 |
- If unset encryption will not be used for the profile. |
| 24 |
type: str |
| 25 |
profiles: |
| 26 |
description: |
| 27 |
- Profiles to be exported/imported. |
| 28 |
- If unset or empty, all profiles will be returned. |
| 29 |
- When clients pass a value of this structure as a parameter, the field must |
| 30 |
contain the id of resources returned by M(vmware.vmware_rest.appliance_infraprofile_configs). |
| 31 |
elements: str |
| 32 |
type: list |
| 33 |
session_timeout: |
| 34 |
description: |
| 35 |
- 'Timeout settings for client session. ' |
| 36 |
- 'The maximal number of seconds for the whole operation including connection |
| 37 |
establishment, request sending and response. ' |
| 38 |
- The default value is 300s. |
| 39 |
type: float |
| 40 |
version_added: 2.1.0 |
| 41 |
state: |
| 42 |
choices: |
| 43 |
- export |
| 44 |
description: [] |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
vcenter_hostname: |
| 48 |
description: |
| 49 |
- The hostname or IP address of the vSphere vCenter |
| 50 |
- If the value is not specified in the task, the value of environment variable |
| 51 |
C(VMWARE_HOST) will be used instead. |
| 52 |
required: true |
| 53 |
type: str |
| 54 |
vcenter_password: |
| 55 |
description: |
| 56 |
- The vSphere vCenter password |
| 57 |
- If the value is not specified in the task, the value of environment variable |
| 58 |
C(VMWARE_PASSWORD) will be used instead. |
| 59 |
required: true |
| 60 |
type: str |
| 61 |
vcenter_rest_log_file: |
| 62 |
description: |
| 63 |
- 'You can use this optional parameter to set the location of a log file. ' |
| 64 |
- 'This file will be used to record the HTTP REST interaction. ' |
| 65 |
- 'The file will be stored on the host that runs the module. ' |
| 66 |
- 'If the value is not specified in the task, the value of ' |
| 67 |
- environment variable C(VMWARE_REST_LOG_FILE) will be used instead. |
| 68 |
type: str |
| 69 |
vcenter_username: |
| 70 |
description: |
| 71 |
- The vSphere vCenter username |
| 72 |
- If the value is not specified in the task, the value of environment variable |
| 73 |
C(VMWARE_USER) will be used instead. |
| 74 |
required: true |
| 75 |
type: str |
| 76 |
vcenter_validate_certs: |
| 77 |
default: true |
| 78 |
description: |
| 79 |
- Allows connection when SSL certificates are not valid. Set to C(false) when |
| 80 |
certificates are not trusted. |
| 81 |
- If the value is not specified in the task, the value of environment variable |
| 82 |
C(VMWARE_VALIDATE_CERTS) will be used instead. |
| 83 |
type: bool |
| 84 |
author: |
| 85 |
- Ansible Cloud Team (@ansible-collections) |
| 86 |
version_added: 2.0.0 |
| 87 |
requirements: |
| 88 |
- vSphere 7.0.3 or greater |
| 89 |
- python >= 3.6 |
| 90 |
- aiohttp |
| 91 |
notes: |
| 92 |
- Tested on vSphere 7.0.3 |
| 93 |
""" |
| 94 |
|
| 95 |
EXAMPLES = r""" |
| 96 |
- name: Export the ApplianceManagement profile |
| 97 |
vmware.vmware_rest.appliance_infraprofile_configs: |
| 98 |
state: export |
| 99 |
profiles: |
| 100 |
- ApplianceManagement |
| 101 |
register: result |
| 102 |
""" |
| 103 |
RETURN = r""" |
| 104 |
# content generated by the update_return_section callback# task: Export the ApplianceManagement profile |
| 105 |
value: |
| 106 |
description: Export the ApplianceManagement profile |
| 107 |
returned: On success |
| 108 |
sample: |
| 109 |
{ |
| 110 |
"action": "RESTART_SERVICE", |
| 111 |
"productName": "VMware vCenter Server", |
| 112 |
"creationTime": "2022-11-23T20:11:00+0000", |
| 113 |
"version": "7.0.3.00800", |
| 114 |
"profiles": { |
| 115 |
"ApplianceManagement": { |
| 116 |
"action": "RESTART_SERVICE", |
| 117 |
"actionOn": { |
| 118 |
"VC_SERVICES": [ |
| 119 |
"applmgmt" |
| 120 |
], |
| 121 |
"SYSTEMD": [ |
| 122 |
"sendmail", |
| 123 |
"rsyslog" |
| 124 |
] |
| 125 |
}, |
| 126 |
"description": "Appliance Mangment Service", |
| 127 |
"version": "7.0", |
| 128 |
"config": { |
| 129 |
"/etc/applmgmt/appliance/appliance.conf": { |
| 130 |
"Is shell Enabled": true, |
| 131 |
"Shell Expiration Time": 9, |
| 132 |
"TimeSync Mode (Host/NTP)": "NTP" |
| 133 |
}, |
| 134 |
"/etc/ntp.conf": { |
| 135 |
"Time servers": "time.nist.gov" |
| 136 |
}, |
| 137 |
"/etc/shadow": { |
| 138 |
"root": { |
| 139 |
"maximumDays": "90", |
| 140 |
"warningDays": "7" |
| 141 |
}, |
| 142 |
"Send Waring before this No of Days.": null, |
| 143 |
"Password validity (days)": null |
| 144 |
}, |
| 145 |
"/etc/sysconfig/clock": { |
| 146 |
"Time zone": "\"Etc/UTC\"", |
| 147 |
"UTC": "1" |
| 148 |
}, |
| 149 |
"/usr/bin/systemctl/sshd.service": { |
| 150 |
"Enable SSH": "true" |
| 151 |
}, |
| 152 |
"/etc/mail/sendmail.cf": { |
| 153 |
"SMTP Port": null, |
| 154 |
"Mail server": null |
| 155 |
}, |
| 156 |
"/etc/vmware-syslog/syslog.conf": { |
| 157 |
"Port [2]": null, |
| 158 |
"Port [1]": null, |
| 159 |
"Port [0]": null, |
| 160 |
"Protocol [2]": null, |
| 161 |
"Remote Syslog Host [1]": null, |
| 162 |
"Protocol [1]": null, |
| 163 |
"Remote Syslog Host [0]": null, |
| 164 |
"Protocol [0]": null, |
| 165 |
"Remote Syslog Host [2]": null |
| 166 |
}, |
| 167 |
"/etc/pam.d/system-auth": { |
| 168 |
"Deny Login after these many Unsuccessful Attempts.": null, |
| 169 |
"Unlock root after (seconds)": null, |
| 170 |
"On Error Login will be.": null, |
| 171 |
"Include Root user for SSH lockout.": null, |
| 172 |
"Unlock user after (seconds)": null |
| 173 |
} |
| 174 |
}, |
| 175 |
"name": "ApplianceManagement" |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
type: str |
| 180 |
""" |
| 181 |
|
| 182 |
|
| 183 |
# This structure describes the format of the data expected by the end-points |
| 184 |
PAYLOAD_FORMAT = { |
| 185 |
"export": { |
| 186 |
"query": {}, |
| 187 |
"body": { |
| 188 |
"description": "description", |
| 189 |
"encryption_key": "encryption_key", |
| 190 |
"profiles": "profiles", |
| 191 |
}, |
| 192 |
"path": {}, |
| 193 |
} |
| 194 |
} # pylint: disable=line-too-long |
| 195 |
|
| 196 |
from ansible.module_utils.basic import env_fallback |
| 197 |
import os |
| 198 |
|
| 199 |
if os.getenv("VMWARE_ENABLE_TURBO", False): |
| 200 |
try: |
| 201 |
from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( |
| 202 |
AnsibleTurboModule as AnsibleModule, |
| 203 |
) |
| 204 |
|
| 205 |
AnsibleModule.collection_name = "vmware.vmware_rest" |
| 206 |
except ImportError: |
| 207 |
from ansible.module_utils.basic import AnsibleModule |
| 208 |
else: |
| 209 |
from ansible.module_utils.basic import AnsibleModule |
| 210 |
|
| 211 |
from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import ( |
| 212 |
exists, |
| 213 |
gen_args, |
| 214 |
get_subdevice_type, |
| 215 |
open_session, |
| 216 |
prepare_payload, |
| 217 |
session_timeout, |
| 218 |
update_changed_flag, |
| 219 |
) |
| 220 |
|
| 221 |
|
| 222 |
def prepare_argument_spec(): |
| 223 |
argument_spec = { |
| 224 |
"vcenter_hostname": dict( |
| 225 |
type="str", |
| 226 |
required=True, |
| 227 |
fallback=(env_fallback, ["VMWARE_HOST"]), |
| 228 |
), |
| 229 |
"vcenter_username": dict( |
| 230 |
type="str", |
| 231 |
required=True, |
| 232 |
fallback=(env_fallback, ["VMWARE_USER"]), |
| 233 |
), |
| 234 |
"vcenter_password": dict( |
| 235 |
type="str", |
| 236 |
required=True, |
| 237 |
no_log=True, |
| 238 |
fallback=(env_fallback, ["VMWARE_PASSWORD"]), |
| 239 |
), |
| 240 |
"vcenter_validate_certs": dict( |
| 241 |
type="bool", |
| 242 |
required=False, |
| 243 |
default=True, |
| 244 |
fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]), |
| 245 |
), |
| 246 |
"vcenter_rest_log_file": dict( |
| 247 |
type="str", |
| 248 |
required=False, |
| 249 |
fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]), |
| 250 |
), |
| 251 |
"session_timeout": dict( |
| 252 |
type="float", |
| 253 |
required=False, |
| 254 |
fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]), |
| 255 |
), |
| 256 |
} |
| 257 |
|
| 258 |
argument_spec["description"] = {"type": "str"} |
| 259 |
argument_spec["encryption_key"] = {"no_log": True, "type": "str"} |
| 260 |
argument_spec["profiles"] = {"type": "list", "elements": "str"} |
| 261 |
argument_spec["state"] = {"required": True, "type": "str", "choices": ["export"]} |
| 262 |
|
| 263 |
return argument_spec |
| 264 |
|
| 265 |
|
| 266 |
async def main(): |
| 267 |
required_if = list([]) |
| 268 |
|
| 269 |
module_args = prepare_argument_spec() |
| 270 |
module = AnsibleModule( |
| 271 |
argument_spec=module_args, required_if=required_if, supports_check_mode=False |
| 272 |
) |
| 273 |
if not module.params["vcenter_hostname"]: |
| 274 |
module.fail_json("vcenter_hostname cannot be empty") |
| 275 |
if not module.params["vcenter_username"]: |
| 276 |
module.fail_json("vcenter_username cannot be empty") |
| 277 |
if not module.params["vcenter_password"]: |
| 278 |
module.fail_json("vcenter_password cannot be empty") |
| 279 |
try: |
| 280 |
session = await open_session( |
| 281 |
vcenter_hostname=module.params["vcenter_hostname"], |
| 282 |
vcenter_username=module.params["vcenter_username"], |
| 283 |
vcenter_password=module.params["vcenter_password"], |
| 284 |
validate_certs=module.params["vcenter_validate_certs"], |
| 285 |
log_file=module.params["vcenter_rest_log_file"], |
| 286 |
) |
| 287 |
except Exception as err: |
| 288 |
if hasattr(err, "get_message"): |
| 289 |
module.fail_json(err.get_message()) |
| 290 |
else: |
| 291 |
module.fail_json(str(err)) |
| 292 |
result = await entry_point(module, session) |
| 293 |
module.exit_json(**result) |
| 294 |
|
| 295 |
|
| 296 |
# template: default_module.j2 |
| 297 |
def build_url(params): |
| 298 |
return ("https://{vcenter_hostname}" "/api/appliance/infraprofile/configs").format( |
| 299 |
**params |
| 300 |
) |
| 301 |
|
| 302 |
|
| 303 |
async def entry_point(module, session): |
| 304 |
|
| 305 |
if module.params["state"] == "present": |
| 306 |
if "_create" in globals(): |
| 307 |
operation = "create" |
| 308 |
else: |
| 309 |
operation = "update" |
| 310 |
elif module.params["state"] == "absent": |
| 311 |
operation = "delete" |
| 312 |
else: |
| 313 |
operation = module.params["state"] |
| 314 |
|
| 315 |
func = globals()["_" + operation] |
| 316 |
|
| 317 |
return await func(module.params, session) |
| 318 |
|
| 319 |
|
| 320 |
async def _export(params, session): |
| 321 |
_in_query_parameters = PAYLOAD_FORMAT["export"]["query"].keys() |
| 322 |
payload = prepare_payload(params, PAYLOAD_FORMAT["export"]) |
| 323 |
subdevice_type = get_subdevice_type( |
| 324 |
"/api/appliance/infraprofile/configs?action=export" |
| 325 |
) |
| 326 |
if subdevice_type and not params[subdevice_type]: |
| 327 |
_json = await exists(params, session, build_url(params)) |
| 328 |
if _json: |
| 329 |
params[subdevice_type] = _json["id"] |
| 330 |
_url = ( |
| 331 |
"https://{vcenter_hostname}" |
| 332 |
# aa |
| 333 |
"/api/appliance/infraprofile/configs?action=export" |
| 334 |
).format(**params) + gen_args(params, _in_query_parameters) |
| 335 |
async with session.post(_url, json=payload, **session_timeout(params)) as resp: |
| 336 |
try: |
| 337 |
if resp.headers["Content-Type"] == "application/json": |
| 338 |
_json = await resp.json() |
| 339 |
except KeyError: |
| 340 |
_json = {} |
| 341 |
if "value" not in _json: # 7.0.2 |
| 342 |
_json = {"value": _json} |
| 343 |
|
| 344 |
return await update_changed_flag(_json, resp.status, "export") |
| 345 |
|
| 346 |
|
| 347 |
if __name__ == "__main__": |
| 348 |
import asyncio |
| 349 |
|
| 350 |
current_loop = asyncio.new_event_loop() |
| 351 |
try: |
| 352 |
asyncio.set_event_loop(current_loop) |
| 353 |
current_loop.run_until_complete(main()) |
| 354 |
finally: |
| 355 |
current_loop.close() |