add support to vmware_guest for template => vm conversion (#31607)

* add support to vmware_guest for template => vm conversion

While the vmware_guest currently supports conversion of VMs to templates
using the is_template argument, it does not support the inverse:
converting templates back into VMs.  This change adds that
functionality.

When converting a template back into a VM, the extra config option
"uuid.action" is also set so that VMware will automatically create a new
UUID for the converted VM.  If the "uuid.action" setting is already
configured, it will not be modified.  Setting this prevents an
interactive question from being raised when attempting to boot the VM.

* Add integration tests for vmware_guest is_template

* Add additional idempotency test for vmware_guest is_template
This commit is contained in:
Jim Gu 2017-10-31 09:12:40 -04:00 committed by jctanner
parent e3a847a142
commit d23da2e494
2 changed files with 135 additions and 0 deletions

View file

@ -1437,6 +1437,30 @@ class PyVmomiHelper(PyVmomi):
self.current_vm_obj.MarkAsTemplate()
change_applied = True
# Mark Template as VM
elif not self.params['is_template'] and self.current_vm_obj.config.template:
if self.params['resource_pool']:
resource_pool = self.select_resource_pool_by_name(self.params['resource_pool'])
if resource_pool is None:
self.module.fail_json(msg='Unable to find resource pool "%(resource_pool)s"' % self.params)
self.current_vm_obj.MarkAsVirtualMachine(pool=resource_pool)
# Automatically update VMWare UUID when converting template to VM.
# This avoids an interactive prompt during VM startup.
uuid_action = [x for x in self.current_vm_obj.config.extraConfig if x.key == "uuid.action"]
if not uuid_action:
uuid_action_opt = vim.option.OptionValue()
uuid_action_opt.key = "uuid.action"
uuid_action_opt.value = "create"
self.configspec.extraConfig.append(uuid_action_opt)
self.change_detected = True
change_applied = True
else:
self.module.fail_json(msg="Resource pool must be specified when converting template to VM!")
vm_facts = self.gather_facts(self.current_vm_obj)
return {'changed': change_applied, 'failed': False, 'instance': vm_facts}