Support creating new VMs by cloning an existing VM. Support setting power state of cloned VMs.

pull/18777/head
brannon 9 years ago committed by Matt Clay
parent 0fff871819
commit 8691d56f3c

@ -99,6 +99,12 @@ options:
version_added: "2.0" version_added: "2.0"
required: false required: false
default: none default: none
power_on_after_clone:
description:
- Specifies if the VM should be powered on after the clone.
required: false
default: yes
choices: ['yes', 'no']
vm_disk: vm_disk:
description: description:
- A key, value list of disks and their sizes and which datastore to keep it in. - A key, value list of disks and their sizes and which datastore to keep it in.
@ -587,7 +593,7 @@ def vmdisk_id(vm, current_datastore_name):
return id_list return id_list
def deploy_template(vsphere_client, guest, resource_pool, template_src, esxi, module, cluster_name, snapshot_to_clone): def deploy_template(vsphere_client, guest, resource_pool, template_src, esxi, module, cluster_name, snapshot_to_clone, power_on_after_clone):
vmTemplate = vsphere_client.get_vm_by_name(template_src) vmTemplate = vsphere_client.get_vm_by_name(template_src)
vmTarget = None vmTarget = None
@ -664,20 +670,25 @@ def deploy_template(vsphere_client, guest, resource_pool, template_src, esxi, mo
vmTarget = vsphere_client.get_vm_by_name(guest) vmTarget = vsphere_client.get_vm_by_name(guest)
except Exception: except Exception:
pass pass
if not vmTemplate.properties.config.template:
if not vmTemplate.is_powered_off():
module.fail_json( module.fail_json(
msg="Target %s is not a registered template" % template_src msg="Source %s must be powered off" % template_src
) )
try: try:
if vmTarget: if not vmTarget:
changed = False cloneArgs = dict(resourcepool=rpmor, power_on=power_on_after_clone)
elif snapshot_to_clone is not None:
#check if snapshot_to_clone is specified, Create a Linked Clone instead of a full clone. if snapshot_to_clone is not None:
vmTemplate.clone(guest, resourcepool=rpmor, linked=True, snapshot=snapshot_to_clone) #check if snapshot_to_clone is specified, Create a Linked Clone instead of a full clone.
cloneArgs["linked"] = True
cloneArgs["snapshot"] = snapshot_to_clone
vmTemplate.clone(guest, **cloneArgs)
changed = True changed = True
else: else:
vmTemplate.clone(guest, resourcepool=rpmor) changed = False
changed = True
vsphere_client.disconnect() vsphere_client.disconnect()
module.exit_json(changed=changed) module.exit_json(changed=changed)
@ -1317,6 +1328,7 @@ def main():
cluster=dict(required=False, default=None, type='str'), cluster=dict(required=False, default=None, type='str'),
force=dict(required=False, type='bool', default=False), force=dict(required=False, type='bool', default=False),
esxi=dict(required=False, type='dict', default={}), esxi=dict(required=False, type='dict', default={}),
power_on_after_clone=dict(required=False, type='bool', default=True)
), ),
@ -1356,6 +1368,7 @@ def main():
template_src = module.params['template_src'] template_src = module.params['template_src']
from_template = module.params['from_template'] from_template = module.params['from_template']
snapshot_to_clone = module.params['snapshot_to_clone'] snapshot_to_clone = module.params['snapshot_to_clone']
power_on_after_clone = module.params['power_on_after_clone']
# CONNECT TO THE SERVER # CONNECT TO THE SERVER
@ -1437,8 +1450,10 @@ def main():
template_src=template_src, template_src=template_src,
module=module, module=module,
cluster_name=cluster, cluster_name=cluster,
snapshot_to_clone=snapshot_to_clone snapshot_to_clone=snapshot_to_clone,
power_on_after_clone=power_on_after_clone
) )
if state in ['restarted', 'reconfigured']: if state in ['restarted', 'reconfigured']:
module.fail_json( module.fail_json(
msg="No such VM %s. States [" msg="No such VM %s. States ["

Loading…
Cancel
Save