Skip to content

Commit

Permalink
VMware: Fix custom value in vmware_guest (ansible#58157)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde authored Jul 24, 2019
1 parent 5108353 commit bafd06b
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions lib/ansible/modules/cloud/vmware/vmware_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,30 +1521,28 @@ def configure_vapp_properties(self, vm_obj):
self.configspec.vAppConfig = new_vmconfig_spec
self.change_detected = True

def customize_customvalues(self, vm_obj, config_spec):
def customize_customvalues(self, vm_obj):
if len(self.params['customvalues']) == 0:
return

vm_custom_spec = config_spec
vm_custom_spec.extraConfig = []

changed = False
facts = self.gather_facts(vm_obj)
for kv in self.params['customvalues']:
if 'key' not in kv or 'value' not in kv:
self.module.exit_json(msg="customvalues items required both 'key' and 'value fields.")
self.module.exit_json(msg="customvalues items required both 'key' and 'value' fields.")

# If kv is not kv fetched from facts, change it
if kv['key'] not in facts['customvalues'] or facts['customvalues'][kv['key']] != kv['value']:
option = vim.option.OptionValue()
option.key = kv['key']
option.value = kv['value']
key_id = None
for field in self.content.customFieldsManager.field:
if field.name == kv['key']:
key_id = field.key
break

vm_custom_spec.extraConfig.append(option)
changed = True
if not key_id:
self.module.fail_json(msg="Unable to find custom value key %s" % kv['key'])

if changed:
self.change_detected = True
# If kv is not kv fetched from facts, change it
if kv['key'] not in facts['customvalues'] or facts['customvalues'][kv['key']] != kv['value']:
self.content.customFieldsManager.SetField(entity=vm_obj, key=key_id, value=kv['value'])
self.change_detected = True

def customize_vm(self, vm_obj):

Expand Down Expand Up @@ -2316,12 +2314,7 @@ def deploy_vm(self):
return {'changed': self.change_applied, 'failed': True, 'msg': task.info.error.msg, 'op': 'annotation'}

if self.params['customvalues']:
vm_custom_spec = vim.vm.ConfigSpec()
self.customize_customvalues(vm_obj=vm, config_spec=vm_custom_spec)
task = vm.ReconfigVM_Task(vm_custom_spec)
self.wait_for_task(task)
if task.info.state == 'error':
return {'changed': self.change_applied, 'failed': True, 'msg': task.info.error.msg, 'op': 'customvalues'}
self.customize_customvalues(vm_obj=vm)

if self.params['wait_for_ip_address'] or self.params['wait_for_customization'] or self.params['state'] in ['poweredon', 'restarted']:
set_vm_power_state(self.content, vm, 'poweredon', force=False)
Expand Down Expand Up @@ -2359,7 +2352,7 @@ def reconfigure_vm(self):
self.configure_disks(vm_obj=self.current_vm_obj)
self.configure_network(vm_obj=self.current_vm_obj)
self.configure_cdrom(vm_obj=self.current_vm_obj)
self.customize_customvalues(vm_obj=self.current_vm_obj, config_spec=self.configspec)
self.customize_customvalues(vm_obj=self.current_vm_obj)
self.configure_resource_alloc_info(vm_obj=self.current_vm_obj)
self.configure_vapp_properties(vm_obj=self.current_vm_obj)

Expand Down

0 comments on commit bafd06b

Please sign in to comment.