def get_objects(self): self.datastore = find_datastore_by_name(self.si, self.params['datastore']) if not self.datastore: self.module.fail_json(msg='%(datastore)s could not be located' % self.params) self.datacenter = find_datacenter_by_name(self.si, self.params['datacenter']) if not self.datacenter: self.module.fail_json(msg='%(datacenter)s could not be located' % self.params) self.resource_pool = find_resource_pool_by_name( self.si, self.params['resource_pool']) if not self.resource_pool: self.module.fail_json( msg='%(resource_pool)s could not be located' % self.params) for key, value in self.params['networks'].items(): network = find_network_by_name(self.si, value) if not network: self.module.fail_json(msg='%(network)s could not be located' % self.params) network_mapping = vim.OvfManager.NetworkMapping() network_mapping.name = key network_mapping.network = network self.network_mappings.append(network_mapping) return self.datastore, self.datacenter, self.resource_pool, self.network_mappings
def sanitize_params(self): ''' this method is used to verify user provided parameters ''' self.vm_obj = self.get_vm() if self.vm_obj is None: vm_id = self.vm_uuid or self.vm_name or self.moid self.module.fail_json( msg="Failed to find the VM/template with %s" % vm_id) # connect to destination VC self.destination_content = connect_to_api( self.module, hostname=self.destination_vcenter, username=self.destination_vcenter_username, password=self.destination_vcenter_password, port=self.destination_vcenter_port, validate_certs=self.destination_vcenter_validate_certs) # Check if vm name already exists in the destination VC vm = find_vm_by_name(content=self.destination_content, vm_name=self.params['destination_vm_name']) if vm: self.module.exit_json( changed=False, msg="A VM with the given name already exists") datastore_name = self.params['destination_datastore'] datastore_cluster = find_obj(self.destination_content, [vim.StoragePod], datastore_name) if datastore_cluster: # If user specified datastore cluster so get recommended datastore datastore_name = self.get_recommended_datastore( datastore_cluster_obj=datastore_cluster) # Check if get_recommended_datastore or user specified datastore exists or not self.destination_datastore = find_datastore_by_name( content=self.destination_content, datastore_name=datastore_name) if self.destination_datastore is None: self.module.fail_json(msg="Destination datastore not found.") self.destination_host = find_hostsystem_by_name( content=self.destination_content, hostname=self.params['destination_host']) if self.destination_host is None: self.module.fail_json(msg="Destination host not found.") if self.params['destination_resource_pool']: self.destination_resource_pool = find_resource_pool_by_name( content=self.destination_content, resource_pool_name=self.params['destination_resource_pool']) else: self.destination_resource_pool = self.destination_host.parent.resourcePool
def execute(self): result = dict(changed=False) folder_obj = self.content.searchIndex.FindByInventoryPath( inventoryPath="%s" % self.folder) if (not (folder_obj)): self.module.fail_json(msg="folder %s not found." % self.folder) if (self.state == "present"): if (find_vm_by_name(self.content, self.name)): self.module.exit_json(**result) host_obj = self.find_hostsystem_by_name(self.esxi_hostname) if (not (host_obj)): self.module.fail_json(msg="host %s not found." % self.host) resource_pool_obj = find_resource_pool_by_name( self.content, self.pool) if (not (resource_pool_obj)): self.module.fail_json(msg="resource pool %s not found." % self.pool) try: folder_obj.RegisterVM_Task(path=self.path, name=self.name, asTemplate=self.template, pool=resource_pool_obj, host=host_obj) except Exception as e: self.module.fail_json(msg="%s" % e) result.update(changed=True) self.module.exit_json(**result) else: vm_obj = find_vm_by_name(self.content, self.name, folder=folder_obj) if (vm_obj): try: vm_obj.UnregisterVM() except Exception as e: self.module.fail_json(msg="%s" % e) result.update(changed=True) self.module.exit_json(**result) else: self.module.exit_json(**result)
def get_objects(self): self.datastore = find_datastore_by_name(self.si, self.params['datastore']) if not self.datastore: self.module.fail_json(msg='%(datastore)s could not be located' % self.params) self.datacenter = find_datacenter_by_name(self.si, self.params['datacenter']) if not self.datacenter: self.module.fail_json(msg='%(datacenter)s could not be located' % self.params) # self.vm = find_vm_by_name(self.si, self.params['name']) # if not self.vm: # self.module.fail_json(msg='%(name)s could not be located' % self.params) self.resource_pool = find_resource_pool_by_name( self.si, self.params['resource_pool']) if not self.resource_pool: self.module.fail_json( msg='%(resource_pool)s could not be located' % self.params) for key, value in self.params['ovf_networks'].items(): network = find_network_by_name(self.si, value) if not network: self.module.fail_json( msg='%(ovf_network)s could not be located' % self.params) network_mapping = vim.OvfManager.NetworkMapping() network_mapping.name = key network_mapping.network = network self.network_mappings.append(network_mapping) if self.params['property_map']: for key, value in self.params['property_map'].items(): property_map = vim.KeyValue() property_map.key = key property_map.value = value self.property_mappings.append(property_map) return self.datastore, self.datacenter, self.resource_pool, self.network_mappings, self.property_mappings
def execute(self): result = dict(changed=False) datacenter = self.find_datacenter_by_name(self.datacenter) if not datacenter: self.module.fail_json( msg="Cannot find the specified Datacenter: %s" % self.datacenter) dcpath = compile_folder_path_for_object(datacenter) if not dcpath.endswith("/"): dcpath += "/" if (self.folder in [None, "", "/"]): self.module.fail_json( msg="Please specify folder path other than blank or '/'") elif (self.folder.startswith("/vm")): fullpath = "%s%s%s" % (dcpath, self.datacenter, self.folder) else: fullpath = "%s%s" % (dcpath, self.folder) folder_obj = self.content.searchIndex.FindByInventoryPath( inventoryPath="%s" % fullpath) if not folder_obj: details = { 'datacenter': datacenter.name, 'datacenter_path': dcpath, 'folder': self.folder, 'full_search_path': fullpath, } self.module.fail_json( msg="No folder %s matched in the search path : %s" % (self.folder, fullpath), details=details) if self.state == "present": if self.get_vm(): self.module.exit_json(**result) if self.esxi_hostname: host_obj = self.find_hostsystem_by_name(self.esxi_hostname) if not host_obj: self.module.fail_json( msg="Cannot find the specified ESXi host: %s" % self.esxi_hostname) else: host_obj = None if self.cluster: cluster_obj = find_cluster_by_name(self.content, self.cluster, datacenter) if not cluster_obj: self.module.fail_json( msg="Cannot find the specified cluster name: %s" % self.cluster) resource_pool_obj = cluster_obj.resourcePool elif self.resource_pool: resource_pool_obj = find_resource_pool_by_name( self.content, self.resource_pool) if not resource_pool_obj: self.module.fail_json( msg="Cannot find the specified resource pool: %s" % self.resource_pool) else: resource_pool_obj = host_obj.parent.resourcePool task = folder_obj.RegisterVM_Task(path=self.path, name=self.name, asTemplate=self.template, pool=resource_pool_obj, host=host_obj) changed = False try: changed, info = wait_for_task(task) except Exception as task_e: self.module.fail_json(msg=to_native(task_e)) result.update(changed=changed) self.module.exit_json(**result) if self.state == "absent": vm_obj = self.get_vm() if vm_obj: try: vm_obj.UnregisterVM() result.update(changed=True) except Exception as exc: self.module.fail_json(msg=to_native(exc)) self.module.exit_json(**result)
def __init__(self, module): super(VmotionManager, self).__init__(module) self.vm = None self.vm_uuid = self.params.get('vm_uuid', None) self.use_instance_uuid = self.params.get('use_instance_uuid', False) self.vm_name = self.params.get('vm_name', None) self.moid = self.params.get('moid') or None result = dict() self.get_vm() if self.vm is None: vm_id = self.vm_uuid or self.vm_name or self.moid self.module.fail_json(msg="Failed to find the virtual machine with %s" % vm_id) # Get Destination Host System if specified by user dest_host_name = self.params.get('destination_host', None) self.host_object = None if dest_host_name is not None: self.host_object = find_hostsystem_by_name(content=self.content, hostname=dest_host_name) # Get Destination Datastore if specified by user dest_datastore = self.params.get('destination_datastore', None) self.datastore_object = None if dest_datastore is not None: self.datastore_object = find_datastore_by_name(content=self.content, datastore_name=dest_datastore) # At-least one of datastore, host system is required to migrate if self.datastore_object is None and self.host_object is None: self.module.fail_json(msg="Unable to find destination datastore" " and destination host system.") # Get Destination resourcepool dest_resourcepool = self.params.get('destination_resourcepool', None) self.resourcepool_object = None if dest_resourcepool: self.resourcepool_object = find_resource_pool_by_name(content=self.content, resource_pool_name=dest_resourcepool) elif not dest_resourcepool and dest_host_name: self.resourcepool_object = self.host_object.parent.resourcePool # Fail if resourcePool object is not found if self.resourcepool_object is None: self.module.fail_json(msg="Unable to destination resource pool object which is required") # Check if datastore is required, this check is required if destination # and source host system does not share same datastore. host_datastore_required = [] for vm_datastore in self.vm.datastore: if self.host_object and vm_datastore not in self.host_object.datastore: host_datastore_required.append(True) else: host_datastore_required.append(False) if any(host_datastore_required) and dest_datastore is None: msg = "Destination host system does not share" \ " datastore ['%s'] with source host system ['%s'] on which" \ " virtual machine is located. Please specify destination_datastore" \ " to rectify this problem." % ("', '".join([ds.name for ds in self.host_object.datastore]), "', '".join([ds.name for ds in self.vm.datastore])) self.module.fail_json(msg=msg) storage_vmotion_needed = True change_required = True if self.host_object and self.datastore_object: # We have both host system and datastore object if not self.datastore_object.summary.accessible: # Datastore is not accessible self.module.fail_json(msg='Destination datastore %s is' ' not accessible.' % dest_datastore) if self.datastore_object not in self.host_object.datastore: # Datastore is not associated with host system self.module.fail_json(msg="Destination datastore %s provided" " is not associated with destination" " host system %s. Please specify" " datastore value ['%s'] associated with" " the given host system." % (dest_datastore, dest_host_name, "', '".join([ds.name for ds in self.host_object.datastore]))) if self.vm.runtime.host.name == dest_host_name and dest_datastore in [ds.name for ds in self.vm.datastore]: change_required = False if self.host_object and self.datastore_object is None: if self.vm.runtime.host.name == dest_host_name: # VM is already located on same host change_required = False storage_vmotion_needed = False elif self.datastore_object and self.host_object is None: if self.datastore_object in self.vm.datastore: # VM is already located on same datastore change_required = False if not self.datastore_object.summary.accessible: # Datastore is not accessible self.module.fail_json(msg='Destination datastore %s is' ' not accessible.' % dest_datastore) if module.check_mode: result['running_host'] = module.params['destination_host'] result['changed'] = True module.exit_json(**result) if change_required: # Migrate VM and get Task object back task_object = self.migrate_vm() # Wait for task to complete try: wait_for_task(task_object) except TaskError as task_error: self.module.fail_json(msg=to_native(task_error)) # If task was a success the VM has moved, update running_host and complete module if task_object.info.state == vim.TaskInfo.State.success: # The storage layout is not automatically refreshed, so we trigger it to get coherent module return values if storage_vmotion_needed: self.vm.RefreshStorageInfo() result['running_host'] = module.params['destination_host'] result['changed'] = True module.exit_json(**result) else: msg = 'Unable to migrate virtual machine due to an error, please check vCenter' if task_object.info.error is not None: msg += " : %s" % task_object.info.error module.fail_json(msg=msg) else: try: host = self.vm.summary.runtime.host result['running_host'] = host.summary.config.name except vim.fault.NoPermission: result['running_host'] = 'NA' result['changed'] = False module.exit_json(**result)