Skip to content

Commit

Permalink
vmware_drs_group: Manage existing members in DRS group
Browse files Browse the repository at this point in the history
Added new parameter 'operation' to manage existing members
in the existing DRS group. Users can now add, set, and remove
existing members in DRS Group

fixes: ansible-collections#81

Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde committed Nov 3, 2020
1 parent 2bd6032 commit 0751f2c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 30 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/81_vmware_drs_group.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- vmware_drs_group - add facility to manage existing members of VM/Host group (https://github.com/ansible-collections/community.vmware/issues/81).
112 changes: 82 additions & 30 deletions plugins/modules/vmware_drs_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@
required: false
type: list
elements: str
operation:
description:
- Performs member management operation such as I(add), I(set), and I(remove).
- If VM group exists and set to I(add), adds specified VMs into the existing VM group.
- If host group exists and set to I(add), adds specified hosts into the existing host group.
- If VM group exists and set to I(set), only sets specified VMs into the existing VM group. This is default behavior.
- If host group exists and set to I(add), only sets specified hosts into the existing host group. This is default behavior.
- If VM group exists and set to I(remove), removes specified VMs from the existing VM group.
- If host group exists and set to I(remove), removes specified hosts from the existing host group.
type: str
choices: [ 'add', 'set', 'remove' ]
default: 'set'
version_added: '1.4.0'
requirements:
- "python >= 2.6"
- PyVmomi
Expand Down Expand Up @@ -110,6 +123,32 @@
group_name: TEST_HOST_01
state: absent
- name: Add VMs into the existing VM group
community.vmware.vmware_drs_group:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
cluster_name: "Asia-Cluster1"
datacenter_name: "Asia-Datacenter1"
group_name: vm_group_002
vms:
- xd
- centos_7
operation: add
- name: Add a host into the existing host group
community.vmware.vmware_drs_group:
validate_certs: False
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
cluster_name: "Asia-Cluster1"
datacenter_name: "Asia-Datacenter1"
group_name: host_group_002
vms:
- esxi10
operation: add
'''

RETURN = r'''
Expand Down Expand Up @@ -195,15 +234,15 @@ def __init__(self, module, cluster_name, group_name, state,

# Throw error if cluster does not exist
if self.__cluster_obj is None:
if module.check_mode is False:
if not module.check_mode:
raise Exception("Cluster '%s' not found" % self.__cluster_name)
else:
# get group
self.__group_obj = self.__get_group_by_name()
# Set result here. If nothing is to be updated, result is already set
self.__set_result(self.__group_obj)

# Dont populate lists if we are deleting group
# Do not populate lists if we are deleting group
if state == 'present':

if self.__group_obj:
Expand Down Expand Up @@ -278,19 +317,14 @@ def __set_vm_obj_list(self, vm_list=None, cluster_obj=None):
cluster_obj = self.__cluster_obj

if vm_list is not None:

for vm in vm_list:

if self.module.check_mode is False:

if not self.module.check_mode:
# Get host data
vm_obj = find_vm_by_id(content=self.content, vm_id=vm,
vm_id_type='vm_name', cluster=cluster_obj)

if vm_obj is None:
raise Exception("VM %s does not exist in cluster %s" % (vm,
self.__cluster_name))

self.__vm_obj_list.append(vm_obj)

def __set_host_obj_list(self, host_list=None):
Expand All @@ -307,17 +341,12 @@ def __set_host_obj_list(self, host_list=None):
host_list = self.__host_list

if host_list is not None:

for host in host_list:

if self.module.check_mode is False:

# Get host data
host_obj = self.find_hostsystem_by_name(host)

if host_obj is None and self.module.check_mode is False:
if host_obj is None:
raise Exception("ESXi host %s does not exist in cluster %s" % (host, self.__cluster_name))

self.__host_obj_list.append(host_obj)

def __get_group_by_name(self, group_name=None, cluster_obj=None):
Expand Down Expand Up @@ -402,10 +431,11 @@ def __check_if_vms_hosts_changed(self, group_name=None, cluster_obj=None, host_g
list_b = self.__populate_vm_host_list(host_group=host_group)

# 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.
operation = self.module.params.get('operation')
if set(list_a) == set(list_b):
return False
else:
return True
if operation != 'remove':
return False
return True

def __create_host_group(self):

Expand All @@ -414,18 +444,29 @@ def __create_host_group(self):

group = vim.cluster.HostGroup()
group.name = self.__group_name
group.host = self.__host_obj_list
operation = self.module.params.get('operation')
if operation == 'add':
group.host = self.__group_obj.host if self.__operation == 'edit' else []
group.host.extend(self.__host_obj_list)
elif operation == 'set':
group.host = self.__host_obj_list
elif operation == 'remove':
existing_host_list = self.__group_obj.host if self.__operation == 'edit' else []
for host in self.__host_obj_list:
existing_host_list.remove(host)
group.host = existing_host_list

group_spec = vim.cluster.GroupSpec(info=group, operation=self.__operation)
config_spec = vim.cluster.ConfigSpecEx(groupSpec=[group_spec])

changed = True
if not self.module.check_mode:
task = self.__cluster_obj.ReconfigureEx(config_spec, modify=True)
wait_for_task(task)
changed, result = wait_for_task(task)

# Set new result since something changed
self.__set_result(group)
self.__changed = True
self.__changed = changed

if self.__operation == 'edit':
self.__msg = "Updated host group %s successfully" % (self.__group_name)
Expand All @@ -440,18 +481,29 @@ def __create_vm_group(self):
group = vim.cluster.VmGroup()

group.name = self.__group_name
group.vm = self.__vm_obj_list
operation = self.module.params.get('operation')
if operation == 'add':
group.vm = self.__group_obj.vm if self.__operation == 'edit' else []
group.vm.extend(self.__vm_obj_list)
elif operation == 'set':
group.vm = self.__vm_obj_list
elif operation == 'remove':
existing_vm_list = self.__group_obj.vm if self.__operation == 'edit' else []
for vm in self.__vm_obj_list:
existing_vm_list.remove(vm)
group.vm = existing_vm_list

group_spec = vim.cluster.GroupSpec(info=group, operation=self.__operation)
config_spec = vim.cluster.ConfigSpecEx(groupSpec=[group_spec])

# Check if dry run
changed = True
if not self.module.check_mode:
task = self.__cluster_obj.ReconfigureEx(config_spec, modify=True)
wait_for_task(task)
changed, result = wait_for_task(task)

self.__set_result(group)
self.__changed = True
self.__changed = changed

if self.__operation == 'edit':
self.__msg = "Updated vm group %s successfully" % (self.__group_name)
Expand All @@ -477,12 +529,11 @@ def __normalize_group_data(self, group_obj):
hosts=self.__host_list,
type="host"
)
else:
return dict(
group_name=group_obj.name,
vms=self.__vm_list,
type="vm"
)
return dict(
group_name=group_obj.name,
vms=self.__vm_list,
type="vm"
)

def create_drs_group(self):
"""
Expand Down Expand Up @@ -534,7 +585,8 @@ def main():
cluster_name=dict(type='str', required=True),
group_name=dict(type='str', required=True),
vms=dict(type='list', elements='str'),
hosts=dict(type='list', elements='str')
hosts=dict(type='list', elements='str'),
operation=dict(type='str', default='set', choices=['add', 'set', 'remove'])
)

required_if = [
Expand Down

0 comments on commit 0751f2c

Please sign in to comment.