VMware: Improve module vcenter folder (#46497)
* Add datacenter_name alias * Improve vcenter_folder module * add check mode support * fix VM folder deletion * fix folder deletion under parent folder * fix (host) folder creation if two datacenters are present
This commit is contained in:
parent
c24c19594e
commit
ea42ec7e90
2 changed files with 179 additions and 69 deletions
|
@ -22,6 +22,7 @@ description:
|
||||||
version_added: '2.5'
|
version_added: '2.5'
|
||||||
author:
|
author:
|
||||||
- Abhijeet Kasurde (@Akasurde)
|
- Abhijeet Kasurde (@Akasurde)
|
||||||
|
- Christian Kotte (@ckotte) <christian.kotte@gmx.de>
|
||||||
notes:
|
notes:
|
||||||
- Tested on vSphere 6.5
|
- Tested on vSphere 6.5
|
||||||
requirements:
|
requirements:
|
||||||
|
@ -32,6 +33,7 @@ options:
|
||||||
description:
|
description:
|
||||||
- Name of the datacenter.
|
- Name of the datacenter.
|
||||||
required: True
|
required: True
|
||||||
|
aliases: ['datacenter_name']
|
||||||
folder_name:
|
folder_name:
|
||||||
description:
|
description:
|
||||||
- Name of folder to be managed.
|
- Name of folder to be managed.
|
||||||
|
@ -74,7 +76,7 @@ EXAMPLES = r'''
|
||||||
hostname: '{{ vcenter_hostname }}'
|
hostname: '{{ vcenter_hostname }}'
|
||||||
username: '{{ vcenter_username }}'
|
username: '{{ vcenter_username }}'
|
||||||
password: '{{ vcenter_password }}'
|
password: '{{ vcenter_password }}'
|
||||||
datacenter: datacenter_name
|
datacenter_name: datacenter_name
|
||||||
folder_name: sample_vm_folder
|
folder_name: sample_vm_folder
|
||||||
folder_type: vm
|
folder_type: vm
|
||||||
state: present
|
state: present
|
||||||
|
@ -86,7 +88,7 @@ EXAMPLES = r'''
|
||||||
hostname: '{{ vcenter_hostname }}'
|
hostname: '{{ vcenter_hostname }}'
|
||||||
username: '{{ vcenter_username }}'
|
username: '{{ vcenter_username }}'
|
||||||
password: '{{ vcenter_password }}'
|
password: '{{ vcenter_password }}'
|
||||||
datacenter: datacenter_name
|
datacenter_name: datacenter_name
|
||||||
folder_name: sample_datastore_folder
|
folder_name: sample_datastore_folder
|
||||||
folder_type: datastore
|
folder_type: datastore
|
||||||
state: present
|
state: present
|
||||||
|
@ -98,7 +100,7 @@ EXAMPLES = r'''
|
||||||
hostname: '{{ vcenter_hostname }}'
|
hostname: '{{ vcenter_hostname }}'
|
||||||
username: '{{ vcenter_username }}'
|
username: '{{ vcenter_username }}'
|
||||||
password: '{{ vcenter_password }}'
|
password: '{{ vcenter_password }}'
|
||||||
datacenter: datacenter_name
|
datacenter_name: datacenter_name
|
||||||
folder_name: sample_sub_folder
|
folder_name: sample_sub_folder
|
||||||
parent_folder: vm_folder
|
parent_folder: vm_folder
|
||||||
state: present
|
state: present
|
||||||
|
@ -110,7 +112,7 @@ EXAMPLES = r'''
|
||||||
hostname: '{{ vcenter_hostname }}'
|
hostname: '{{ vcenter_hostname }}'
|
||||||
username: '{{ vcenter_username }}'
|
username: '{{ vcenter_username }}'
|
||||||
password: '{{ vcenter_password }}'
|
password: '{{ vcenter_password }}'
|
||||||
datacenter: datacenter_name
|
datacenter_name: datacenter_name
|
||||||
folder_name: sample_vm_folder
|
folder_name: sample_vm_folder
|
||||||
folder_type: vm
|
folder_type: vm
|
||||||
state: absent
|
state: absent
|
||||||
|
@ -157,6 +159,7 @@ class VmwareFolderManager(PyVmomi):
|
||||||
Manage internal state management
|
Manage internal state management
|
||||||
"""
|
"""
|
||||||
state = self.module.params.get('state')
|
state = self.module.params.get('state')
|
||||||
|
datacenter_name = self.module.params.get('datacenter')
|
||||||
folder_type = self.module.params.get('folder_type')
|
folder_type = self.module.params.get('folder_type')
|
||||||
folder_name = self.module.params.get('folder_name')
|
folder_name = self.module.params.get('folder_name')
|
||||||
parent_folder = self.module.params.get('parent_folder', None)
|
parent_folder = self.module.params.get('parent_folder', None)
|
||||||
|
@ -165,14 +168,16 @@ class VmwareFolderManager(PyVmomi):
|
||||||
# Check if the folder already exists
|
# Check if the folder already exists
|
||||||
p_folder_obj = None
|
p_folder_obj = None
|
||||||
if parent_folder:
|
if parent_folder:
|
||||||
p_folder_obj = self.get_folder(folder_name=parent_folder,
|
p_folder_obj = self.get_folder(datacenter_name=datacenter_name,
|
||||||
|
folder_name=parent_folder,
|
||||||
folder_type=folder_type)
|
folder_type=folder_type)
|
||||||
|
|
||||||
if not p_folder_obj:
|
if not p_folder_obj:
|
||||||
self.module.fail_json(msg="Parent folder %s does not exist" % parent_folder)
|
self.module.fail_json(msg="Parent folder %s does not exist" % parent_folder)
|
||||||
|
|
||||||
# Check if folder exists under parent folder
|
# Check if folder exists under parent folder
|
||||||
child_folder_obj = self.get_folder(folder_name=folder_name,
|
child_folder_obj = self.get_folder(datacenter_name=datacenter_name,
|
||||||
|
folder_name=folder_name,
|
||||||
folder_type=folder_type,
|
folder_type=folder_type,
|
||||||
parent_folder=p_folder_obj)
|
parent_folder=p_folder_obj)
|
||||||
if child_folder_obj:
|
if child_folder_obj:
|
||||||
|
@ -180,7 +185,8 @@ class VmwareFolderManager(PyVmomi):
|
||||||
" parent folder %s" % (folder_name, parent_folder)
|
" parent folder %s" % (folder_name, parent_folder)
|
||||||
self.module.exit_json(**results)
|
self.module.exit_json(**results)
|
||||||
else:
|
else:
|
||||||
folder_obj = self.get_folder(folder_name=folder_name,
|
folder_obj = self.get_folder(datacenter_name=datacenter_name,
|
||||||
|
folder_name=folder_name,
|
||||||
folder_type=folder_type)
|
folder_type=folder_type)
|
||||||
|
|
||||||
if folder_obj:
|
if folder_obj:
|
||||||
|
@ -190,14 +196,21 @@ class VmwareFolderManager(PyVmomi):
|
||||||
# Create a new folder
|
# Create a new folder
|
||||||
try:
|
try:
|
||||||
if parent_folder and p_folder_obj:
|
if parent_folder and p_folder_obj:
|
||||||
p_folder_obj.CreateFolder(folder_name)
|
if self.module.check_mode:
|
||||||
|
results['result'] = "Folder '%s' of type '%s' under '%s' will be created." % \
|
||||||
|
(folder_name, folder_type, parent_folder)
|
||||||
|
else:
|
||||||
|
p_folder_obj.CreateFolder(folder_name)
|
||||||
|
results['result'] = "Folder '%s' of type '%s' under '%s' created" \
|
||||||
|
" successfully." % (folder_name, folder_type, parent_folder)
|
||||||
results['changed'] = True
|
results['changed'] = True
|
||||||
results['result'] = "Folder '%s' of type '%s' created under %s" \
|
|
||||||
" successfully." % (folder_name, folder_type, parent_folder)
|
|
||||||
elif not parent_folder and not p_folder_obj:
|
elif not parent_folder and not p_folder_obj:
|
||||||
self.datacenter_folder_type[folder_type].CreateFolder(folder_name)
|
if self.module.check_mode:
|
||||||
|
results['result'] = "Folder '%s' of type '%s' will be created." % (folder_name, folder_type)
|
||||||
|
else:
|
||||||
|
self.datacenter_folder_type[folder_type].CreateFolder(folder_name)
|
||||||
|
results['result'] = "Folder '%s' of type '%s' created successfully." % (folder_name, folder_type)
|
||||||
results['changed'] = True
|
results['changed'] = True
|
||||||
results['result'] = "Folder '%s' of type '%s' created successfully" % (folder_name, folder_type)
|
|
||||||
except vim.fault.DuplicateName as duplicate_name:
|
except vim.fault.DuplicateName as duplicate_name:
|
||||||
# To be consistent with the other vmware modules, We decided to accept this error
|
# To be consistent with the other vmware modules, We decided to accept this error
|
||||||
# and the playbook should simply carry on with other tasks.
|
# and the playbook should simply carry on with other tasks.
|
||||||
|
@ -214,11 +227,48 @@ class VmwareFolderManager(PyVmomi):
|
||||||
" exception : %s " % to_native(general_exc))
|
" exception : %s " % to_native(general_exc))
|
||||||
self.module.exit_json(**results)
|
self.module.exit_json(**results)
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
folder_obj = self.get_folder(folder_name=folder_name, folder_type=folder_type)
|
# Check if the folder already exists
|
||||||
|
p_folder_obj = None
|
||||||
|
if parent_folder:
|
||||||
|
p_folder_obj = self.get_folder(datacenter_name=datacenter_name,
|
||||||
|
folder_name=parent_folder,
|
||||||
|
folder_type=folder_type)
|
||||||
|
|
||||||
|
if not p_folder_obj:
|
||||||
|
self.module.fail_json(msg="Parent folder %s does not exist" % parent_folder)
|
||||||
|
|
||||||
|
# Check if folder exists under parent folder
|
||||||
|
folder_obj = self.get_folder(datacenter_name=datacenter_name,
|
||||||
|
folder_name=folder_name,
|
||||||
|
folder_type=folder_type,
|
||||||
|
parent_folder=p_folder_obj)
|
||||||
|
else:
|
||||||
|
folder_obj = self.get_folder(datacenter_name=datacenter_name,
|
||||||
|
folder_name=folder_name,
|
||||||
|
folder_type=folder_type)
|
||||||
if folder_obj:
|
if folder_obj:
|
||||||
try:
|
try:
|
||||||
task = folder_obj.UnregisterAndDestroy()
|
if parent_folder:
|
||||||
results['changed'], results['result'] = wait_for_task(task=task)
|
if self.module.check_mode:
|
||||||
|
results['changed'] = True
|
||||||
|
results['result'] = "Folder '%s' of type '%s' under '%s' will be removed." % \
|
||||||
|
(folder_name, folder_type, parent_folder)
|
||||||
|
else:
|
||||||
|
if folder_type == 'vm':
|
||||||
|
task = folder_obj.UnregisterAndDestroy()
|
||||||
|
else:
|
||||||
|
task = folder_obj.Destroy()
|
||||||
|
results['changed'], results['result'] = wait_for_task(task=task)
|
||||||
|
else:
|
||||||
|
if self.module.check_mode:
|
||||||
|
results['changed'] = True
|
||||||
|
results['result'] = "Folder '%s' of type '%s' will be removed." % (folder_name, folder_type)
|
||||||
|
else:
|
||||||
|
if folder_type == 'vm':
|
||||||
|
task = folder_obj.UnregisterAndDestroy()
|
||||||
|
else:
|
||||||
|
task = folder_obj.Destroy()
|
||||||
|
results['changed'], results['result'] = wait_for_task(task=task)
|
||||||
except vim.fault.ConcurrentAccess as concurrent_access:
|
except vim.fault.ConcurrentAccess as concurrent_access:
|
||||||
self.module.fail_json(msg="Failed to remove folder as another client"
|
self.module.fail_json(msg="Failed to remove folder as another client"
|
||||||
" modified folder before this operation : %s" % to_native(concurrent_access.msg))
|
" modified folder before this operation : %s" % to_native(concurrent_access.msg))
|
||||||
|
@ -230,7 +280,7 @@ class VmwareFolderManager(PyVmomi):
|
||||||
" exception %s " % to_native(gen_exec))
|
" exception %s " % to_native(gen_exec))
|
||||||
self.module.exit_json(**results)
|
self.module.exit_json(**results)
|
||||||
|
|
||||||
def get_folder(self, folder_name, folder_type, parent_folder=None):
|
def get_folder(self, datacenter_name, folder_name, folder_type, parent_folder=None):
|
||||||
"""
|
"""
|
||||||
Get managed object of folder by name
|
Get managed object of folder by name
|
||||||
Returns: Managed object of folder by name
|
Returns: Managed object of folder by name
|
||||||
|
@ -238,9 +288,15 @@ class VmwareFolderManager(PyVmomi):
|
||||||
"""
|
"""
|
||||||
folder_objs = get_all_objs(self.content, [vim.Folder], parent_folder)
|
folder_objs = get_all_objs(self.content, [vim.Folder], parent_folder)
|
||||||
for folder in folder_objs:
|
for folder in folder_objs:
|
||||||
if folder.name == folder_name and \
|
if parent_folder:
|
||||||
self.datacenter_folder_type[folder_type].childType == folder.childType:
|
if folder.name == folder_name and \
|
||||||
return folder
|
self.datacenter_folder_type[folder_type].childType == folder.childType:
|
||||||
|
return folder
|
||||||
|
else:
|
||||||
|
if folder.name == folder_name and \
|
||||||
|
self.datacenter_folder_type[folder_type].childType == folder.childType and \
|
||||||
|
folder.parent.parent.name == datacenter_name: # e.g. folder.parent.parent.name == /DC01/host/folder
|
||||||
|
return folder
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -248,7 +304,7 @@ class VmwareFolderManager(PyVmomi):
|
||||||
def main():
|
def main():
|
||||||
argument_spec = vmware_argument_spec()
|
argument_spec = vmware_argument_spec()
|
||||||
argument_spec.update(
|
argument_spec.update(
|
||||||
datacenter=dict(type='str', required=True),
|
datacenter=dict(type='str', required=True, aliases=['datacenter_name']),
|
||||||
folder_name=dict(type='str', required=True),
|
folder_name=dict(type='str', required=True),
|
||||||
parent_folder=dict(type='str', required=False),
|
parent_folder=dict(type='str', required=False),
|
||||||
state=dict(type='str',
|
state=dict(type='str',
|
||||||
|
@ -262,7 +318,7 @@ def main():
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=argument_spec,
|
argument_spec=argument_spec,
|
||||||
supports_check_mode=False,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(module.params.get('folder_name')) > 79:
|
if len(module.params.get('folder_name')) > 79:
|
||||||
|
|
|
@ -41,6 +41,31 @@
|
||||||
set_fact:
|
set_fact:
|
||||||
dc1: "{{ datacenters.json[0] | basename }}"
|
dc1: "{{ datacenters.json[0] | basename }}"
|
||||||
|
|
||||||
|
- name: Create all types of folder in check mode
|
||||||
|
vcenter_folder:
|
||||||
|
hostname: "{{ vcsim }}"
|
||||||
|
username: "{{ vcsim_instance.json.username }}"
|
||||||
|
password: "{{ vcsim_instance.json.password }}"
|
||||||
|
validate_certs: no
|
||||||
|
datacenter: "{{ dc1 }}"
|
||||||
|
folder_name: "{{ item }}_folder"
|
||||||
|
folder_type: "{{ item }}"
|
||||||
|
state: present
|
||||||
|
register: all_folder_results
|
||||||
|
with_items:
|
||||||
|
- vm
|
||||||
|
- host
|
||||||
|
- datastore
|
||||||
|
- network
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- debug: msg="{{ all_folder_results }}"
|
||||||
|
|
||||||
|
- name: ensure everything for {{ dc1 }}
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- all_folder_results.changed
|
||||||
|
|
||||||
- name: Create all types of folder
|
- name: Create all types of folder
|
||||||
vcenter_folder:
|
vcenter_folder:
|
||||||
hostname: "{{ vcsim }}"
|
hostname: "{{ vcsim }}"
|
||||||
|
@ -60,6 +85,31 @@
|
||||||
|
|
||||||
- debug: msg="{{ all_folder_results }}"
|
- debug: msg="{{ all_folder_results }}"
|
||||||
|
|
||||||
|
- name: ensure everything for {{ dc1 }}
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- all_folder_results.changed
|
||||||
|
|
||||||
|
- name: Create all types of sub folder in check mode
|
||||||
|
vcenter_folder:
|
||||||
|
hostname: "{{ vcsim }}"
|
||||||
|
username: "{{ vcsim_instance.json.username }}"
|
||||||
|
password: "{{ vcsim_instance.json.password }}"
|
||||||
|
validate_certs: no
|
||||||
|
datacenter: "{{ dc1 }}"
|
||||||
|
folder_name: "sub_{{ item }}_folder"
|
||||||
|
parent_folder: 'vm_folder'
|
||||||
|
state: present
|
||||||
|
register: all_folder_results
|
||||||
|
with_items:
|
||||||
|
- vm
|
||||||
|
- host
|
||||||
|
- datastore
|
||||||
|
- network
|
||||||
|
check_mode: yes
|
||||||
|
|
||||||
|
- debug: msg="{{ all_folder_results }}"
|
||||||
|
|
||||||
- name: ensure everything for {{ dc1 }}
|
- name: ensure everything for {{ dc1 }}
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
|
@ -81,7 +131,7 @@
|
||||||
- host
|
- host
|
||||||
- datastore
|
- datastore
|
||||||
- network
|
- network
|
||||||
|
|
||||||
- name: Recreate all types of sub folder
|
- name: Recreate all types of sub folder
|
||||||
vcenter_folder:
|
vcenter_folder:
|
||||||
hostname: "{{ vcsim }}"
|
hostname: "{{ vcsim }}"
|
||||||
|
@ -107,50 +157,54 @@
|
||||||
- all_folder_results.changed
|
- all_folder_results.changed
|
||||||
- not recreate_folders.changed
|
- not recreate_folders.changed
|
||||||
|
|
||||||
#- name: Delete all types of folder
|
## Testcase: Delete Delete all types of folder
|
||||||
# vcenter_folder:
|
#
|
||||||
# hostname: "{{ vcsim }}"
|
# Doesn't work with vcsim. Looks like UnregisterAndDestroy isn't supported.
|
||||||
# username: "{{ vcsim_instance.json.username }}"
|
#
|
||||||
# password: "{{ vcsim_instance.json.password }}"
|
# - name: Delete all types of folder
|
||||||
# validate_certs: no
|
# vcenter_folder:
|
||||||
# datacenter: "{{ dc1 }}"
|
# hostname: "{{ vcsim }}"
|
||||||
# folder_name: "{{ item }}_folder"
|
# username: "{{ vcsim_instance.json.username }}"
|
||||||
# folder_type: "{{ item }}"
|
# password: "{{ vcsim_instance.json.password }}"
|
||||||
# state: absent
|
# validate_certs: no
|
||||||
# register: all_folder_results
|
# datacenter: "{{ dc1 }}"
|
||||||
# with_items:
|
# folder_name: "{{ item }}_folder"
|
||||||
# - vm
|
# folder_type: "{{ item }}"
|
||||||
# - host
|
# state: absent
|
||||||
# - datastore
|
# register: all_folder_results
|
||||||
# - network
|
# with_items:
|
||||||
|
# - vm
|
||||||
#- debug: msg="{{ all_folder_results }}"
|
# - host
|
||||||
|
# - datastore
|
||||||
#- name: ensure everything for {{ dc1 }}
|
# - network
|
||||||
# assert:
|
#
|
||||||
# that:
|
# - debug: msg="{{ all_folder_results }}"
|
||||||
# - all_folder_results.changed
|
#
|
||||||
|
# - name: ensure everything for {{ dc1 }}
|
||||||
#- name: Delete all types of folder again
|
# assert:
|
||||||
# vcenter_folder:
|
# that:
|
||||||
# hostname: "{{ vcsim }}"
|
# - all_folder_results.changed
|
||||||
# username: "{{ vcsim_instance.json.username }}"
|
#
|
||||||
# password: "{{ vcsim_instance.json.password }}"
|
# - name: Delete all types of folder again
|
||||||
# validate_certs: no
|
# vcenter_folder:
|
||||||
# datacenter: "{{ dc1 }}"
|
# hostname: "{{ vcsim }}"
|
||||||
# folder_name: "{{ item }}_folder"
|
# username: "{{ vcsim_instance.json.username }}"
|
||||||
# folder_type: "{{ item }}"
|
# password: "{{ vcsim_instance.json.password }}"
|
||||||
# state: absent
|
# validate_certs: no
|
||||||
# register: all_folder_results
|
# datacenter: "{{ dc1 }}"
|
||||||
# with_items:
|
# folder_name: "{{ item }}_folder"
|
||||||
# - vm
|
# folder_type: "{{ item }}"
|
||||||
# - host
|
# state: absent
|
||||||
# - datastore
|
# register: all_folder_results
|
||||||
# - network
|
# with_items:
|
||||||
|
# - vm
|
||||||
#- debug: msg="{{ all_folder_results }}"
|
# - host
|
||||||
|
# - datastore
|
||||||
#- name: ensure everything for {{ dc1 }}
|
# - network
|
||||||
# assert:
|
#
|
||||||
# that:
|
# - debug: msg="{{ all_folder_results }}"
|
||||||
# - not all_folder_results.changed
|
#
|
||||||
|
# - name: ensure everything for {{ dc1 }}
|
||||||
|
# assert:
|
||||||
|
# that:
|
||||||
|
# - not all_folder_results.changed
|
||||||
|
|
Loading…
Reference in a new issue