| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Karsten Kaj Jakobsen <[email protected]> |
| 5 |
# Copyright: (c) 2020, Ansible Project |
| 6 |
# Copyright: (c) 2020, Abhijeet Kasurde <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: vmware_drs_group_manager |
| 17 |
author: |
| 18 |
- Abhijeet Kasurde (@Akasurde) |
| 19 |
short_description: Manage VMs and Hosts in DRS group. |
| 20 |
description: |
| 21 |
- The module can be used to add VMs / Hosts to or remove them from a DRS group. |
| 22 |
extends_documentation_fragment: |
| 23 |
- vmware.vmware.base_options |
| 24 |
options: |
| 25 |
cluster: |
| 26 |
description: |
| 27 |
- Cluster to which DRS group associated with. |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
aliases: |
| 31 |
- cluster_name |
| 32 |
datacenter: |
| 33 |
aliases: |
| 34 |
- datacenter_name |
| 35 |
description: |
| 36 |
- Name of the datacenter. |
| 37 |
required: false |
| 38 |
type: str |
| 39 |
group_name: |
| 40 |
description: |
| 41 |
- The name of the group to manage. |
| 42 |
required: true |
| 43 |
type: str |
| 44 |
hosts: |
| 45 |
description: |
| 46 |
- A List of hosts to add / remove in the group. |
| 47 |
- Required if O(vms) is not set. |
| 48 |
- O(hosts) and O(vms) are mutually exclusive parameters. |
| 49 |
required: false |
| 50 |
type: list |
| 51 |
elements: str |
| 52 |
vms: |
| 53 |
description: |
| 54 |
- A List of vms to add / remove in the group. |
| 55 |
- Required if O(hosts) is not set. |
| 56 |
- O(hosts) and O(vms) are mutually exclusive parameters. |
| 57 |
required: false |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
state: |
| 61 |
choices: [ present, absent ] |
| 62 |
default: present |
| 63 |
description: |
| 64 |
- If set to V(present), VMs/hosts will be added to the given DRS group. |
| 65 |
- If set to V(absent), VMs/hosts will be removed from the given DRS group. |
| 66 |
type: str |
| 67 |
''' |
| 68 |
|
| 69 |
EXAMPLES = r''' |
| 70 |
--- |
| 71 |
- name: Add VMs in an existing DRS VM group |
| 72 |
delegate_to: localhost |
| 73 |
community.vmware.vmware_drs_group_manager: |
| 74 |
hostname: "{{ vcenter_hostname }}" |
| 75 |
password: "{{ vcenter_password }}" |
| 76 |
username: "{{ vcenter_username }}" |
| 77 |
cluster: DC0_C0 |
| 78 |
datacenter: DC0 |
| 79 |
group_name: TEST_VM_01 |
| 80 |
vms: |
| 81 |
- DC0_C0_RP0_VM0 |
| 82 |
- DC0_C0_RP0_VM1 |
| 83 |
state: present |
| 84 |
|
| 85 |
- name: Add Hosts in an existing DRS Host group |
| 86 |
delegate_to: localhost |
| 87 |
community.vmware.vmware_drs_group_manager: |
| 88 |
hostname: "{{ vcenter_hostname }}" |
| 89 |
password: "{{ vcenter_password }}" |
| 90 |
username: "{{ vcenter_username }}" |
| 91 |
cluster: DC0_C0 |
| 92 |
datacenter: DC0 |
| 93 |
group_name: TEST_HOST_01 |
| 94 |
hosts: |
| 95 |
- DC0_C0_H0 |
| 96 |
- DC0_C0_H1 |
| 97 |
- DC0_C0_H2 |
| 98 |
state: present |
| 99 |
|
| 100 |
- name: Remove VM from an existing DRS VM group |
| 101 |
delegate_to: localhost |
| 102 |
community.vmware.vmware_drs_group_manager: |
| 103 |
hostname: "{{ vcenter_hostname }}" |
| 104 |
password: "{{ vcenter_password }}" |
| 105 |
username: "{{ vcenter_username }}" |
| 106 |
cluster: DC0_C0 |
| 107 |
datacenter: DC0 |
| 108 |
group_name: TEST_VM_01 |
| 109 |
vms: |
| 110 |
- DC0_C0_RP0_VM0 |
| 111 |
state: absent |
| 112 |
|
| 113 |
- name: Remove host from an existing DRS Host group |
| 114 |
delegate_to: localhost |
| 115 |
community.vmware.vmware_drs_group_manager: |
| 116 |
hostname: "{{ vcenter_hostname }}" |
| 117 |
password: "{{ vcenter_password }}" |
| 118 |
username: "{{ vcenter_username }}" |
| 119 |
cluster: DC0_C0 |
| 120 |
datacenter: DC0 |
| 121 |
group_name: TEST_HOST_01 |
| 122 |
hosts: |
| 123 |
- DC0_C0_H0 |
| 124 |
state: absent |
| 125 |
|
| 126 |
''' |
| 127 |
|
| 128 |
RETURN = r''' |
| 129 |
drs_group_member_info: |
| 130 |
description: Metadata about DRS group |
| 131 |
returned: always |
| 132 |
type: dict |
| 133 |
sample: { |
| 134 |
"Asia-Cluster1": [ |
| 135 |
{ |
| 136 |
"group_name": "vm_group_002", |
| 137 |
"type": "vm", |
| 138 |
"vms": [ |
| 139 |
"dev-1" |
| 140 |
] |
| 141 |
} |
| 142 |
] |
| 143 |
} |
| 144 |
msg: |
| 145 |
description: Info message |
| 146 |
returned: always |
| 147 |
type: str |
| 148 |
sample: "Updated host group TEST_HOST_01 successfully" |
| 149 |
''' |
| 150 |
|
| 151 |
try: |
| 152 |
from pyVmomi import vim |
| 153 |
except ImportError: |
| 154 |
pass |
| 155 |
|
| 156 |
from ansible.module_utils.basic import AnsibleModule |
| 157 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 158 |
PyVmomi, |
| 159 |
wait_for_task, |
| 160 |
find_vm_by_id) |
| 161 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 162 |
|
| 163 |
|
| 164 |
class VmwareDrsGroupMemberManager(PyVmomi): |
| 165 |
""" |
| 166 |
Class to manage DRS group members |
| 167 |
""" |
| 168 |
|
| 169 |
def __init__(self, module): |
| 170 |
""" |
| 171 |
Init |
| 172 |
""" |
| 173 |
|
| 174 |
super(VmwareDrsGroupMemberManager, self).__init__(module) |
| 175 |
|
| 176 |
self._datacenter_name = module.params.get('datacenter') |
| 177 |
self._datacenter_obj = None |
| 178 |
self._cluster_name = module.params.get('cluster') |
| 179 |
self._cluster_obj = None |
| 180 |
self._group_name = module.params.get('group_name') |
| 181 |
self._group_obj = None |
| 182 |
self._operation = None |
| 183 |
self._vm_list = module.params.get('vms') |
| 184 |
self._vm_obj_list = [] |
| 185 |
self._host_list = module.params.get('hosts') |
| 186 |
self._host_obj_list = [] |
| 187 |
self.message = 'Nothing to see here...' |
| 188 |
self.result = dict() |
| 189 |
self.changed = False |
| 190 |
self._state = module.params.get('state') |
| 191 |
|
| 192 |
if self._datacenter_name is not None: |
| 193 |
self._datacenter_obj = self.find_datacenter_by_name(self._datacenter_name) |
| 194 |
|
| 195 |
if self._datacenter_obj is None: |
| 196 |
self.module.fail_json(msg="Datacenter '%s' not found" % self._datacenter_name) |
| 197 |
|
| 198 |
self._cluster_obj = self.find_cluster_by_name(self._cluster_name, self._datacenter_obj) |
| 199 |
|
| 200 |
# Throw error if cluster does not exist |
| 201 |
if self._cluster_obj is None: |
| 202 |
self.module.fail_json(msg="Cluster '%s' not found" % self._cluster_name) |
| 203 |
|
| 204 |
# get group |
| 205 |
self._group_obj = self._get_group_by_name() |
| 206 |
if self._group_obj is None: |
| 207 |
self.module.fail_json(msg="Cluster %s does not have a DRS group %s" % (self._cluster_name, self._group_name)) |
| 208 |
|
| 209 |
# Set result here. If nothing is to be updated, result is already set |
| 210 |
self._set_result(self._group_obj) |
| 211 |
|
| 212 |
self._operation = 'edit' if self._state == 'present' else 'remove' |
| 213 |
|
| 214 |
if self._vm_list is not None: |
| 215 |
self._set_vm_obj_list(vm_list=self._vm_list) |
| 216 |
|
| 217 |
if self._host_list is not None: |
| 218 |
self._set_host_obj_list(host_list=self._host_list) |
| 219 |
|
| 220 |
def _set_result(self, group_obj): |
| 221 |
""" |
| 222 |
Creates result for successful run |
| 223 |
Args: |
| 224 |
group_obj: group object |
| 225 |
|
| 226 |
Returns: None |
| 227 |
|
| 228 |
""" |
| 229 |
self.result = dict() |
| 230 |
|
| 231 |
if self._cluster_obj is not None and group_obj is not None: |
| 232 |
self.result[self._cluster_obj.name] = [] |
| 233 |
self.result[self._cluster_obj.name].append(self._normalize_group_data(group_obj)) |
| 234 |
|
| 235 |
def _set_vm_obj_list(self, vm_list=None, cluster_obj=None): |
| 236 |
""" |
| 237 |
Populate vm object list from list of vms |
| 238 |
Args: |
| 239 |
vm_list: List of vm names |
| 240 |
|
| 241 |
Returns: None |
| 242 |
|
| 243 |
""" |
| 244 |
|
| 245 |
if vm_list is None: |
| 246 |
vm_list = self._vm_list |
| 247 |
|
| 248 |
if cluster_obj is None: |
| 249 |
cluster_obj = self._cluster_obj |
| 250 |
|
| 251 |
if vm_list is not None: |
| 252 |
for vm in vm_list: |
| 253 |
if self.module.check_mode is False: |
| 254 |
# Get host data |
| 255 |
vm_obj = find_vm_by_id(content=self.content, vm_id=vm, |
| 256 |
vm_id_type='vm_name', cluster=cluster_obj) |
| 257 |
if vm_obj is None: |
| 258 |
self.module.fail_json(msg="VM %s does not exist in cluster %s" % (vm, self._cluster_name)) |
| 259 |
self._vm_obj_list.append(vm_obj) |
| 260 |
|
| 261 |
def _set_host_obj_list(self, host_list=None): |
| 262 |
""" |
| 263 |
Populate host object list from list of hostnames |
| 264 |
Args: |
| 265 |
host_list: List of host names |
| 266 |
|
| 267 |
Returns: None |
| 268 |
|
| 269 |
""" |
| 270 |
|
| 271 |
if host_list is None: |
| 272 |
host_list = self._host_list |
| 273 |
|
| 274 |
if host_list is not None: |
| 275 |
for host in host_list: |
| 276 |
if self.module.check_mode is False: |
| 277 |
# Get host data |
| 278 |
host_obj = self.find_hostsystem_by_name(host) |
| 279 |
if host_obj is None and self.module.check_mode is False: |
| 280 |
self.module.fail_json(msg="ESXi host %s does not exist in cluster %s" % (host, self._cluster_name)) |
| 281 |
self._host_obj_list.append(host_obj) |
| 282 |
|
| 283 |
def _get_group_by_name(self, group_name=None, cluster_obj=None): |
| 284 |
""" |
| 285 |
Get group by name |
| 286 |
Args: |
| 287 |
group_name: Name of group |
| 288 |
cluster_obj: vim Cluster object |
| 289 |
|
| 290 |
Returns: Group Object if found or None |
| 291 |
|
| 292 |
""" |
| 293 |
|
| 294 |
if group_name is None: |
| 295 |
group_name = self._group_name |
| 296 |
|
| 297 |
if cluster_obj is None: |
| 298 |
cluster_obj = self._cluster_obj |
| 299 |
|
| 300 |
# Allow for group check even if dry run |
| 301 |
if self.module.check_mode and cluster_obj is None: |
| 302 |
return None |
| 303 |
|
| 304 |
for group in cluster_obj.configurationEx.group: |
| 305 |
if group.name == group_name: |
| 306 |
return group |
| 307 |
|
| 308 |
# No group found |
| 309 |
return None |
| 310 |
|
| 311 |
def _populate_vm_host_list(self, group_name=None, cluster_obj=None, host_group=False): |
| 312 |
""" |
| 313 |
Return all VMs/Hosts names using given group name |
| 314 |
Args: |
| 315 |
group_name: group name |
| 316 |
cluster_obj: Cluster managed object |
| 317 |
host_group: True if we want only host name from group |
| 318 |
|
| 319 |
Returns: List of VMs/Hosts names belonging to given group object |
| 320 |
|
| 321 |
""" |
| 322 |
obj_name_list = [] |
| 323 |
|
| 324 |
if group_name is None: |
| 325 |
group_name = self._group_name |
| 326 |
|
| 327 |
if cluster_obj is None: |
| 328 |
cluster_obj = self._cluster_obj |
| 329 |
|
| 330 |
if not all([group_name, cluster_obj]): |
| 331 |
return obj_name_list |
| 332 |
|
| 333 |
group = self._group_obj |
| 334 |
|
| 335 |
if not host_group and isinstance(group, vim.cluster.VmGroup): |
| 336 |
obj_name_list = [vm.name for vm in group.vm] |
| 337 |
|
| 338 |
elif host_group and isinstance(group, vim.cluster.HostGroup): |
| 339 |
obj_name_list = [host.name for host in group.host] |
| 340 |
|
| 341 |
return obj_name_list |
| 342 |
|
| 343 |
def _check_if_vms_hosts_changed(self, group_name=None, cluster_obj=None, host_group=False): |
| 344 |
""" |
| 345 |
Check if VMs/Hosts changed |
| 346 |
Args: |
| 347 |
group_name: Name of group |
| 348 |
cluster_obj: vim Cluster object |
| 349 |
host_group: True if we want to check hosts, else check vms |
| 350 |
|
| 351 |
Returns: Bool |
| 352 |
|
| 353 |
""" |
| 354 |
|
| 355 |
if group_name is None: |
| 356 |
group_name = self._group_name |
| 357 |
|
| 358 |
if cluster_obj is None: |
| 359 |
cluster_obj = self._cluster_obj |
| 360 |
|
| 361 |
list_a = self._host_list if host_group else self._vm_list |
| 362 |
list_b = self._populate_vm_host_list(host_group=host_group) |
| 363 |
|
| 364 |
# By casting lists as a set, you remove duplicates and order doesn't count. Comparing sets is also much faster and more efficient than comparing lists. |
| 365 |
if set(list_a) == set(list_b): |
| 366 |
if self._operation != 'remove': |
| 367 |
return False |
| 368 |
return True |
| 369 |
|
| 370 |
def _manage_host_group(self): |
| 371 |
# Check if anything has changed when editing |
| 372 |
if self._check_if_vms_hosts_changed(host_group=True): |
| 373 |
|
| 374 |
need_reconfigure = False |
| 375 |
group = vim.cluster.HostGroup() |
| 376 |
group.name = self._group_name |
| 377 |
group.host = self._group_obj.host or [] |
| 378 |
|
| 379 |
# Modify existing hosts |
| 380 |
for host in self._host_obj_list: |
| 381 |
if self._operation == 'edit' and host not in group.host: |
| 382 |
group.host.append(host) |
| 383 |
need_reconfigure = True |
| 384 |
if self._operation == 'remove' and host in group.host: |
| 385 |
group.host.remove(host) |
| 386 |
need_reconfigure = True |
| 387 |
|
| 388 |
group_spec = vim.cluster.GroupSpec(info=group, operation='edit') |
| 389 |
config_spec = vim.cluster.ConfigSpecEx(groupSpec=[group_spec]) |
| 390 |
|
| 391 |
if not self.module.check_mode and need_reconfigure: |
| 392 |
task = self._cluster_obj.ReconfigureEx(config_spec, modify=True) |
| 393 |
self.changed, dummy = wait_for_task(task) |
| 394 |
|
| 395 |
# Set new result since something changed |
| 396 |
self._set_result(group) |
| 397 |
if self.changed: |
| 398 |
self.message = "Updated host group %s successfully" % self._group_name |
| 399 |
else: |
| 400 |
self.message = "No update to host group %s" % self._group_name |
| 401 |
else: |
| 402 |
self.changed = False |
| 403 |
self.message = "No update to host group %s" % self._group_name |
| 404 |
|
| 405 |
def _manage_vm_group(self): |
| 406 |
|
| 407 |
# Check if anything has changed when editing |
| 408 |
if self._check_if_vms_hosts_changed(): |
| 409 |
need_reconfigure = False |
| 410 |
group = vim.cluster.VmGroup() |
| 411 |
group.name = self._group_name |
| 412 |
group.vm = self._group_obj.vm or [] |
| 413 |
|
| 414 |
# Modify existing VMs |
| 415 |
for vm in self._vm_obj_list: |
| 416 |
if self._operation == 'edit' and vm not in group.vm: |
| 417 |
group.vm.append(vm) |
| 418 |
need_reconfigure = True |
| 419 |
if self._operation == 'remove' and vm in group.vm: |
| 420 |
group.vm.remove(vm) |
| 421 |
need_reconfigure = True |
| 422 |
|
| 423 |
group_spec = vim.cluster.GroupSpec(info=group, operation='edit') |
| 424 |
config_spec = vim.cluster.ConfigSpecEx(groupSpec=[group_spec]) |
| 425 |
|
| 426 |
# Check if dry run |
| 427 |
if not self.module.check_mode and need_reconfigure: |
| 428 |
task = self._cluster_obj.ReconfigureEx(config_spec, modify=True) |
| 429 |
self.changed, dummy = wait_for_task(task) |
| 430 |
|
| 431 |
self._set_result(group) |
| 432 |
if self.changed: |
| 433 |
self.message = "Updated vm group %s successfully" % self._group_name |
| 434 |
else: |
| 435 |
self.message = "No update to vm group %s" % self._group_name |
| 436 |
else: |
| 437 |
self.changed = False |
| 438 |
self.message = "No update to vm group %s" % self._group_name |
| 439 |
|
| 440 |
def _normalize_group_data(self, group_obj): |
| 441 |
""" |
| 442 |
Return human readable group spec |
| 443 |
Args: |
| 444 |
group_obj: Group object |
| 445 |
|
| 446 |
Returns: DRS group object fact |
| 447 |
|
| 448 |
""" |
| 449 |
if not all([group_obj]): |
| 450 |
return {} |
| 451 |
|
| 452 |
# Check if group is a host group |
| 453 |
if hasattr(group_obj, 'host'): |
| 454 |
return dict( |
| 455 |
group_name=group_obj.name, |
| 456 |
hosts=self._host_list, |
| 457 |
type="host" |
| 458 |
) |
| 459 |
return dict( |
| 460 |
group_name=group_obj.name, |
| 461 |
vms=self._vm_list, |
| 462 |
type="vm" |
| 463 |
) |
| 464 |
|
| 465 |
def manage_drs_group_members(self): |
| 466 |
""" |
| 467 |
Add a DRS host/vm group members |
| 468 |
""" |
| 469 |
|
| 470 |
if self._vm_list is None: |
| 471 |
self._manage_host_group() |
| 472 |
elif self._host_list is None: |
| 473 |
self._manage_vm_group() |
| 474 |
else: |
| 475 |
self.module.fail_json(msg="Failed, no hosts or vms defined") |
| 476 |
|
| 477 |
|
| 478 |
def main(): |
| 479 |
""" |
| 480 |
Main function |
| 481 |
""" |
| 482 |
|
| 483 |
argument_spec = base_argument_spec() |
| 484 |
argument_spec.update( |
| 485 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 486 |
datacenter=dict(type='str', required=False, aliases=['datacenter_name']), |
| 487 |
cluster=dict(type='str', required=True, aliases=['cluster_name']), |
| 488 |
group_name=dict(type='str', required=True), |
| 489 |
vms=dict(type='list', elements='str'), |
| 490 |
hosts=dict(type='list', elements='str') |
| 491 |
) |
| 492 |
|
| 493 |
module = AnsibleModule( |
| 494 |
argument_spec=argument_spec, |
| 495 |
supports_check_mode=True, |
| 496 |
mutually_exclusive=[['vms', 'hosts']], |
| 497 |
required_one_of=[['vms', 'hosts']] |
| 498 |
) |
| 499 |
|
| 500 |
try: |
| 501 |
# Create instance of VmwareDrsGroupMemberManager |
| 502 |
vmware_drs_group = VmwareDrsGroupMemberManager(module=module) |
| 503 |
vmware_drs_group.manage_drs_group_members() |
| 504 |
|
| 505 |
# Set results |
| 506 |
results = dict(msg=vmware_drs_group.message, |
| 507 |
failed=False, |
| 508 |
changed=vmware_drs_group.changed, |
| 509 |
drs_group_member_info=vmware_drs_group.result) |
| 510 |
|
| 511 |
except Exception as error: |
| 512 |
results = dict(failed=True, msg="Error: %s" % error) |
| 513 |
|
| 514 |
if results['failed']: |
| 515 |
module.fail_json(**results) |
| 516 |
module.exit_json(**results) |
| 517 |
|
| 518 |
|
| 519 |
if __name__ == "__main__": |
| 520 |
main() |