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
pull/32397/head
Jim Gu 7 years ago committed by jctanner
parent e3a847a142
commit d23da2e494

@ -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}

@ -0,0 +1,111 @@
- name: Wait for Flask controller to come up online
wait_for:
host: "{{ vcsim }}"
port: 5000
state: started
- name: kill vcsim
uri:
url: "http://{{ vcsim }}:5000/killall"
- name: start vcsim with no folders
uri:
url: "http://{{ vcsim }}:5000/spawn?datacenter=1&cluster=1&folder=0"
register: vcsim_instance
- name: Wait for Flask controller to come up online
wait_for:
host: "{{ vcsim }}"
port: 443
state: started
- name: get a list of VMs from vcsim
uri:
url: "http://{{ vcsim }}:5000/govc_find?filter=VM"
register: vmlist
- debug: var=vcsim_instance
- debug: var=vmlist
- name: ensure that VMs are not flagged as templates
vmware_guest:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ (item|basename).split('_')[0] }}"
folder: "{{ item|dirname }}"
name: "{{ item|basename }}"
state: present
is_template: False
with_items: "{{ vmlist['json'] }}"
register: no_template_initial
- debug: var=no_template_initial
- name: ensure no changes were made
assert:
that:
- "no_template_initial.results|map(attribute='changed')|unique|list == [False]"
- name: convert all VMs to templates
vmware_guest:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ (item|basename).split('_')[0] }}"
folder: "{{ item|dirname }}"
name: "{{ item|basename }}"
state: present
is_template: True
with_items: "{{ vmlist['json'] }}"
register: convert_to_template
- debug: var=convert_to_template
- name: ensure that changes were made
assert:
that:
- "convert_to_template.results|map(attribute='changed')|unique|list == [True]"
- name: make double sure that all VMs are templates
vmware_guest:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ (item|basename).split('_')[0] }}"
folder: "{{ item|dirname }}"
name: "{{ item|basename }}"
state: present
is_template: True
with_items: "{{ vmlist['json'] }}"
register: still_templates
- debug: var=still_templates
- name: ensure that no changes were made
assert:
that:
- "still_templates.results|map(attribute='changed')|unique|list == [False]"
- name: convert all templates back to VMs
vmware_guest:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ (item|basename).split('_')[0] }}"
folder: "{{ item|dirname }}"
name: "{{ item|basename }}"
state: present
is_template: False
with_items: "{{ vmlist['json'] }}"
register: revert_to_vm
- debug: var=revert_to_vm
- name: ensure that changes were made
assert:
that:
- "revert_to_vm.results|map(attribute='changed')|unique|list == [True]"
Loading…
Cancel
Save