VMware datacenter module shouldn't hold pyvmomi context in Ansible module object (#1568)
* VMware datacenter module rewritten to don't hold pyvmomi context and objects in Ansible module object fixed exceptions handling added datacenter destroy result, moved checks changed wrong value wrong value again... need some sleep * check_mode fixes * state defaults to present, default changed to true * module check fixes
This commit is contained in:
parent
e986aad9aa
commit
26fe35fd0c
1 changed files with 39 additions and 58 deletions
|
@ -25,9 +25,9 @@ short_description: Manage VMware vSphere Datacenters
|
||||||
description:
|
description:
|
||||||
- Manage VMware vSphere Datacenters
|
- Manage VMware vSphere Datacenters
|
||||||
version_added: 2.0
|
version_added: 2.0
|
||||||
author: "Joseph Callen (@jcpowermac)"
|
author: "Joseph Callen (@jcpowermac), Kamil Szczygiel (@kamsz)"
|
||||||
notes:
|
notes:
|
||||||
- Tested on vSphere 5.5
|
- Tested on vSphere 6.0
|
||||||
requirements:
|
requirements:
|
||||||
- "python >= 2.6"
|
- "python >= 2.6"
|
||||||
- PyVmomi
|
- PyVmomi
|
||||||
|
@ -54,7 +54,7 @@ options:
|
||||||
description:
|
description:
|
||||||
- If the datacenter should be present or absent
|
- If the datacenter should be present or absent
|
||||||
choices: ['present', 'absent']
|
choices: ['present', 'absent']
|
||||||
required: True
|
default: present
|
||||||
extends_documentation_fragment: vmware.documentation
|
extends_documentation_fragment: vmware.documentation
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ EXAMPLES = '''
|
||||||
local_action: >
|
local_action: >
|
||||||
vmware_datacenter
|
vmware_datacenter
|
||||||
hostname="{{ ansible_ssh_host }}" username=root password=vmware
|
hostname="{{ ansible_ssh_host }}" username=root password=vmware
|
||||||
datacenter_name="datacenter"
|
datacenter_name="datacenter" state=present
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -74,18 +74,28 @@ except ImportError:
|
||||||
HAS_PYVMOMI = False
|
HAS_PYVMOMI = False
|
||||||
|
|
||||||
|
|
||||||
def state_create_datacenter(module):
|
def get_datacenter(context, module):
|
||||||
datacenter_name = module.params['datacenter_name']
|
try:
|
||||||
content = module.params['content']
|
datacenter_name = module.params.get('datacenter_name')
|
||||||
changed = True
|
datacenter = find_datacenter_by_name(context, datacenter_name)
|
||||||
datacenter = None
|
return datacenter
|
||||||
|
except vmodl.RuntimeFault as runtime_fault:
|
||||||
|
module.fail_json(msg=runtime_fault.msg)
|
||||||
|
except vmodl.MethodFault as method_fault:
|
||||||
|
module.fail_json(msg=method_fault.msg)
|
||||||
|
|
||||||
folder = content.rootFolder
|
|
||||||
|
def create_datacenter(context, module):
|
||||||
|
datacenter_name = module.params.get('datacenter_name')
|
||||||
|
folder = context.rootFolder
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
datacenter = get_datacenter(context, module)
|
||||||
|
if not datacenter:
|
||||||
|
changed = True
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
datacenter = folder.CreateDatacenter(name=datacenter_name)
|
folder.CreateDatacenter(name=datacenter_name)
|
||||||
module.exit_json(changed=changed, result=str(datacenter))
|
module.exit_json(changed=changed)
|
||||||
except vim.fault.DuplicateName:
|
except vim.fault.DuplicateName:
|
||||||
module.fail_json(msg="A datacenter with the name %s already exists" % datacenter_name)
|
module.fail_json(msg="A datacenter with the name %s already exists" % datacenter_name)
|
||||||
except vim.fault.InvalidName:
|
except vim.fault.InvalidName:
|
||||||
|
@ -99,31 +109,13 @@ def state_create_datacenter(module):
|
||||||
module.fail_json(msg=method_fault.msg)
|
module.fail_json(msg=method_fault.msg)
|
||||||
|
|
||||||
|
|
||||||
def check_datacenter_state(module):
|
def destroy_datacenter(context, module):
|
||||||
datacenter_name = module.params['datacenter_name']
|
|
||||||
|
|
||||||
try:
|
|
||||||
content = connect_to_api(module)
|
|
||||||
datacenter = find_datacenter_by_name(content, datacenter_name)
|
|
||||||
module.params['content'] = content
|
|
||||||
|
|
||||||
if datacenter is None:
|
|
||||||
return 'absent'
|
|
||||||
else:
|
|
||||||
module.params['datacenter'] = datacenter
|
|
||||||
return 'present'
|
|
||||||
except vmodl.RuntimeFault as runtime_fault:
|
|
||||||
module.fail_json(msg=runtime_fault.msg)
|
|
||||||
except vmodl.MethodFault as method_fault:
|
|
||||||
module.fail_json(msg=method_fault.msg)
|
|
||||||
|
|
||||||
|
|
||||||
def state_destroy_datacenter(module):
|
|
||||||
datacenter = module.params['datacenter']
|
|
||||||
changed = True
|
|
||||||
result = None
|
result = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
datacenter = get_datacenter(context, module)
|
||||||
|
if datacenter:
|
||||||
|
changed = True
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
task = datacenter.Destroy_Task()
|
task = datacenter.Destroy_Task()
|
||||||
changed, result = wait_for_task(task)
|
changed, result = wait_for_task(task)
|
||||||
|
@ -136,17 +128,13 @@ def state_destroy_datacenter(module):
|
||||||
module.fail_json(msg=method_fault.msg)
|
module.fail_json(msg=method_fault.msg)
|
||||||
|
|
||||||
|
|
||||||
def state_exit_unchanged(module):
|
|
||||||
module.exit_json(changed=False)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
argument_spec = vmware_argument_spec()
|
argument_spec = vmware_argument_spec()
|
||||||
argument_spec.update(
|
argument_spec.update(
|
||||||
dict(
|
dict(
|
||||||
datacenter_name=dict(required=True, type='str'),
|
datacenter_name=dict(required=True, type='str'),
|
||||||
state=dict(required=True, choices=['present', 'absent'], type='str'),
|
state=dict(default='present', choices=['present', 'absent'], type='str')
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||||
|
@ -154,21 +142,14 @@ def main():
|
||||||
if not HAS_PYVMOMI:
|
if not HAS_PYVMOMI:
|
||||||
module.fail_json(msg='pyvmomi is required for this module')
|
module.fail_json(msg='pyvmomi is required for this module')
|
||||||
|
|
||||||
datacenter_states = {
|
context = connect_to_api(module)
|
||||||
'absent': {
|
state = module.params.get('state')
|
||||||
'present': state_destroy_datacenter,
|
|
||||||
'absent': state_exit_unchanged,
|
|
||||||
},
|
|
||||||
'present': {
|
|
||||||
'present': state_exit_unchanged,
|
|
||||||
'absent': state_create_datacenter,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
desired_state = module.params['state']
|
|
||||||
current_state = check_datacenter_state(module)
|
|
||||||
|
|
||||||
datacenter_states[desired_state][current_state](module)
|
if state == 'present':
|
||||||
|
create_datacenter(context, module)
|
||||||
|
|
||||||
|
if state == 'absent':
|
||||||
|
destroy_datacenter(context, module)
|
||||||
|
|
||||||
from ansible.module_utils.basic import *
|
from ansible.module_utils.basic import *
|
||||||
from ansible.module_utils.vmware import *
|
from ansible.module_utils.vmware import *
|
||||||
|
|
Loading…
Reference in a new issue