Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/ansible/modules/cloud/vmware/vmware_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,30 @@ def reconfigure_vm(self):
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}

Expand Down
111 changes: 111 additions & 0 deletions test/integration/targets/vmware_guest/tasks/template_d1_c1_f0.yml
Original file line number Diff line number Diff line change
@@ -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]"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add one more test where all VMs are "again" converted to template to ensure idempotency of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull has been updated with new idempotency test.

- 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]"