diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index eb9821d9311..a32187ebdc4 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -2198,27 +2198,44 @@ class PyVmomiHelper(PyVmomi): # 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 + resource_pool = self.get_resource_pool() + kwargs = dict(pool=resource_pool) - change_applied = True - else: - self.module.fail_json(msg="Resource pool must be specified when converting template to VM!") + if self.params.get('esxi_hostname', None): + host_system_obj = self.select_host() + kwargs.update(host=host_system_obj) + + try: + self.current_vm_obj.MarkAsVirtualMachine(**kwargs) + except vim.fault.InvalidState as invalid_state: + self.module.fail_json(msg="Virtual machine is not marked" + " as template : %s" % to_native(invalid_state.msg)) + except vim.fault.InvalidDatastore as invalid_ds: + self.module.fail_json(msg="Converting template to virtual machine" + " operation cannot be performed on the" + " target datastores: %s" % to_native(invalid_ds.msg)) + except vim.fault.CannotAccessVmComponent as cannot_access: + self.module.fail_json(msg="Failed to convert template to virtual machine" + " as operation unable access virtual machine" + " component: %s" % to_native(cannot_access.msg)) + except vmodl.fault.InvalidArgument as invalid_argument: + self.module.fail_json(msg="Failed to convert template to virtual machine" + " due to : %s" % to_native(invalid_argument.msg)) + except Exception as generic_exc: + self.module.fail_json(msg="Failed to convert template to virtual machine" + " due to generic error : %s" % to_native(generic_exc)) + + # 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 vm_facts = self.gather_facts(self.current_vm_obj) return {'changed': change_applied, 'failed': False, 'instance': vm_facts}