| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2017, Davis Phillips [email protected] |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_resource_pool |
| 15 |
short_description: Add/remove resource pools to/from vCenter |
| 16 |
description: |
| 17 |
- This module can be used to add/remove a resource pool to/from vCenter |
| 18 |
author: |
| 19 |
- Davis Phillips (@dav1x) |
| 20 |
options: |
| 21 |
datacenter: |
| 22 |
description: |
| 23 |
- Name of the datacenter. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
cluster: |
| 27 |
description: |
| 28 |
- Name of the cluster to configure the resource pool. |
| 29 |
- This parameter is required if O(esxi_hostname) or O(parent_resource_pool) is not specified. |
| 30 |
- The O(cluster), O(esxi_hostname) and O(parent_resource_pool) parameters are mutually exclusive. |
| 31 |
type: str |
| 32 |
esxi_hostname: |
| 33 |
description: |
| 34 |
- Name of the host to configure the resource pool. |
| 35 |
- The host must not be member of a cluster. |
| 36 |
- This parameter is required if O(cluster) or O(parent_resource_pool) is not specified. |
| 37 |
- The O(cluster), O(esxi_hostname) and O(parent_resource_pool) parameters are mutually exclusive. |
| 38 |
type: str |
| 39 |
parent_resource_pool: |
| 40 |
description: |
| 41 |
- Name of the parent resource pool. |
| 42 |
- This parameter is required if O(cluster) or O(esxi_hostname) is not specified. |
| 43 |
- The O(cluster), O(esxi_hostname) and O(parent_resource_pool) parameters are mutually exclusive. |
| 44 |
type: str |
| 45 |
resource_pool: |
| 46 |
description: |
| 47 |
- Resource pool name to manage. |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
cpu_expandable_reservations: |
| 51 |
description: |
| 52 |
- In a resource pool with an expandable reservation, the reservation on a resource pool can grow beyond the specified value. |
| 53 |
default: true |
| 54 |
type: bool |
| 55 |
cpu_reservation: |
| 56 |
description: |
| 57 |
- Amount of resource that is guaranteed available to the virtual machine or resource pool. |
| 58 |
default: 0 |
| 59 |
type: int |
| 60 |
cpu_limit: |
| 61 |
description: |
| 62 |
- The utilization of a virtual machine/resource pool will not exceed this limit, even if there are available resources. |
| 63 |
- The default value -1 indicates no limit. |
| 64 |
default: -1 |
| 65 |
type: int |
| 66 |
cpu_shares: |
| 67 |
description: |
| 68 |
- Memory shares are used in case of resource contention. |
| 69 |
choices: |
| 70 |
- high |
| 71 |
- custom |
| 72 |
- low |
| 73 |
- normal |
| 74 |
default: normal |
| 75 |
type: str |
| 76 |
cpu_allocation_shares: |
| 77 |
description: |
| 78 |
- The number of cpu shares allocated. |
| 79 |
- This value is only set if O(cpu_shares=custom). |
| 80 |
type: int |
| 81 |
default: 4000 |
| 82 |
mem_expandable_reservations: |
| 83 |
description: |
| 84 |
- In a resource pool with an expandable reservation, the reservation on a resource pool can grow beyond the specified value. |
| 85 |
default: true |
| 86 |
type: bool |
| 87 |
mem_reservation: |
| 88 |
description: |
| 89 |
- Amount of resource that is guaranteed available to the virtual machine or resource pool. |
| 90 |
default: 0 |
| 91 |
type: int |
| 92 |
mem_limit: |
| 93 |
description: |
| 94 |
- The utilization of a virtual machine/resource pool will not exceed this limit, even if there are available resources. |
| 95 |
- The default value -1 indicates no limit. |
| 96 |
default: -1 |
| 97 |
type: int |
| 98 |
mem_shares: |
| 99 |
description: |
| 100 |
- Memory shares are used in case of resource contention. |
| 101 |
choices: |
| 102 |
- high |
| 103 |
- custom |
| 104 |
- low |
| 105 |
- normal |
| 106 |
default: normal |
| 107 |
type: str |
| 108 |
mem_allocation_shares: |
| 109 |
description: |
| 110 |
- The number of memory shares allocated. |
| 111 |
- This value is only set if O(mem_shares=custom). |
| 112 |
type: int |
| 113 |
default: 163840 |
| 114 |
state: |
| 115 |
description: |
| 116 |
- Add or remove the resource pool |
| 117 |
default: 'present' |
| 118 |
choices: |
| 119 |
- 'present' |
| 120 |
- 'absent' |
| 121 |
type: str |
| 122 |
extends_documentation_fragment: |
| 123 |
- vmware.vmware.base_options |
| 124 |
|
| 125 |
''' |
| 126 |
|
| 127 |
EXAMPLES = r''' |
| 128 |
- name: Add resource pool to vCenter |
| 129 |
community.vmware.vmware_resource_pool: |
| 130 |
hostname: '{{ vcenter_hostname }}' |
| 131 |
username: '{{ vcenter_username }}' |
| 132 |
password: '{{ vcenter_password }}' |
| 133 |
datacenter: '{{ datacenter_name }}' |
| 134 |
cluster: '{{ cluster_name }}' |
| 135 |
resource_pool: '{{ resource_pool_name }}' |
| 136 |
mem_shares: normal |
| 137 |
mem_limit: -1 |
| 138 |
mem_reservation: 0 |
| 139 |
mem_expandable_reservations: true |
| 140 |
cpu_shares: normal |
| 141 |
cpu_limit: -1 |
| 142 |
cpu_reservation: 0 |
| 143 |
cpu_expandable_reservations: true |
| 144 |
state: present |
| 145 |
delegate_to: localhost |
| 146 |
''' |
| 147 |
|
| 148 |
RETURN = r''' |
| 149 |
instance: |
| 150 |
description: metadata about the new resource pool |
| 151 |
returned: always |
| 152 |
type: dict |
| 153 |
sample: None |
| 154 |
resource_pool_config: |
| 155 |
description: config data about the resource pool, version added 1.4.0 |
| 156 |
returned: always |
| 157 |
type: dict |
| 158 |
sample: >- |
| 159 |
{ |
| 160 |
"_vimtype": "vim.ResourceConfigSpec", |
| 161 |
"changeVersion": null, |
| 162 |
"cpuAllocation": { |
| 163 |
"_vimtype": "vim.ResourceAllocationInfo", |
| 164 |
"expandableReservation": true, |
| 165 |
"limit": -1, |
| 166 |
"overheadLimit": null, |
| 167 |
"reservation": 0, |
| 168 |
"shares": { |
| 169 |
"_vimtype": "vim.SharesInfo", |
| 170 |
"level": "normal", |
| 171 |
"shares": 4000 |
| 172 |
} |
| 173 |
}, |
| 174 |
"entity": "vim.ResourcePool:resgroup-1108", |
| 175 |
"lastModified": null, |
| 176 |
"memoryAllocation": { |
| 177 |
"_vimtype": "vim.ResourceAllocationInfo", |
| 178 |
"expandableReservation": true, |
| 179 |
"limit": -1, |
| 180 |
"overheadLimit": null, |
| 181 |
"reservation": 0, |
| 182 |
"shares": { |
| 183 |
"_vimtype": "vim.SharesInfo", |
| 184 |
"level": "high", |
| 185 |
"shares": 327680 |
| 186 |
} |
| 187 |
}, |
| 188 |
"name": "test_pr1", |
| 189 |
"scaleDescendantsShares": null |
| 190 |
} |
| 191 |
''' |
| 192 |
|
| 193 |
try: |
| 194 |
from pyVmomi import vim, vmodl |
| 195 |
except ImportError: |
| 196 |
pass |
| 197 |
|
| 198 |
from ansible.module_utils.common.text.converters import to_native |
| 199 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import get_all_objs, find_datacenter_by_name, \ |
| 200 |
find_cluster_by_name, find_object_by_name, wait_for_task, find_resource_pool_by_name, PyVmomi |
| 201 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 202 |
from ansible.module_utils.basic import AnsibleModule |
| 203 |
|
| 204 |
|
| 205 |
class VMwareResourcePool(PyVmomi): |
| 206 |
|
| 207 |
def __init__(self, module): |
| 208 |
super(VMwareResourcePool, self).__init__(module) |
| 209 |
self.datacenter = module.params['datacenter'] |
| 210 |
self.resource_pool = module.params['resource_pool'] |
| 211 |
self.hostname = module.params['hostname'] |
| 212 |
self.username = module.params['username'] |
| 213 |
self.password = module.params['password'] |
| 214 |
self.state = module.params['state'] |
| 215 |
self.mem_shares = module.params['mem_shares'] |
| 216 |
self.mem_allocation_shares = module.params['mem_allocation_shares'] |
| 217 |
self.mem_limit = module.params['mem_limit'] |
| 218 |
self.mem_reservation = module.params['mem_reservation'] |
| 219 |
self.mem_expandable_reservations = module.params[ |
| 220 |
'mem_expandable_reservations'] |
| 221 |
self.cpu_shares = module.params['cpu_shares'] |
| 222 |
self.cpu_allocation_shares = module.params['cpu_allocation_shares'] |
| 223 |
self.cpu_limit = module.params['cpu_limit'] |
| 224 |
self.cpu_reservation = module.params['cpu_reservation'] |
| 225 |
self.cpu_expandable_reservations = module.params[ |
| 226 |
'cpu_expandable_reservations'] |
| 227 |
self.parent_resource_pool = module.params['parent_resource_pool'] |
| 228 |
self.resource_pool_obj = None |
| 229 |
|
| 230 |
self.dc_obj = find_datacenter_by_name(self.content, self.datacenter) |
| 231 |
if self.dc_obj is None: |
| 232 |
self.module.fail_json(msg="Unable to find datacenter with name %s" % self.datacenter) |
| 233 |
|
| 234 |
if module.params['cluster']: |
| 235 |
self.compute_resource_obj = find_cluster_by_name(self.content, module.params['cluster'], datacenter=self.dc_obj) |
| 236 |
if self.compute_resource_obj is None: |
| 237 |
self.module.fail_json(msg="Unable to find cluster with name %s" % module.params['cluster']) |
| 238 |
|
| 239 |
if module.params['esxi_hostname']: |
| 240 |
self.compute_resource_obj = find_object_by_name(self.content, module.params['esxi_hostname'], [vim.ComputeResource], folder=self.dc_obj.hostFolder) |
| 241 |
if self.compute_resource_obj is None: |
| 242 |
self.module.fail_json(msg="Unable to find host with name %s" % module.params['esxi_hostname']) |
| 243 |
|
| 244 |
if module.params['parent_resource_pool']: |
| 245 |
self.compute_resource_obj = find_resource_pool_by_name(self.content, module.params['parent_resource_pool']) |
| 246 |
if self.compute_resource_obj is None: |
| 247 |
self.module.fail_json(msg="Unable to find resource pool with name %s" % module.params['parent_resource_pool']) |
| 248 |
|
| 249 |
def select_resource_pool(self): |
| 250 |
pool_obj = None |
| 251 |
|
| 252 |
resource_pools = get_all_objs(self.content, [vim.ResourcePool], folder=self.compute_resource_obj) |
| 253 |
|
| 254 |
pool_selections = self.get_obj( |
| 255 |
[vim.ResourcePool], |
| 256 |
self.resource_pool, |
| 257 |
return_all=True |
| 258 |
) |
| 259 |
if pool_selections: |
| 260 |
for p in pool_selections: |
| 261 |
if p in resource_pools: |
| 262 |
pool_obj = p |
| 263 |
break |
| 264 |
|
| 265 |
return pool_obj |
| 266 |
|
| 267 |
def get_obj(self, vimtype, name, return_all=False): |
| 268 |
obj = list() |
| 269 |
container = self.content.viewManager.CreateContainerView( |
| 270 |
self.content.rootFolder, vimtype, True) |
| 271 |
|
| 272 |
for c in container.view: |
| 273 |
if name in [c.name, c._GetMoId()]: |
| 274 |
if return_all is False: |
| 275 |
return c |
| 276 |
else: |
| 277 |
obj.append(c) |
| 278 |
|
| 279 |
if len(obj) > 0: |
| 280 |
return obj |
| 281 |
else: |
| 282 |
# for backwards-compat |
| 283 |
return None |
| 284 |
|
| 285 |
def process_state(self): |
| 286 |
try: |
| 287 |
rp_states = { |
| 288 |
'absent': { |
| 289 |
'present': self.state_remove_rp, |
| 290 |
'absent': self.state_exit_unchanged, |
| 291 |
}, |
| 292 |
'present': { |
| 293 |
'present': self.state_update_existing_pr, |
| 294 |
'absent': self.state_add_rp, |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
rp_states[self.state][self.check_rp_state()]() |
| 299 |
|
| 300 |
except vmodl.RuntimeFault as runtime_fault: |
| 301 |
self.module.fail_json(msg=runtime_fault.msg) |
| 302 |
except vmodl.MethodFault as method_fault: |
| 303 |
self.module.fail_json(msg=method_fault.msg) |
| 304 |
except Exception as e: |
| 305 |
self.module.fail_json(msg=to_native(e)) |
| 306 |
|
| 307 |
def generate_rp_config(self): |
| 308 |
rp_spec = vim.ResourceConfigSpec() |
| 309 |
cpu_alloc = vim.ResourceAllocationInfo() |
| 310 |
cpu_alloc.expandableReservation = self.cpu_expandable_reservations |
| 311 |
cpu_alloc.limit = self.cpu_limit |
| 312 |
cpu_alloc.reservation = self.cpu_reservation |
| 313 |
cpu_alloc_shares = vim.SharesInfo() |
| 314 |
if self.cpu_shares == 'custom': |
| 315 |
cpu_alloc_shares.shares = self.cpu_allocation_shares |
| 316 |
cpu_alloc_shares.level = self.cpu_shares |
| 317 |
cpu_alloc.shares = cpu_alloc_shares |
| 318 |
rp_spec.cpuAllocation = cpu_alloc |
| 319 |
|
| 320 |
mem_alloc = vim.ResourceAllocationInfo() |
| 321 |
mem_alloc.limit = self.mem_limit |
| 322 |
mem_alloc.expandableReservation = self.mem_expandable_reservations |
| 323 |
mem_alloc.reservation = self.mem_reservation |
| 324 |
mem_alloc_shares = vim.SharesInfo() |
| 325 |
if self.mem_shares == 'custom': |
| 326 |
mem_alloc_shares.shares = self.mem_allocation_shares |
| 327 |
mem_alloc_shares.level = self.mem_shares |
| 328 |
mem_alloc.shares = mem_alloc_shares |
| 329 |
rp_spec.memoryAllocation = mem_alloc |
| 330 |
|
| 331 |
return rp_spec |
| 332 |
|
| 333 |
def generate_rp_config_return_value(self, include_rp_config=False): |
| 334 |
resource_config_return_value = {} |
| 335 |
if include_rp_config: |
| 336 |
resource_config_return_value = self.to_json(self.select_resource_pool().config) |
| 337 |
|
| 338 |
resource_config_return_value['name'] = self.resource_pool |
| 339 |
|
| 340 |
return resource_config_return_value |
| 341 |
|
| 342 |
def state_exit_unchanged(self): |
| 343 |
changed = False |
| 344 |
if self.module.check_mode: |
| 345 |
self.module.exit_json(changed=changed) |
| 346 |
|
| 347 |
self.module.exit_json(changed=changed, resource_pool_config=self.generate_rp_config_return_value()) |
| 348 |
|
| 349 |
def state_update_existing_pr(self): |
| 350 |
changed = False |
| 351 |
|
| 352 |
# check the difference between the existing config and the new config |
| 353 |
rp_spec = self.generate_rp_config() |
| 354 |
if self.mem_shares and self.mem_shares != self.resource_pool_obj.config.memoryAllocation.shares.level: |
| 355 |
changed = True |
| 356 |
rp_spec.memoryAllocation.shares.level = self.mem_shares |
| 357 |
|
| 358 |
if self.mem_allocation_shares and self.mem_shares == 'custom': |
| 359 |
if self.mem_allocation_shares != self.resource_pool_obj.config.memoryAllocation.shares.shares: |
| 360 |
changed = True |
| 361 |
rp_spec.memoryAllocation.shares.shares = self.mem_allocation_shares |
| 362 |
|
| 363 |
if self.mem_limit and self.mem_limit != self.resource_pool_obj.config.memoryAllocation.limit: |
| 364 |
changed = True |
| 365 |
rp_spec.memoryAllocation.limit = self.mem_limit |
| 366 |
|
| 367 |
if self.mem_reservation and self.mem_reservation != self.resource_pool_obj.config.memoryAllocation.reservation: |
| 368 |
changed = True |
| 369 |
rp_spec.memoryAllocation.reservation = self.mem_reservation |
| 370 |
|
| 371 |
if self.mem_expandable_reservations != self.resource_pool_obj.config.memoryAllocation.expandableReservation: |
| 372 |
changed = True |
| 373 |
rp_spec.memoryAllocation.expandableReservation = self.mem_expandable_reservations |
| 374 |
|
| 375 |
if self.cpu_shares and self.cpu_shares != self.resource_pool_obj.config.cpuAllocation.shares.level: |
| 376 |
changed = True |
| 377 |
rp_spec.cpuAllocation.shares.level = self.cpu_shares |
| 378 |
|
| 379 |
if self.cpu_allocation_shares and self.cpu_shares == 'custom': |
| 380 |
if self.cpu_allocation_shares != self.resource_pool_obj.config.cpuAllocation.shares.shares: |
| 381 |
changed = True |
| 382 |
rp_spec.cpuAllocation.shares.shares = self.cpu_allocation_shares |
| 383 |
|
| 384 |
if self.cpu_limit and self.cpu_limit != self.resource_pool_obj.config.cpuAllocation.limit: |
| 385 |
changed = True |
| 386 |
rp_spec.cpuAllocation.limit = self.cpu_limit |
| 387 |
|
| 388 |
if self.cpu_reservation and self.cpu_reservation != self.resource_pool_obj.config.cpuAllocation.reservation: |
| 389 |
changed = True |
| 390 |
rp_spec.cpuAllocation.reservation = self.cpu_reservation |
| 391 |
|
| 392 |
if self.cpu_expandable_reservations != self.resource_pool_obj.config.cpuAllocation.expandableReservation: |
| 393 |
changed = True |
| 394 |
rp_spec.cpuAllocation.expandableReservation = self.cpu_expandable_reservations |
| 395 |
|
| 396 |
if self.module.check_mode: |
| 397 |
self.module.exit_json(changed=changed) |
| 398 |
|
| 399 |
if changed: |
| 400 |
self.resource_pool_obj.UpdateConfig(self.resource_pool, rp_spec) |
| 401 |
|
| 402 |
resource_pool_config = self.generate_rp_config_return_value(True) |
| 403 |
self.module.exit_json(changed=changed, resource_pool_config=resource_pool_config) |
| 404 |
|
| 405 |
def state_remove_rp(self): |
| 406 |
changed = True |
| 407 |
result = None |
| 408 |
if self.module.check_mode: |
| 409 |
self.module.exit_json(changed=changed) |
| 410 |
|
| 411 |
resource_pool_config = self.generate_rp_config_return_value(True) |
| 412 |
try: |
| 413 |
task = self.resource_pool_obj.Destroy() |
| 414 |
success, result = wait_for_task(task) |
| 415 |
|
| 416 |
except Exception: |
| 417 |
self.module.fail_json(msg="Failed to remove resource pool '%s' '%s'" % ( |
| 418 |
self.resource_pool, self.resource_pool)) |
| 419 |
self.module.exit_json(changed=changed, resource_pool_config=resource_pool_config) |
| 420 |
|
| 421 |
def state_add_rp(self): |
| 422 |
changed = True |
| 423 |
if self.module.check_mode: |
| 424 |
self.module.exit_json(changed=changed) |
| 425 |
|
| 426 |
rp_spec = self.generate_rp_config() |
| 427 |
|
| 428 |
if self.parent_resource_pool: |
| 429 |
rootResourcePool = self.compute_resource_obj |
| 430 |
else: |
| 431 |
rootResourcePool = self.compute_resource_obj.resourcePool |
| 432 |
|
| 433 |
rootResourcePool.CreateResourcePool(self.resource_pool, rp_spec) |
| 434 |
|
| 435 |
resource_pool_config = self.generate_rp_config_return_value(True) |
| 436 |
self.module.exit_json(changed=changed, resource_pool_config=resource_pool_config) |
| 437 |
|
| 438 |
def check_rp_state(self): |
| 439 |
self.resource_pool_obj = self.select_resource_pool() |
| 440 |
if self.resource_pool_obj is None: |
| 441 |
return 'absent' |
| 442 |
|
| 443 |
return 'present' |
| 444 |
|
| 445 |
|
| 446 |
def main(): |
| 447 |
argument_spec = base_argument_spec() |
| 448 |
argument_spec.update(dict(datacenter=dict(required=True, type='str'), |
| 449 |
cluster=dict(type='str', required=False), |
| 450 |
esxi_hostname=dict(type='str', required=False), |
| 451 |
parent_resource_pool=dict(type='str', required=False), |
| 452 |
resource_pool=dict(required=True, type='str'), |
| 453 |
mem_shares=dict(type='str', default="normal", choices=[ |
| 454 |
'high', 'custom', 'normal', 'low']), |
| 455 |
mem_allocation_shares=dict(type='int', default=163840), |
| 456 |
mem_limit=dict(type='int', default=-1), |
| 457 |
mem_reservation=dict(type='int', default=0), |
| 458 |
mem_expandable_reservations=dict( |
| 459 |
type='bool', default="True"), |
| 460 |
cpu_shares=dict(type='str', default="normal", choices=[ |
| 461 |
'high', 'custom', 'normal', 'low']), |
| 462 |
cpu_allocation_shares=dict(type='int', default=4000), |
| 463 |
cpu_limit=dict(type='int', default=-1), |
| 464 |
cpu_reservation=dict(type='int', default=0), |
| 465 |
cpu_expandable_reservations=dict( |
| 466 |
type='bool', default="True"), |
| 467 |
state=dict(default='present', choices=['present', 'absent'], type='str'))) |
| 468 |
|
| 469 |
module = AnsibleModule(argument_spec=argument_spec, |
| 470 |
required_if=[ |
| 471 |
['mem_shares', 'custom', ['mem_allocation_shares']], |
| 472 |
['cpu_shares', 'custom', ['cpu_allocation_shares']] |
| 473 |
], |
| 474 |
required_one_of=[ |
| 475 |
['cluster', 'esxi_hostname', 'parent_resource_pool'], |
| 476 |
], |
| 477 |
mutually_exclusive=[ |
| 478 |
['cluster', 'esxi_hostname', 'parent_resource_pool'], |
| 479 |
], |
| 480 |
supports_check_mode=True) |
| 481 |
|
| 482 |
vmware_rp = VMwareResourcePool(module) |
| 483 |
vmware_rp.process_state() |
| 484 |
|
| 485 |
|
| 486 |
if __name__ == '__main__': |
| 487 |
main() |