| 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_hardware_cpu |
| 12 |
short_description: Updates the CPU-related settings of a virtual machine. |
| 13 |
description: Updates the CPU-related settings of a virtual machine. |
| 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.vm) instead. |
| 18 |
options: |
| 19 |
cores_per_socket: |
| 20 |
description: |
| 21 |
- New number of CPU cores per socket. The number of CPU cores in the virtual |
| 22 |
machine must be a multiple of the number of cores per socket. |
| 23 |
- If unset, the value is unchanged. |
| 24 |
type: int |
| 25 |
count: |
| 26 |
description: |
| 27 |
- 'New number of CPU cores. The number of CPU cores in the virtual machine |
| 28 |
must be a multiple of the number of cores per socket. ' |
| 29 |
- ' The supported range of CPU counts is constrained by the configured guest |
| 30 |
operating system and virtual hardware version of the virtual machine. ' |
| 31 |
- '' |
| 32 |
- ' If the virtual machine is running, the number of CPU cores may only be |
| 33 |
increased if I(hot_add_enabled) is true, and may only be decreased if |
| 34 |
I(hot_remove_enabled) is true.' |
| 35 |
- '' |
| 36 |
- If unset, the value is unchanged. |
| 37 |
type: int |
| 38 |
hot_add_enabled: |
| 39 |
description: |
| 40 |
- 'Flag indicating whether adding CPUs while the virtual machine is running |
| 41 |
is enabled. ' |
| 42 |
- ' This field may only be modified if the virtual machine is powered off.' |
| 43 |
- '' |
| 44 |
- If unset, the value is unchanged. |
| 45 |
type: bool |
| 46 |
hot_remove_enabled: |
| 47 |
description: |
| 48 |
- 'Flag indicating whether removing CPUs while the virtual machine is running |
| 49 |
is enabled. ' |
| 50 |
- ' This field may only be modified if the virtual machine is powered off.' |
| 51 |
- '' |
| 52 |
- If unset, the value is unchanged. |
| 53 |
type: bool |
| 54 |
session_timeout: |
| 55 |
description: |
| 56 |
- 'Timeout settings for client session. ' |
| 57 |
- 'The maximal number of seconds for the whole operation including connection |
| 58 |
establishment, request sending and response. ' |
| 59 |
- The default value is 300s. |
| 60 |
type: float |
| 61 |
version_added: 2.1.0 |
| 62 |
state: |
| 63 |
choices: |
| 64 |
- present |
| 65 |
default: present |
| 66 |
description: [] |
| 67 |
type: str |
| 68 |
vcenter_hostname: |
| 69 |
description: |
| 70 |
- The hostname or IP address of the vSphere vCenter |
| 71 |
- If the value is not specified in the task, the value of environment variable |
| 72 |
C(VMWARE_HOST) will be used instead. |
| 73 |
required: true |
| 74 |
type: str |
| 75 |
vcenter_password: |
| 76 |
description: |
| 77 |
- The vSphere vCenter password |
| 78 |
- If the value is not specified in the task, the value of environment variable |
| 79 |
C(VMWARE_PASSWORD) will be used instead. |
| 80 |
required: true |
| 81 |
type: str |
| 82 |
vcenter_rest_log_file: |
| 83 |
description: |
| 84 |
- 'You can use this optional parameter to set the location of a log file. ' |
| 85 |
- 'This file will be used to record the HTTP REST interaction. ' |
| 86 |
- 'The file will be stored on the host that runs the module. ' |
| 87 |
- 'If the value is not specified in the task, the value of ' |
| 88 |
- environment variable C(VMWARE_REST_LOG_FILE) will be used instead. |
| 89 |
type: str |
| 90 |
vcenter_username: |
| 91 |
description: |
| 92 |
- The vSphere vCenter username |
| 93 |
- If the value is not specified in the task, the value of environment variable |
| 94 |
C(VMWARE_USER) will be used instead. |
| 95 |
required: true |
| 96 |
type: str |
| 97 |
vcenter_validate_certs: |
| 98 |
default: true |
| 99 |
description: |
| 100 |
- Allows connection when SSL certificates are not valid. Set to C(false) when |
| 101 |
certificates are not trusted. |
| 102 |
- If the value is not specified in the task, the value of environment variable |
| 103 |
C(VMWARE_VALIDATE_CERTS) will be used instead. |
| 104 |
type: bool |
| 105 |
vm: |
| 106 |
description: |
| 107 |
- Virtual machine identifier. |
| 108 |
- The parameter must be the id of a resource returned by M(vmware.vmware_rest.vcenter_vm_info). |
| 109 |
This parameter is mandatory. |
| 110 |
required: true |
| 111 |
type: str |
| 112 |
author: |
| 113 |
- Ansible Cloud Team (@ansible-collections) |
| 114 |
version_added: 0.1.0 |
| 115 |
requirements: |
| 116 |
- vSphere 7.0.3 or greater |
| 117 |
- python >= 3.6 |
| 118 |
- aiohttp |
| 119 |
notes: |
| 120 |
- Tested on vSphere 7.0.3 |
| 121 |
""" |
| 122 |
|
| 123 |
EXAMPLES = r""" |
| 124 |
- name: Look up the VM called test_vm1 in the inventory |
| 125 |
register: search_result |
| 126 |
vmware.vmware_rest.vcenter_vm_info: |
| 127 |
filter_names: |
| 128 |
- test_vm1 |
| 129 |
|
| 130 |
- name: Collect information about a specific VM |
| 131 |
vmware.vmware_rest.vcenter_vm_info: |
| 132 |
vm: '{{ search_result.value[0].vm }}' |
| 133 |
register: test_vm1_info |
| 134 |
|
| 135 |
- name: Dedicate one core to the VM |
| 136 |
vmware.vmware_rest.vcenter_vm_hardware_cpu: |
| 137 |
vm: '{{ test_vm1_info.id }}' |
| 138 |
count: 1 |
| 139 |
register: _result |
| 140 |
""" |
| 141 |
RETURN = r""" |
| 142 |
# content generated by the update_return_section callback# task: Dedicate one core to the VM |
| 143 |
id: |
| 144 |
description: moid of the resource |
| 145 |
returned: On success |
| 146 |
sample: null |
| 147 |
type: dict |
| 148 |
value: |
| 149 |
description: Dedicate one core to the VM |
| 150 |
returned: On success |
| 151 |
sample: |
| 152 |
cores_per_socket: 1 |
| 153 |
count: 1 |
| 154 |
hot_add_enabled: 0 |
| 155 |
hot_remove_enabled: 0 |
| 156 |
type: dict |
| 157 |
""" |
| 158 |
|
| 159 |
|
| 160 |
# This structure describes the format of the data expected by the end-points |
| 161 |
PAYLOAD_FORMAT = { |
| 162 |
"update": { |
| 163 |
"query": {}, |
| 164 |
"body": { |
| 165 |
"cores_per_socket": "cores_per_socket", |
| 166 |
"count": "count", |
| 167 |
"hot_add_enabled": "hot_add_enabled", |
| 168 |
"hot_remove_enabled": "hot_remove_enabled", |
| 169 |
}, |
| 170 |
"path": {"vm": "vm"}, |
| 171 |
} |
| 172 |
} # pylint: disable=line-too-long |
| 173 |
|
| 174 |
from ansible.module_utils.basic import env_fallback |
| 175 |
import os |
| 176 |
|
| 177 |
if os.getenv("VMWARE_ENABLE_TURBO", False): |
| 178 |
try: |
| 179 |
from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( |
| 180 |
AnsibleTurboModule as AnsibleModule, |
| 181 |
) |
| 182 |
|
| 183 |
AnsibleModule.collection_name = "vmware.vmware_rest" |
| 184 |
except ImportError: |
| 185 |
from ansible.module_utils.basic import AnsibleModule |
| 186 |
else: |
| 187 |
from ansible.module_utils.basic import AnsibleModule |
| 188 |
|
| 189 |
from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import ( |
| 190 |
open_session, |
| 191 |
prepare_payload, |
| 192 |
session_timeout, |
| 193 |
update_changed_flag, |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
def prepare_argument_spec(): |
| 198 |
argument_spec = { |
| 199 |
"vcenter_hostname": dict( |
| 200 |
type="str", |
| 201 |
required=True, |
| 202 |
fallback=(env_fallback, ["VMWARE_HOST"]), |
| 203 |
), |
| 204 |
"vcenter_username": dict( |
| 205 |
type="str", |
| 206 |
required=True, |
| 207 |
fallback=(env_fallback, ["VMWARE_USER"]), |
| 208 |
), |
| 209 |
"vcenter_password": dict( |
| 210 |
type="str", |
| 211 |
required=True, |
| 212 |
no_log=True, |
| 213 |
fallback=(env_fallback, ["VMWARE_PASSWORD"]), |
| 214 |
), |
| 215 |
"vcenter_validate_certs": dict( |
| 216 |
type="bool", |
| 217 |
required=False, |
| 218 |
default=True, |
| 219 |
fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]), |
| 220 |
), |
| 221 |
"vcenter_rest_log_file": dict( |
| 222 |
type="str", |
| 223 |
required=False, |
| 224 |
fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]), |
| 225 |
), |
| 226 |
"session_timeout": dict( |
| 227 |
type="float", |
| 228 |
required=False, |
| 229 |
fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]), |
| 230 |
), |
| 231 |
} |
| 232 |
|
| 233 |
argument_spec["cores_per_socket"] = {"type": "int"} |
| 234 |
argument_spec["count"] = {"type": "int"} |
| 235 |
argument_spec["hot_add_enabled"] = {"type": "bool"} |
| 236 |
argument_spec["hot_remove_enabled"] = {"type": "bool"} |
| 237 |
argument_spec["state"] = { |
| 238 |
"type": "str", |
| 239 |
"choices": ["present"], |
| 240 |
"default": "present", |
| 241 |
} |
| 242 |
argument_spec["vm"] = {"required": True, "type": "str"} |
| 243 |
|
| 244 |
return argument_spec |
| 245 |
|
| 246 |
|
| 247 |
async def main(): |
| 248 |
required_if = list([]) |
| 249 |
|
| 250 |
module_args = prepare_argument_spec() |
| 251 |
module = AnsibleModule( |
| 252 |
argument_spec=module_args, required_if=required_if, supports_check_mode=False |
| 253 |
) |
| 254 |
if not module.params["vcenter_hostname"]: |
| 255 |
module.fail_json("vcenter_hostname cannot be empty") |
| 256 |
if not module.params["vcenter_username"]: |
| 257 |
module.fail_json("vcenter_username cannot be empty") |
| 258 |
if not module.params["vcenter_password"]: |
| 259 |
module.fail_json("vcenter_password cannot be empty") |
| 260 |
try: |
| 261 |
session = await open_session( |
| 262 |
vcenter_hostname=module.params["vcenter_hostname"], |
| 263 |
vcenter_username=module.params["vcenter_username"], |
| 264 |
vcenter_password=module.params["vcenter_password"], |
| 265 |
validate_certs=module.params["vcenter_validate_certs"], |
| 266 |
log_file=module.params["vcenter_rest_log_file"], |
| 267 |
) |
| 268 |
except Exception as err: |
| 269 |
if hasattr(err, "get_message"): |
| 270 |
module.fail_json(err.get_message()) |
| 271 |
else: |
| 272 |
module.fail_json(str(err)) |
| 273 |
result = await entry_point(module, session) |
| 274 |
module.exit_json(**result) |
| 275 |
|
| 276 |
|
| 277 |
# template: default_module.j2 |
| 278 |
def build_url(params): |
| 279 |
return ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/hardware/cpu").format( |
| 280 |
**params |
| 281 |
) |
| 282 |
|
| 283 |
|
| 284 |
async def entry_point(module, session): |
| 285 |
|
| 286 |
if module.params["state"] == "present": |
| 287 |
if "_create" in globals(): |
| 288 |
operation = "create" |
| 289 |
else: |
| 290 |
operation = "update" |
| 291 |
elif module.params["state"] == "absent": |
| 292 |
operation = "delete" |
| 293 |
else: |
| 294 |
operation = module.params["state"] |
| 295 |
|
| 296 |
func = globals()["_" + operation] |
| 297 |
|
| 298 |
return await func(module.params, session) |
| 299 |
|
| 300 |
|
| 301 |
async def _update(params, session): |
| 302 |
payload = prepare_payload(params, PAYLOAD_FORMAT["update"]) |
| 303 |
_url = ("https://{vcenter_hostname}" "/api/vcenter/vm/{vm}/hardware/cpu").format( |
| 304 |
**params |
| 305 |
) |
| 306 |
async with session.get(_url, **session_timeout(params)) as resp: |
| 307 |
_json = await resp.json() |
| 308 |
if "value" in _json: |
| 309 |
value = _json["value"] |
| 310 |
else: # 7.0.2 and greater |
| 311 |
value = _json |
| 312 |
for k, v in value.items(): |
| 313 |
if k in payload: |
| 314 |
if isinstance(payload[k], dict) and isinstance(v, dict): |
| 315 |
to_delete = True |
| 316 |
for _k in list(payload[k].keys()): |
| 317 |
if payload[k][_k] != v.get(_k): |
| 318 |
to_delete = False |
| 319 |
if to_delete: |
| 320 |
del payload[k] |
| 321 |
elif payload[k] == v: |
| 322 |
del payload[k] |
| 323 |
elif payload[k] == {}: |
| 324 |
del payload[k] |
| 325 |
|
| 326 |
if payload == {} or payload == {"spec": {}}: |
| 327 |
# Nothing has changed |
| 328 |
if "value" not in _json: # 7.0.2 |
| 329 |
_json = {"value": _json} |
| 330 |
_json["id"] = params.get("None") |
| 331 |
return await update_changed_flag(_json, resp.status, "get") |
| 332 |
async with session.patch(_url, json=payload, **session_timeout(params)) as resp: |
| 333 |
try: |
| 334 |
if resp.headers["Content-Type"] == "application/json": |
| 335 |
_json = await resp.json() |
| 336 |
except KeyError: |
| 337 |
_json = {} |
| 338 |
if "value" not in _json: # 7.0.2 |
| 339 |
_json = {"value": _json} |
| 340 |
|
| 341 |
# e.g: content_configuration |
| 342 |
if not _json and resp.status == 204: |
| 343 |
async with session.get(_url, **session_timeout(params)) as resp_get: |
| 344 |
_json_get = await resp_get.json() |
| 345 |
if _json_get: |
| 346 |
_json = _json_get |
| 347 |
|
| 348 |
_json["id"] = params.get("None") |
| 349 |
return await update_changed_flag(_json, resp.status, "update") |
| 350 |
|
| 351 |
|
| 352 |
if __name__ == "__main__": |
| 353 |
import asyncio |
| 354 |
|
| 355 |
current_loop = asyncio.new_event_loop() |
| 356 |
try: |
| 357 |
asyncio.set_event_loop(current_loop) |
| 358 |
current_loop.run_until_complete(main()) |
| 359 |
finally: |
| 360 |
current_loop.close() |