def deploy_ovf_template(self, lib_item_id, ovf_summary, deployment_target): # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template result = self.client.ovf_lib_item_service.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print( 'Deployment successful. Result resource: {0}, ID: {1}'.format( result.resource_id.type, result.resource_id.id)) self.vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) # Power on the VM and wait for the power on operation to be completed self.vm_obj = get_obj_by_moId(self.servicemanager.content, [vim.VirtualMachine], self.vm_id) assert self.vm_obj is not None poweron_vm(self.servicemanager.content, self.vm_obj) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message))
def deploy_ovf_template(self, lib_item_id, ovf_summary, deployment_target, vm_name): # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=vm_name, annotation=ovf_summary.annotation, accept_all_eula=True) # Deploy the ovf template result = self.client.ovf_lib_item_service.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) if result.succeeded: vm_id = result.resource_id.id return vm_id else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message)) raise Exception('OVF deploy failed.')
def deploy_vm_from_ovf_template(self): # Find the datacenter by the given datacenter name self.datacenter_id = self.get_datacenter_by_name( datacenter_name=self.datacenter) if not self.datacenter_id: self.module.fail_json(msg="Failed to find the datacenter %s" % self.datacenter) # Find the datastore by the given datastore name self.datastore_id = self.get_datastore_by_name(self.datacenter, self.datastore) if not self.datastore_id: self.module.fail_json(msg="Failed to find the datastore %s" % self.datastore) # Find the LibraryItem (Template) by the given LibraryItem name self.library_item_id = self.get_library_item_by_name( self.ovf_template_name) if not self.library_item_id: self.module.fail_json(msg="Failed to find the library Item %s" % self.ovf_template_name) # Find the folder by the given folder name self.folder_id = self.get_folder_by_name(self.datacenter, self.folder) if not self.folder_id: self.module.fail_json(msg="Failed to find the folder %s" % self.folder) # Find the Host by given HostName self.host_id = self.get_host_by_name(self.datacenter, self.host) if not self.host_id: self.module.fail_json(msg="Failed to find the Host %s" % self.host) # Find the resourcepool by the given resourcepool name self.resourcepool_id = None if self.resourcepool: self.resourcepool_id = self.get_resource_pool_by_name( self.datacenter, self.resourcepool) if not self.resourcepool_id: self.module.fail_json( msg="Failed to find the resource_pool %s" % self.resourcepool) # Find the Cluster by the given Cluster name self.cluster_id = None if self.cluster: self.cluster_id = self.get_cluster_by_name(self.datacenter, self.cluster) if not self.cluster_id: self.module.fail_json(msg="Failed to find the Cluster %s" % self.cluster) deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=self.resourcepool_id, folder_id=self.folder_id) self.ovf_summary = self.api_client.vcenter.ovf.LibraryItem.filter( ovf_library_item_id=self.library_item_id, target=deployment_target) self.deploy_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=self.ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=self.storage_provisioning, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) result = self.api_client.vcenter.ovf.LibraryItem.deploy( self.library_item_id, deployment_target, self.deploy_spec) if result.succeeded: self.module.exit_json(changed=True, vm_deploy_info=dict( msg="Deployed Virtual Machine '%s'." % self.vm_name, vm_id=result.resource_id.id, )) self.module.exit_json(changed=False, vm_deploy_info=dict( msg="Virtual Machine deployment failed", vm_id=''))
def deploy_ovf_template(self): # Build the deployment target with resource pool ID and folder ID rp_filter_spec = ResourcePool.FilterSpec(names=set([self.resourcepoolname])) resource_pool_summaries = self.client.vcenter.ResourcePool.list(rp_filter_spec) if not resource_pool_summaries: raise ValueError("Resource pool with name '{}' not found". format(self.resourcepoolname)) resource_pool_id = resource_pool_summaries[0].resource_pool print('Resource pool ID: {}'.format(resource_pool_id)) folder_filter_spec = Folder.FilterSpec(names=set([self.foldername])) folder_summaries = self.client.vcenter.Folder.list(folder_filter_spec) if not folder_summaries: raise ValueError("Folder with name '{}' not found". format(self.foldername)) folder_id = folder_summaries[0].folder print('Folder ID: {}'.format(folder_id)) deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=resource_pool_id, folder_id=folder_id ) # Find the library item find_spec = Item.FindSpec(name=self.lib_item_name) lib_item_ids = self.client.content.library.Item.find(find_spec) if not lib_item_ids: raise ValueError("Library item with name '{}' not found". format(self.lib_item_name)) lib_item_id = lib_item_ids[0] print('Library item ID: {}'.format(lib_item_id)) ovf_summary = self.client.vcenter.ovf.LibraryItem.filter( ovf_library_item_id=lib_item_id, target=deployment_target) print('Found an OVF template: {} to deploy.'.format(ovf_summary.name)) # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template result = self.client.vcenter.ovf.LibraryItem.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print('Deployment successful. VM Name: "{}", ID: "{}"' .format(self.vm_name, result.resource_id.id)) self.vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message)) # Add an opaque network portgroup to the deployed VM if self.opaquenetworkname: filter = Network.FilterSpec( names=set([self.opaquenetworkname]), types=set([Network.Type.OPAQUE_NETWORK])) network_summaries = self.client.vcenter.Network.list(filter=filter) if not network_summaries: raise ValueError("Opaque network {} can not find".format( self.opaquenetworkname)) network = network_summaries[0].network nic_create_spec = Ethernet.CreateSpec( start_connected=True, mac_type=Ethernet.MacAddressType.GENERATED, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.OPAQUE_NETWORK, network=network)) print('vm.hardware.Ethernet.create({}, {}) -> {}'.format( self.vm_id, nic_create_spec, network)) nic = self.client.vcenter.vm.hardware.Ethernet.create( self.vm_id, nic_create_spec) nic_info = self.client.vcenter.vm.hardware.Ethernet.get(self.vm_id, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( self.vm_id, nic, pp(nic_info)))
def deploy_kmip_node(self, spec, debug=False): # Check if we know about the deployment target yet if self.vcs_cluster_obj is None: # Find the cluster's resource pool moid print() print("Obtaining the cluster's resource pool moid...") self.vcs_cluster_obj = get_obj(self.service_manager.content, [vim.ClusterComputeResource], self.deploy_to_cluster) assert self.vcs_cluster_obj is not None if debug: print(" --> Cluster: {0}".format(self.vcs_cluster_obj)) # Get a deployment target resource pool from the Cluster Object deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=self.vcs_cluster_obj.resourcePool._GetMoId()) if debug: print(" --> Resource Pool Moref: {0}".format( self.vcs_cluster_obj.resourcePool._GetMoId())) # Find lib item id from given item name find_spec = Item.FindSpec() find_spec.name = self.selected_item["name"] item_ids = self.sm_client.library_item_service.find(find_spec) assert (item_ids is not None and len(item_ids) > 0), ( 'No items found with name: {0}'.format(self.selected_item["name"])) lib_item_id = item_ids[0] ovf_summary = self.sm_client.ovf_lib_item_service.filter( ovf_library_item_id=lib_item_id, target=deployment_target) print() print('Deploying OVF template: {0} to cluster: {1}...'.format( ovf_summary.name, self.deploy_to_cluster)) # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=spec.name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template print("Deploying HyTrust OVF Template -- please wait...") print() result = self.sm_client.ovf_lib_item_service.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print('>>> Deployment successful.') if debug: print('Result resource: {0}, ID: {1}'.format( result.resource_id.type, result.resource_id.id)) error = result.error # if error is not None: # for warning in error.warnings: # print('OVF warning: {}'.format(warning.message)) # Get the vm object if spec.is_primary(): self.primary_node_id = result.resource_id.id self.primary_node_obj = get_obj_by_moId( self.service_manager.content, [vim.VirtualMachine], self.primary_node_id) assert self.primary_node_obj is not None vm_obj = self.primary_node_obj vm_id = self.primary_node_id else: self.secondary_node_id = result.resource_id.id self.secondary_node_obj = get_obj_by_moId( self.service_manager.content, [vim.VirtualMachine], self.secondary_node_id) assert self.secondary_node_obj is not None vm_obj = self.secondary_node_obj vm_id = self.secondary_node_id # Invoke the SmartConnect() method by supplying the host name, user name, and password. self.service_instance_stub = SmartConnect(host=self.web_svcs_host, user=self.sso_user, pwd=self.sso_pass) atexit.register(connect.Disconnect, self.service_instance_stub) content = self.service_instance_stub.RetrieveContent() if debug: print("VM UUID: {0}".format(vm_obj.summary.config.uuid)) if spec.is_primary(): self.primary_node_vm = content.searchIndex.FindByUuid( None, vm_obj.summary.config.uuid, True) vm = self.primary_node_vm else: self.secondary_node_vm = content.searchIndex.FindByUuid( None, vm_obj.summary.config.uuid, True) vm = self.secondary_node_vm print() print("Setting vApp Options on the VM...") vapp_spec_list = list() # Domain Name vapp_domainName = vim.vApp.PropertyInfo() vapp_domainName.key = 0 vapp_domainName.value = spec.domain vapp_spec1 = vim.vApp.PropertySpec() vapp_spec1.info = vapp_domainName vapp_spec1.operation = 'edit' vapp_spec_list.append(vapp_spec1) # Netmask vapp_netmask = vim.vApp.PropertyInfo() vapp_netmask.key = 1 vapp_netmask.value = spec.subnet vapp_spec2 = vim.vApp.PropertySpec() vapp_spec2.info = vapp_netmask vapp_spec2.operation = 'edit' vapp_spec_list.append(vapp_spec2) # KCMaster vapp_kcmaster = vim.vApp.PropertyInfo() vapp_kcmaster.key = 2 if spec.is_primary(): vapp_kcmaster.value = '' else: vapp_kcmaster.userConfigurable = True vapp_kcmaster.value = self.primary_node_spec.ip vapp_spec3 = vim.vApp.PropertySpec() vapp_spec3.info = vapp_kcmaster vapp_spec3.operation = 'edit' vapp_spec_list.append(vapp_spec3) # Console Password vapp_consolepw = vim.vApp.PropertyInfo() vapp_consolepw.key = 3 vapp_consolepw.userConfigurable = True vapp_consolepw.value = spec.consolepw vapp_spec4 = vim.vApp.PropertySpec() vapp_spec4.info = vapp_consolepw vapp_spec4.operation = 'edit' vapp_spec_list.append(vapp_spec4) # HTKC Hostname vapp_htkcHostname = vim.vApp.PropertyInfo() vapp_htkcHostname.key = 4 vapp_htkcHostname.value = spec.name vapp_spec5 = vim.vApp.PropertySpec() vapp_spec5.info = vapp_htkcHostname vapp_spec5.operation = 'edit' vapp_spec_list.append(vapp_spec5) # DNS Servers vapp_dns = vim.vApp.PropertyInfo() vapp_dns.key = 5 vapp_dns.value = spec.dns vapp_spec6 = vim.vApp.PropertySpec() vapp_spec6.info = vapp_dns vapp_spec6.operation = 'edit' vapp_spec_list.append(vapp_spec6) # HTKC IP Address vapp_htkcIP = vim.vApp.PropertyInfo() vapp_htkcIP.key = 6 vapp_htkcIP.value = spec.ip vapp_spec7 = vim.vApp.PropertySpec() vapp_spec7.info = vapp_htkcIP vapp_spec7.operation = 'edit' vapp_spec_list.append(vapp_spec7) # NTP Servers vapp_ntp = vim.vApp.PropertyInfo() vapp_ntp.key = 7 vapp_ntp.value = '0.us.pool.ntp.org, 1.us.pool.ntp.org' vapp_spec8 = vim.vApp.PropertySpec() vapp_spec8.info = vapp_ntp vapp_spec8.operation = 'edit' vapp_spec_list.append(vapp_spec8) # Gateway vapp_gateway = vim.vApp.PropertyInfo() vapp_gateway.key = 8 vapp_gateway.value = spec.gateway vapp_spec9 = vim.vApp.PropertySpec() vapp_spec9.info = vapp_gateway vapp_spec9.operation = 'edit' vapp_spec_list.append(vapp_spec9) # KC Cluster Password vapp_kcpw = vim.vApp.PropertyInfo() vapp_kcpw.key = 9 vapp_kcpw.userConfigurable = True vapp_kcpw.value = spec.clusterpw vapp_spec10 = vim.vApp.PropertySpec() vapp_spec10.info = vapp_kcpw vapp_spec10.operation = 'edit' vapp_spec_list.append(vapp_spec10) # Make the modifications config_spec = vim.vm.ConfigSpec() config_spec.vAppConfig = vim.vApp.VmConfigSpec() config_spec.vAppConfig.property = vapp_spec_list reconfig_task = vm.ReconfigVM_Task(config_spec) task.wait_for_task(reconfig_task) print("Successfully modified VM:[{0}] properties".format( spec.name)) # Add the KMS tag to the VM print() print('Tagging the KMS VM :: {0}...'.format(spec.name)) kms_tag_attached = self.tag_vm(vm_id, self.kms_tag_id) assert kms_tag_attached if debug: print('Tagged KMS vm: {0}'.format(vm_id)) # Power on the VM and wait for the power on operation to be completed print() print("Powering on the KMS Server: {0} ...".format(spec.name)) poweron_vm(self.service_manager.content, vm_obj) while vm.runtime.powerState != vim.VirtualMachinePowerState.poweredOn: print("Waiting for VM [{0}] to power on...".format(spec.name)) time.sleep(3) # Get a WebKMS ticket so we can manipulate the console if we need to if spec.is_primary(): self.primary_node_ticket = vm.AcquireTicket("webmks") ticket = self.primary_node_ticket else: self.secondary_node_ticket = vm.AcquireTicket("webmks") ticket = self.secondary_node_ticket print("WebMKS Ticket: {0}".format(ticket)) print("WMKS URL: wss://{0}:{1}/ticket/{2}".format( ticket.host, ticket.port, ticket.ticket)) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message))
def deploy_ovf_template(self): # Build the deployment target with resource pool ID filter_spec = ResourcePool.FilterSpec( names=set([self.resourcepoolname])) resource_pool_summaries = self.client.vcenter.ResourcePool.list( filter_spec) if not resource_pool_summaries: raise ValueError("Resource pool with name '{}' not found".format( self.resourcepoolname)) resource_pool_id = resource_pool_summaries[0].resource_pool print('Resource pool ID: {}'.format(resource_pool_id)) deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=resource_pool_id) # Find the library item find_spec = Item.FindSpec(name=self.lib_item_name) lib_item_ids = self.client.content.library.Item.find(find_spec) if not lib_item_ids: raise ValueError("Library item with name '{}' not found".format( self.lib_item_name)) lib_item_id = lib_item_ids[0] print('Library item ID: {}'.format(lib_item_id)) ovf_summary = self.client.vcenter.ovf.LibraryItem.filter( ovf_library_item_id=lib_item_id, target=deployment_target) print('Found an OVF template: {} to deploy.'.format(ovf_summary.name)) # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template result = self.client.vcenter.ovf.LibraryItem.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print('Deployment successful. Result resource: {}, ID: {}'.format( result.resource_id.type, result.resource_id.id)) self.vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message))
from pyVmomi import vim from samples.vsphere.common.service_manager import ServiceManager from samples.vsphere.contentlibrary.lib.cls_api_client import ClsApiClient from samples.vsphere.common.vim.helpers.vim_utils import get_obj from com.vmware.content.library_client import Item from com.vmware.vcenter.ovf_client import LibraryItem vm_name = '' rp_name = '' template_name = '' esx_host = '' api_host = '' api_user = '' api_pass = '' service_manager = ServiceManager(api_host, api_user, api_pass, True) service_manager.connect() client = ClsApiClient(service_manager) find_spec = Item.FindSpec(name=template_name) ovf_template_id = client.library_item_service.find(find_spec)[0] target = get_obj(service_manager.content, [vim.HostSystem], esx_host) rp = get_obj(service_manager.content, [vim.ResourcePool], rp_name) deployment_target = LibraryItem.DeploymentTarget( host_id=target._GetMoId(), resource_pool_id=rp._GetMoId()) deployment_spec = LibraryItem.ResourcePoolDeploymentSpec(name=template_name + '_deployed', accept_all_eula=True) client.ovf_lib_item_service.deploy(ovf_template_id, deployment_target, deployment_spec)
def deploy_vm_from_ovf_template(self): # Find the datacenter by the given datacenter name self._datacenter_id = self.get_datacenter_by_name(self.datacenter) if not self._datacenter_id: self._fail(msg="Failed to find the datacenter %s" % self.datacenter) # Find the datastore by the given datastore name if self.datastore: self._datastore_id = self.get_datastore_by_name( self.datacenter, self.datastore) if not self._datastore_id: self._fail(msg="Failed to find the datastore %s" % self.datastore) # Find the datastore by the given datastore cluster name if self.datastore_cluster and not self._datastore_id: dsc = self._pyv.find_datastore_cluster_by_name( self.datastore_cluster) if dsc: self.datastore = self._pyv.get_recommended_datastore(dsc) self._datastore_id = self.get_datastore_by_name( self.datacenter, self.datastore) else: self._fail(msg="Failed to find the datastore cluster %s" % self.datastore_cluster) if not self._datastore_id: self._fail( msg= "Failed to find the datastore using either datastore or datastore cluster" ) # Find the LibraryItem (Template) by the given LibraryItem name if self.library: self._library_item_id = self.get_library_item_from_content_library_name( self.template, self.library) if not self._library_item_id: self._fail( msg= "Failed to find the library Item %s in content library %s" % (self.template, self.library)) else: self._library_item_id = self.get_library_item_by_name( self.template) if not self._library_item_id: self._fail(msg="Failed to find the library Item %s" % self.template) # Find the folder by the given FQPN folder name # The FQPN is I(datacenter)/I(folder type)/folder name/... for # example Lab/vm/someparent/myfolder is a vm folder in the Lab datacenter. folder_obj = self._pyv.find_folder_by_fqpn(self.folder, self.datacenter, folder_type='vm') if folder_obj: self._folder_id = folder_obj._moId if not self._folder_id: self._fail(msg="Failed to find the folder %s" % self.folder) # Find the Host by the given name if self.host: self._host_id = self.get_host_by_name(self.datacenter, self.host) if not self._host_id: self._fail(msg="Failed to find the Host %s" % self.host) # Find the Cluster by the given Cluster name if self.cluster: self._cluster_id = self.get_cluster_by_name( self.datacenter, self.cluster) if not self._cluster_id: self._fail(msg="Failed to find the Cluster %s" % self.cluster) cluster_obj = self.api_client.vcenter.Cluster.get(self._cluster_id) self._resourcepool_id = cluster_obj.resource_pool # Find the resourcepool by the given resourcepool name if self.resourcepool and self.cluster and self.host: self._resourcepool_id = self.get_resource_pool_by_name( self.datacenter, self.resourcepool, self.cluster, self.host) if not self._resourcepool_id: self._fail(msg="Failed to find the resource_pool %s" % self.resourcepool) if not self._resourcepool_id: self._fail( msg="Failed to find a resource pool either by name or cluster") deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=self._resourcepool_id, folder_id=self._folder_id) self.ovf_summary = self.api_client.vcenter.ovf.LibraryItem.filter( ovf_library_item_id=self._library_item_id, target=deployment_target) self.deploy_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=self.ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=self.storage_provisioning, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=self._datastore_id) response = {'succeeded': False} try: response = self.api_client.vcenter.ovf.LibraryItem.deploy( self._library_item_id, deployment_target, self.deploy_spec) except Error as error: self._fail(msg="%s" % self.get_error_message(error)) except Exception as err: self._fail(msg="%s" % to_native(err)) if not response.succeeded: self.result['vm_deploy_info'] = dict( msg="Virtual Machine deployment failed", vm_id='') self._fail(msg="Virtual Machine deployment failed") self.result['changed'] = True self.result['vm_deploy_info'] = dict( msg="Deployed Virtual Machine '%s'." % self.vm_name, vm_id=response.resource_id.id, ) self._exit()
def deployVM(self, sddcName=None, templateName='centos_master', vmName='centos', datastoreName='WorkloadDatastore', resourcePoolName='Compute-ResourcePool', folderName='Workloads', ipAddress='192.168.2.4', subnetMask='255.255.255.0', gateway='192.168.2.1'): if not sddcName: raise ValueError('You must supply an SDDC name') if not templateName: raise ValueError('You must supply a Template name') if not vmName: raise ValueError('You must supply a VM name') if not datastoreName: raise ValueError('You must supply a Datastore name') if not resourcePoolName: raise ValueError('You must supply a Resource Pool name') if not folderName: raise ValueError('You must supply a Folder name') #podNumber = re.sub(r'^[^0-9]*(.*)',r'\1',sddcName) datastore = self.getDatastore(datastoreName)._moId resourcePool = self.getResourcePool(resourcePoolName)._moId folder = self.getFolder(folderName)._moId deploymentTarget = LibraryItem.DeploymentTarget( resource_pool_id=resourcePool, folder_id=folder) findSpec = Item.FindSpec(name=templateName) libraryItemService = Item(self.stub_config) ovfLibraryItemService = LibraryItem(self.stub_config) itemIDs = libraryItemService.find(findSpec) libItemID = itemIDs[0] if itemIDs else None print('Library item ID: {0}'.format(libItemID)) ovfSummary = ovfLibraryItemService.filter( ovf_library_item_id=libItemID, target=deploymentTarget) print('Found an OVF template: {0} to deploy.'.format(ovfSummary.name)) adaptermap = vim.vm.customization.AdapterMapping() adaptermap.adapter = vim.vm.customization.IPSettings( ip=vim.vm.customization.FixedIp(ipAddress=ipAddress), subnetMask=subnetMask, gateway=gateway) globalip = vim.vm.customization.GlobalIPSettings( dnsServerList=self.org.config['WorkshopConfig']['DnsConfig']) ident = vim.vm.customization.LinuxPrep( domain='domain.local', hostName=vim.vm.customization.FixedName(name=vmName)) customspec = vim.vm.customization.Specification( nicSettingMap=[adaptermap], globalIPSettings=globalip, identity=ident) deploymentSpec = LibraryItem.ResourcePoolDeploymentSpec( name='centos', annotation=ovfSummary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) result = ovfLibraryItemService.deploy(libItemID, deploymentTarget, deploymentSpec, client_token=str(uuid.uuid4())) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print( 'Deployment successful. Result resource: {0}, ID: {1}'.format( result.resource_id.type, result.resource_id.id)) vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) # Power on the VM and wait for the power on operation to be completed vm_obj = None container = self.content.viewManager.CreateContainerView( self.content.rootFolder, [vim.VirtualMachine], True) for c in container.view: if c._GetMoId() == vm_id: vm_obj = c break assert vm_obj is not None self.wait_for_tasks(self.content, [vm_obj.Customize(spec=customspec)]) self.wait_for_tasks(self.content, [vm_obj.PowerOn()]) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message))