Merge pull request #1620 from jcpowermac/vmware_cluster_fix_20
Resolves issue with vmware_cluster module for v2.0
This commit is contained in:
commit
18c654b90e
1 changed files with 135 additions and 149 deletions
|
@ -77,152 +77,153 @@ except ImportError:
|
||||||
HAS_PYVMOMI = False
|
HAS_PYVMOMI = False
|
||||||
|
|
||||||
|
|
||||||
def configure_ha(enable_ha):
|
class VMwareCluster(object):
|
||||||
das_config = vim.cluster.DasConfigInfo()
|
def __init__(self, module):
|
||||||
das_config.enabled = enable_ha
|
self.module = module
|
||||||
das_config.admissionControlPolicy = vim.cluster.FailoverLevelAdmissionControlPolicy()
|
self.enable_ha = module.params['enable_ha']
|
||||||
das_config.admissionControlPolicy.failoverLevel = 2
|
self.enable_drs = module.params['enable_drs']
|
||||||
return das_config
|
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_drs(enable_drs):
|
def configure_ha(self):
|
||||||
drs_config = vim.cluster.DrsConfigInfo()
|
das_config = vim.cluster.DasConfigInfo()
|
||||||
drs_config.enabled = enable_drs
|
das_config.enabled = self.enable_ha
|
||||||
# Set to partially automated
|
das_config.admissionControlPolicy = vim.cluster.FailoverLevelAdmissionControlPolicy()
|
||||||
drs_config.vmotionRate = 3
|
das_config.admissionControlPolicy.failoverLevel = 2
|
||||||
return drs_config
|
return das_config
|
||||||
|
|
||||||
|
def configure_drs(self):
|
||||||
|
drs_config = vim.cluster.DrsConfigInfo()
|
||||||
|
drs_config.enabled = self.enable_drs
|
||||||
|
# Set to partially automated
|
||||||
|
drs_config.vmotionRate = 3
|
||||||
|
return drs_config
|
||||||
|
|
||||||
def configure_vsan(enable_vsan):
|
def configure_vsan(self):
|
||||||
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):
|
||||||
|
try:
|
||||||
|
cluster_config_spec = vim.cluster.ConfigSpecEx()
|
||||||
|
cluster_config_spec.dasConfig = self.configure_ha()
|
||||||
|
cluster_config_spec.drsConfig = self.configure_drs()
|
||||||
|
if self.enable_vsan:
|
||||||
|
cluster_config_spec.vsanConfig = self.configure_vsan()
|
||||||
|
if not self.module.check_mode:
|
||||||
|
self.datacenter.hostFolder.CreateClusterEx(self.cluster_name, cluster_config_spec)
|
||||||
|
self.module.exit_json(changed=True)
|
||||||
|
except vim.fault.DuplicateName:
|
||||||
|
self.module.fail_json(msg="A cluster with the name %s already exists" % self.cluster_name)
|
||||||
|
except vmodl.fault.InvalidArgument:
|
||||||
|
self.module.fail_json(msg="Cluster configuration specification parameter is invalid")
|
||||||
|
except vim.fault.InvalidName:
|
||||||
|
self.module.fail_json(msg="%s is an invalid name for a cluster" % self.cluster_name)
|
||||||
|
except vmodl.fault.NotSupported:
|
||||||
|
# This should never happen
|
||||||
|
self.module.fail_json(msg="Trying to create a cluster on an incorrect folder object")
|
||||||
|
except vmodl.RuntimeFault as runtime_fault:
|
||||||
|
self.module.fail_json(msg=runtime_fault.msg)
|
||||||
|
except vmodl.MethodFault as method_fault:
|
||||||
|
# This should never happen either
|
||||||
|
self.module.fail_json(msg=method_fault.msg)
|
||||||
|
|
||||||
def state_create_cluster(module):
|
def state_destroy_cluster(self):
|
||||||
|
changed = True
|
||||||
|
result = None
|
||||||
|
|
||||||
enable_ha = module.params['enable_ha']
|
try:
|
||||||
enable_drs = module.params['enable_drs']
|
if not self.module.check_mode:
|
||||||
enable_vsan = module.params['enable_vsan']
|
task = self.cluster.Destroy_Task()
|
||||||
cluster_name = module.params['cluster_name']
|
changed, result = wait_for_task(task)
|
||||||
datacenter = module.params['datacenter']
|
self.module.exit_json(changed=changed, result=result)
|
||||||
|
except vim.fault.VimFault as vim_fault:
|
||||||
|
self.module.fail_json(msg=vim_fault.msg)
|
||||||
|
except vmodl.RuntimeFault as runtime_fault:
|
||||||
|
self.module.fail_json(msg=runtime_fault.msg)
|
||||||
|
except vmodl.MethodFault as method_fault:
|
||||||
|
self.module.fail_json(msg=method_fault.msg)
|
||||||
|
|
||||||
try:
|
def state_exit_unchanged(self):
|
||||||
|
self.module.exit_json(changed=False)
|
||||||
|
|
||||||
|
def state_update_cluster(self):
|
||||||
cluster_config_spec = vim.cluster.ConfigSpecEx()
|
cluster_config_spec = vim.cluster.ConfigSpecEx()
|
||||||
cluster_config_spec.dasConfig = configure_ha(enable_ha)
|
changed = True
|
||||||
cluster_config_spec.drsConfig = configure_drs(enable_drs)
|
result = None
|
||||||
if enable_vsan:
|
|
||||||
cluster_config_spec.vsanConfig = configure_vsan(enable_vsan)
|
|
||||||
if not module.check_mode:
|
|
||||||
datacenter.hostFolder.CreateClusterEx(cluster_name, cluster_config_spec)
|
|
||||||
module.exit_json(changed=True)
|
|
||||||
except vim.fault.DuplicateName:
|
|
||||||
module.fail_json(msg="A cluster with the name %s already exists" % cluster_name)
|
|
||||||
except vmodl.fault.InvalidArgument:
|
|
||||||
module.fail_json(msg="Cluster configuration specification parameter is invalid")
|
|
||||||
except vim.fault.InvalidName:
|
|
||||||
module.fail_json(msg="%s is an invalid name for a cluster" % cluster_name)
|
|
||||||
except vmodl.fault.NotSupported:
|
|
||||||
# This should never happen
|
|
||||||
module.fail_json(msg="Trying to create a cluster on an incorrect folder object")
|
|
||||||
except vmodl.RuntimeFault as runtime_fault:
|
|
||||||
module.fail_json(msg=runtime_fault.msg)
|
|
||||||
except vmodl.MethodFault as method_fault:
|
|
||||||
# This should never happen either
|
|
||||||
module.fail_json(msg=method_fault.msg)
|
|
||||||
|
|
||||||
|
if self.cluster.configurationEx.dasConfig.enabled != self.enable_ha:
|
||||||
|
cluster_config_spec.dasConfig = self.configure_ha()
|
||||||
|
if self.cluster.configurationEx.drsConfig.enabled != self.enable_drs:
|
||||||
|
cluster_config_spec.drsConfig = self.configure_drs()
|
||||||
|
if self.cluster.configurationEx.vsanConfigInfo.enabled != self.enable_vsan:
|
||||||
|
cluster_config_spec.vsanConfig = self.configure_vsan()
|
||||||
|
|
||||||
def state_destroy_cluster(module):
|
try:
|
||||||
cluster = module.params['cluster']
|
if not self.module.check_mode:
|
||||||
changed = True
|
task = self.cluster.ReconfigureComputeResource_Task(cluster_config_spec, True)
|
||||||
result = None
|
changed, result = wait_for_task(task)
|
||||||
|
self.module.exit_json(changed=changed, result=result)
|
||||||
|
except vmodl.RuntimeFault as runtime_fault:
|
||||||
|
self.module.fail_json(msg=runtime_fault.msg)
|
||||||
|
except vmodl.MethodFault as method_fault:
|
||||||
|
self.module.fail_json(msg=method_fault.msg)
|
||||||
|
except TaskError as task_e:
|
||||||
|
self.module.fail_json(msg=str(task_e))
|
||||||
|
|
||||||
try:
|
def check_cluster_configuration(self):
|
||||||
if not module.check_mode:
|
try:
|
||||||
task = cluster.Destroy_Task()
|
self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name)
|
||||||
changed, result = wait_for_task(task)
|
if self.datacenter is None:
|
||||||
module.exit_json(changed=changed, result=result)
|
self.module.fail_json(msg="Datacenter %s does not exist, "
|
||||||
except vim.fault.VimFault as vim_fault:
|
"please create first with Ansible Module vmware_datacenter or manually."
|
||||||
module.fail_json(msg=vim_fault.msg)
|
% self.datacenter_name)
|
||||||
except vmodl.RuntimeFault as runtime_fault:
|
self.cluster = find_cluster_by_name_datacenter(self.datacenter, self.cluster_name)
|
||||||
module.fail_json(msg=runtime_fault.msg)
|
|
||||||
except vmodl.MethodFault as method_fault:
|
|
||||||
module.fail_json(msg=method_fault.msg)
|
|
||||||
|
|
||||||
|
if self.cluster is None:
|
||||||
def state_exit_unchanged(module):
|
return 'absent'
|
||||||
module.exit_json(changed=False)
|
|
||||||
|
|
||||||
|
|
||||||
def state_update_cluster(module):
|
|
||||||
|
|
||||||
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
|
|
||||||
result = None
|
|
||||||
|
|
||||||
if cluster.configurationEx.dasConfig.enabled != enable_ha:
|
|
||||||
cluster_config_spec.dasConfig = configure_ha(enable_ha)
|
|
||||||
if cluster.configurationEx.drsConfig.enabled != enable_drs:
|
|
||||||
cluster_config_spec.drsConfig = configure_drs(enable_drs)
|
|
||||||
if cluster.configurationEx.vsanConfigInfo.enabled != enable_vsan:
|
|
||||||
cluster_config_spec.vsanConfig = configure_vsan(enable_vsan)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if not module.check_mode:
|
|
||||||
task = cluster.ReconfigureComputeResource_Task(cluster_config_spec, True)
|
|
||||||
changed, result = wait_for_task(task)
|
|
||||||
module.exit_json(changed=changed, result=result)
|
|
||||||
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)
|
|
||||||
except TaskError as task_e:
|
|
||||||
module.fail_json(msg=str(task_e))
|
|
||||||
|
|
||||||
|
|
||||||
def check_cluster_configuration(module):
|
|
||||||
datacenter_name = module.params['datacenter_name']
|
|
||||||
cluster_name = module.params['cluster_name']
|
|
||||||
|
|
||||||
try:
|
|
||||||
content = connect_to_api(module)
|
|
||||||
datacenter = find_datacenter_by_name(content, datacenter_name)
|
|
||||||
if datacenter is None:
|
|
||||||
module.fail_json(msg="Datacenter %s does not exist, "
|
|
||||||
"please create first with Ansible Module vmware_datacenter or manually."
|
|
||||||
% datacenter_name)
|
|
||||||
cluster = find_cluster_by_name_datacenter(datacenter, cluster_name)
|
|
||||||
|
|
||||||
module.params['content'] = content
|
|
||||||
module.params['datacenter'] = datacenter
|
|
||||||
|
|
||||||
if cluster is None:
|
|
||||||
return 'absent'
|
|
||||||
else:
|
|
||||||
module.params['cluster'] = cluster
|
|
||||||
|
|
||||||
desired_state = (module.params['enable_ha'],
|
|
||||||
module.params['enable_drs'],
|
|
||||||
module.params['enable_vsan'])
|
|
||||||
|
|
||||||
current_state = (cluster.configurationEx.dasConfig.enabled,
|
|
||||||
cluster.configurationEx.drsConfig.enabled,
|
|
||||||
cluster.configurationEx.vsanConfigInfo.enabled)
|
|
||||||
|
|
||||||
if cmp(desired_state, current_state) != 0:
|
|
||||||
return 'update'
|
|
||||||
else:
|
else:
|
||||||
return 'present'
|
desired_state = (self.enable_ha,
|
||||||
except vmodl.RuntimeFault as runtime_fault:
|
self.enable_drs,
|
||||||
module.fail_json(msg=runtime_fault.msg)
|
self.enable_vsan)
|
||||||
except vmodl.MethodFault as method_fault:
|
|
||||||
module.fail_json(msg=method_fault.msg)
|
current_state = (self.cluster.configurationEx.dasConfig.enabled,
|
||||||
|
self.cluster.configurationEx.drsConfig.enabled,
|
||||||
|
self.cluster.configurationEx.vsanConfigInfo.enabled)
|
||||||
|
|
||||||
|
if cmp(desired_state, current_state) != 0:
|
||||||
|
return 'update'
|
||||||
|
else:
|
||||||
|
return 'present'
|
||||||
|
except vmodl.RuntimeFault as runtime_fault:
|
||||||
|
self.module.fail_json(msg=runtime_fault.msg)
|
||||||
|
except vmodl.MethodFault as method_fault:
|
||||||
|
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 *
|
||||||
|
|
Loading…
Reference in a new issue