1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

vmware_guest: Add support for Linked Clones (#23284)

* vmware_guest: Add support for Linked Clones

* Resolve PEP8 Issues

* Change snapshot_name to snapshot_src

* Add additional option 'snapshot_linked' to specify whether to use a linked clone or not

* Changed snapshot_linked parameter to "linked_clone"

* Remove unnecessary conditional
This commit is contained in:
Jimmy Conner 2017-05-16 08:26:51 -05:00 committed by ansibot
parent 61d68275e8
commit 2380e50408

View file

@ -104,6 +104,16 @@ options:
- Wait until vCenter detects an IP address for the VM - Wait until vCenter detects an IP address for the VM
- This requires vmware-tools (vmtoolsd) to properly work after creation - This requires vmware-tools (vmtoolsd) to properly work after creation
default: False default: False
snapshot_src:
description:
- Name of an existing snapshot to use to create a clone of a VM.
default: None
version_added: "2.4"
linked_clone:
description:
- Whether to create a Linked Clone from the snapshot specified
default: False
version_added: "2.4"
force: force:
description: description:
- Ignore warnings and complete the actions - Ignore warnings and complete the actions
@ -1096,10 +1106,22 @@ class PyVmomiHelper(object):
relospec.datastore = datastore relospec.datastore = datastore
relospec.pool = resource_pool relospec.pool = resource_pool
if self.params['snapshot_src'] is not None and self.params['linked_clone']:
relospec.diskMoveType = vim.vm.RelocateSpec.DiskMoveOptions.createNewChildDiskBacking
clonespec = vim.vm.CloneSpec(template=self.params['is_template'], location=relospec) clonespec = vim.vm.CloneSpec(template=self.params['is_template'], location=relospec)
if self.customspec: if self.customspec:
clonespec.customization = self.customspec clonespec.customization = self.customspec
if self.params['snapshot_src'] is not None:
snapshot = self.get_snapshots_by_name_recursively(snapshots=vm_obj.snapshot.rootSnapshotList,
snapname=self.params['snapshot_src'])
if len(snapshot) != 1:
self.module.fail_json(msg='virtual machine "{0}" does not contain snapshot named "{1}"'.format(
self.params['template'], self.params['snapshot_src']))
clonespec.snapshot = snapshot[0].snapshot
clonespec.config = self.configspec clonespec.config = self.configspec
task = vm_obj.Clone(folder=destfolder, name=self.params['name'], spec=clonespec) task = vm_obj.Clone(folder=destfolder, name=self.params['name'], spec=clonespec)
self.change_detected = True self.change_detected = True
@ -1142,6 +1164,15 @@ class PyVmomiHelper(object):
vm_facts = self.gather_facts(vm) vm_facts = self.gather_facts(vm)
return {'changed': self.change_detected, 'failed': False, 'instance': vm_facts} return {'changed': self.change_detected, 'failed': False, 'instance': vm_facts}
def get_snapshots_by_name_recursively(self, snapshots, snapname):
snap_obj = []
for snapshot in snapshots:
if snapshot.name == snapname:
snap_obj.append(snapshot)
else:
snap_obj = snap_obj + self.get_snapshots_by_name_recursively(snapshot.childSnapshotList, snapname)
return snap_obj
def reconfigure_vm(self): def reconfigure_vm(self):
self.configspec = vim.vm.ConfigSpec() self.configspec = vim.vm.ConfigSpec()
self.configspec.deviceChange = [] self.configspec.deviceChange = []
@ -1284,6 +1315,8 @@ def main():
esxi_hostname=dict(type='str'), esxi_hostname=dict(type='str'),
cluster=dict(type='str'), cluster=dict(type='str'),
wait_for_ip_address=dict(type='bool', default=False), wait_for_ip_address=dict(type='bool', default=False),
snapshot_src=dict(type='str', default=None),
linked_clone=dict(type='bool', default=False),
networks=dict(type='list', default=[]), networks=dict(type='list', default=[]),
resource_pool=dict(type='str'), resource_pool=dict(type='str'),
customization=dict(type='dict', no_log=True, default={}), customization=dict(type='dict', no_log=True, default={}),