Python Example of Creating a VM Template in a Content Library from a Virtual Machine

This example shows how you can create a VM template from a virtual machine and add the template to a content library by using the
vSphere Automation
API. The example is based on the
create_vm_template.py
sample.
For a complete and up-to-date version of the sample code, see the
vsphere-automation-sdk-python
VMware repository at GitHub.
class CreateVmTemplate(SampleBase): ... def _setup(self): # Required arguments self.vm_name = self.args.vmname self.datacenter_name = self.args.datacentername self.resource_pool_name = self.args.resourcepoolname self.datastore_name = self.args.datastorename # Optional arguments self.item_name = (self.args.itemname if self.args.itemname else rand('vmtx-item-')) self.servicemanager = self.get_service_manager() self.client = ClsApiClient(self.servicemanager) self.helper = ClsApiHelper(self.client, self.skip_verification) session = get_unverified_session() if self.skip_verification else None self.vsphere_client = create_vsphere_client(server=self.server, username=self.username, password=self.password, session=session) def _execute(self): # Get the identifiers for the virtual machine and resource pool vm_id = get_vm(self.vsphere_client, self.vm_name) assert vm_id resource_pool_id = get_resource_pool(self.vsphere_client, self.datacenter_name, self.resource_pool_name) assert resource_pool_id # Create a local content library storage_backings = self.helper.create_storage_backings(self.servicemanager, self.datastore_name) self.library_id = self.helper.create_local_library(storage_backings, self.library_name) # Build the library item create specification create_spec = VmtxLibraryItem.CreateSpec() create_spec.source_vm = vm_id create_spec.library = self.library_id create_spec.name = self.item_name create_spec.placement = VmtxLibraryItem.CreatePlacementSpec(resource_pool=resource_pool_id) # Create a new library item from the source virtual machine self.item_id = self.client.vmtx_service.create(create_spec) ...