Resolves issue with vmware_cluster module for v2.0

When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_cluster module.

@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568

Playbook
```
    - name: Create Cluster
      local_action:
        module: vmware_cluster
        hostname: "{{ mgmt_ip_address }}"
        username: "{{ vcsa_user }}"
        password: "{{ vcsa_pass }}"
        datacenter_name: "{{ mgmt_vdc }}"
        cluster_name: "{{ mgmt_cluster }}"
        enable_ha: True
        enable_drs: True
        enable_vsan: True
```

Module testing
```
TASK [Create Cluster] **********************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:188
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693788.92-14097560271233 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693788.92-14097560271233 )" )
localhost PUT /tmp/tmpAJfdPb TO /root/.ansible/tmp/ansible-tmp-1454693788.92-14097560271233/vmware_cluster
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693788.92-14097560271233/vmware_cluster; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693788.92-14097560271233/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "enable_drs": true, "enable_ha": true, "enable_vsan": true, "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_cluster"}}
```
This commit is contained in:
Joseph Callen 2016-02-05 14:14:25 -05:00 committed by Matt Clay
parent 0aa4f867de
commit 9bdc59a6ac

View file

@ -77,152 +77,153 @@ except ImportError:
HAS_PYVMOMI = False HAS_PYVMOMI = False
def configure_ha(enable_ha): class VMwareCluster(object):
def __init__(self, module):
self.module = module
self.enable_ha = module.params['enable_ha']
self.enable_drs = module.params['enable_drs']
self.enable_vsan = module.params['enable_vsan']
self.cluster_name = module.params['cluster_name']
self.desired_state = module.params['state']
self.datacenter = None
self.cluster = None
self.content = connect_to_api(module)
self.datacenter_name = module.params['datacenter_name']
def process_state(self):
cluster_states = {
'absent': {
'present': self.state_destroy_cluster,
'absent': self.state_exit_unchanged,
},
'present': {
'update': self.state_update_cluster,
'present': self.state_exit_unchanged,
'absent': self.state_create_cluster,
}
}
current_state = self.check_cluster_configuration()
# Based on the desired_state and the current_state call
# the appropriate method from the dictionary
cluster_states[self.desired_state][current_state]()
def configure_ha(self):
das_config = vim.cluster.DasConfigInfo() das_config = vim.cluster.DasConfigInfo()
das_config.enabled = enable_ha das_config.enabled = self.enable_ha
das_config.admissionControlPolicy = vim.cluster.FailoverLevelAdmissionControlPolicy() das_config.admissionControlPolicy = vim.cluster.FailoverLevelAdmissionControlPolicy()
das_config.admissionControlPolicy.failoverLevel = 2 das_config.admissionControlPolicy.failoverLevel = 2
return das_config return das_config
def configure_drs(self):
def configure_drs(enable_drs):
drs_config = vim.cluster.DrsConfigInfo() drs_config = vim.cluster.DrsConfigInfo()
drs_config.enabled = enable_drs drs_config.enabled = self.enable_drs
# Set to partially automated # Set to partially automated
drs_config.vmotionRate = 3 drs_config.vmotionRate = 3
return drs_config return drs_config
def configure_vsan(self):
def configure_vsan(enable_vsan):
vsan_config = vim.vsan.cluster.ConfigInfo() vsan_config = vim.vsan.cluster.ConfigInfo()
vsan_config.enabled = enable_vsan vsan_config.enabled = self.enable_vsan
vsan_config.defaultConfig = vim.vsan.cluster.ConfigInfo.HostDefaultInfo() vsan_config.defaultConfig = vim.vsan.cluster.ConfigInfo.HostDefaultInfo()
vsan_config.defaultConfig.autoClaimStorage = False vsan_config.defaultConfig.autoClaimStorage = False
return vsan_config return vsan_config
def state_create_cluster(self):
def state_create_cluster(module):
enable_ha = module.params['enable_ha']
enable_drs = module.params['enable_drs']
enable_vsan = module.params['enable_vsan']
cluster_name = module.params['cluster_name']
datacenter = module.params['datacenter']
try: try:
cluster_config_spec = vim.cluster.ConfigSpecEx() cluster_config_spec = vim.cluster.ConfigSpecEx()
cluster_config_spec.dasConfig = configure_ha(enable_ha) cluster_config_spec.dasConfig = self.configure_ha()
cluster_config_spec.drsConfig = configure_drs(enable_drs) cluster_config_spec.drsConfig = self.configure_drs()
if enable_vsan: if self.enable_vsan:
cluster_config_spec.vsanConfig = configure_vsan(enable_vsan) cluster_config_spec.vsanConfig = self.configure_vsan()
if not module.check_mode: if not self.module.check_mode:
datacenter.hostFolder.CreateClusterEx(cluster_name, cluster_config_spec) self.datacenter.hostFolder.CreateClusterEx(self.cluster_name, cluster_config_spec)
module.exit_json(changed=True) self.module.exit_json(changed=True)
except vim.fault.DuplicateName: except vim.fault.DuplicateName:
module.fail_json(msg="A cluster with the name %s already exists" % cluster_name) self.module.fail_json(msg="A cluster with the name %s already exists" % self.cluster_name)
except vmodl.fault.InvalidArgument: except vmodl.fault.InvalidArgument:
module.fail_json(msg="Cluster configuration specification parameter is invalid") self.module.fail_json(msg="Cluster configuration specification parameter is invalid")
except vim.fault.InvalidName: except vim.fault.InvalidName:
module.fail_json(msg="%s is an invalid name for a cluster" % cluster_name) self.module.fail_json(msg="%s is an invalid name for a cluster" % self.cluster_name)
except vmodl.fault.NotSupported: except vmodl.fault.NotSupported:
# This should never happen # This should never happen
module.fail_json(msg="Trying to create a cluster on an incorrect folder object") self.module.fail_json(msg="Trying to create a cluster on an incorrect folder object")
except vmodl.RuntimeFault as runtime_fault: except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg) self.module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault: except vmodl.MethodFault as method_fault:
# This should never happen either # This should never happen either
module.fail_json(msg=method_fault.msg) self.module.fail_json(msg=method_fault.msg)
def state_destroy_cluster(self):
def state_destroy_cluster(module):
cluster = module.params['cluster']
changed = True changed = True
result = None result = None
try: try:
if not module.check_mode: if not self.module.check_mode:
task = cluster.Destroy_Task() task = self.cluster.Destroy_Task()
changed, result = wait_for_task(task) changed, result = wait_for_task(task)
module.exit_json(changed=changed, result=result) self.module.exit_json(changed=changed, result=result)
except vim.fault.VimFault as vim_fault: except vim.fault.VimFault as vim_fault:
module.fail_json(msg=vim_fault.msg) self.module.fail_json(msg=vim_fault.msg)
except vmodl.RuntimeFault as runtime_fault: except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg) self.module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault: except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg) self.module.fail_json(msg=method_fault.msg)
def state_exit_unchanged(self):
self.module.exit_json(changed=False)
def state_exit_unchanged(module): def state_update_cluster(self):
module.exit_json(changed=False)
def state_update_cluster(module):
cluster_config_spec = vim.cluster.ConfigSpecEx() cluster_config_spec = vim.cluster.ConfigSpecEx()
cluster = module.params['cluster']
enable_ha = module.params['enable_ha']
enable_drs = module.params['enable_drs']
enable_vsan = module.params['enable_vsan']
changed = True changed = True
result = None result = None
if cluster.configurationEx.dasConfig.enabled != enable_ha: if self.cluster.configurationEx.dasConfig.enabled != self.enable_ha:
cluster_config_spec.dasConfig = configure_ha(enable_ha) cluster_config_spec.dasConfig = self.configure_ha()
if cluster.configurationEx.drsConfig.enabled != enable_drs: if self.cluster.configurationEx.drsConfig.enabled != self.enable_drs:
cluster_config_spec.drsConfig = configure_drs(enable_drs) cluster_config_spec.drsConfig = self.configure_drs()
if cluster.configurationEx.vsanConfigInfo.enabled != enable_vsan: if self.cluster.configurationEx.vsanConfigInfo.enabled != self.enable_vsan:
cluster_config_spec.vsanConfig = configure_vsan(enable_vsan) cluster_config_spec.vsanConfig = self.configure_vsan()
try: try:
if not module.check_mode: if not self.module.check_mode:
task = cluster.ReconfigureComputeResource_Task(cluster_config_spec, True) task = self.cluster.ReconfigureComputeResource_Task(cluster_config_spec, True)
changed, result = wait_for_task(task) changed, result = wait_for_task(task)
module.exit_json(changed=changed, result=result) self.module.exit_json(changed=changed, result=result)
except vmodl.RuntimeFault as runtime_fault: except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg) self.module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault: except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg) self.module.fail_json(msg=method_fault.msg)
except TaskError as task_e: except TaskError as task_e:
module.fail_json(msg=str(task_e)) self.module.fail_json(msg=str(task_e))
def check_cluster_configuration(module):
datacenter_name = module.params['datacenter_name']
cluster_name = module.params['cluster_name']
def check_cluster_configuration(self):
try: try:
content = connect_to_api(module) self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name)
datacenter = find_datacenter_by_name(content, datacenter_name) if self.datacenter is None:
if datacenter is None: self.module.fail_json(msg="Datacenter %s does not exist, "
module.fail_json(msg="Datacenter %s does not exist, "
"please create first with Ansible Module vmware_datacenter or manually." "please create first with Ansible Module vmware_datacenter or manually."
% datacenter_name) % self.datacenter_name)
cluster = find_cluster_by_name_datacenter(datacenter, cluster_name) self.cluster = find_cluster_by_name_datacenter(self.datacenter, self.cluster_name)
module.params['content'] = content if self.cluster is None:
module.params['datacenter'] = datacenter
if cluster is None:
return 'absent' return 'absent'
else: else:
module.params['cluster'] = cluster desired_state = (self.enable_ha,
self.enable_drs,
self.enable_vsan)
desired_state = (module.params['enable_ha'], current_state = (self.cluster.configurationEx.dasConfig.enabled,
module.params['enable_drs'], self.cluster.configurationEx.drsConfig.enabled,
module.params['enable_vsan']) self.cluster.configurationEx.vsanConfigInfo.enabled)
current_state = (cluster.configurationEx.dasConfig.enabled,
cluster.configurationEx.drsConfig.enabled,
cluster.configurationEx.vsanConfigInfo.enabled)
if cmp(desired_state, current_state) != 0: if cmp(desired_state, current_state) != 0:
return 'update' return 'update'
else: else:
return 'present' return 'present'
except vmodl.RuntimeFault as runtime_fault: except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg) self.module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault: except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg) self.module.fail_json(msg=method_fault.msg)
def main(): def main():
@ -240,23 +241,8 @@ 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')
cluster_states = { vmware_cluster = VMwareCluster(module)
'absent': { vmware_cluster.process_state()
'present': state_destroy_cluster,
'absent': state_exit_unchanged,
},
'present': {
'update': state_update_cluster,
'present': state_exit_unchanged,
'absent': state_create_cluster,
}
}
desired_state = module.params['state']
current_state = check_cluster_configuration(module)
# Based on the desired_state and the current_state call
# the appropriate method from the dictionary
cluster_states[desired_state][current_state](module)
from ansible.module_utils.vmware import * from ansible.module_utils.vmware import *
from ansible.module_utils.basic import * from ansible.module_utils.basic import *