def __init__(self, module): super(VmwareGuestFileManager, self).__init__(module) datacenter_name = module.params['datacenter'] cluster_name = module.params['cluster'] folder = module.params['folder'] datacenter = None if datacenter_name: datacenter = find_datacenter_by_name(self.content, datacenter_name) if not datacenter: module.fail_json( msg="Unable to find %(datacenter)s datacenter" % module.params) cluster = None if cluster_name: cluster = find_cluster_by_name(self.content, cluster_name, datacenter) if not cluster: module.fail_json(msg="Unable to find %(cluster)s cluster" % module.params) if module.params['vm_id_type'] == 'inventory_path': vm = find_vm_by_id(self.content, vm_id=module.params['vm_id'], vm_id_type="inventory_path", folder=folder) else: vm = find_vm_by_id(self.content, vm_id=module.params['vm_id'], vm_id_type=module.params['vm_id_type'], datacenter=datacenter, cluster=cluster) if not vm: module.fail_json(msg='Unable to find virtual machine.') self.vm = vm try: result = dict(changed=False) if module.params['directory']: result = self.directory() if module.params['copy']: result = self.copy() if module.params['fetch']: result = self.fetch() module.exit_json(**result) except vmodl.RuntimeFault as runtime_fault: module.fail_json(msg=to_native(runtime_fault.msg)) except vmodl.MethodFault as method_fault: module.fail_json(msg=to_native(method_fault.msg)) except Exception as e: module.fail_json(msg=to_native(e))
def __init__(self, module): super(VMwareShellManager, self).__init__(module) datacenter_name = module.params['datacenter'] cluster_name = module.params['cluster'] folder = module.params['folder'] self.pm = self.content.guestOperationsManager.processManager self.timeout = self.params.get('timeout', 3600) self.wait_for_pid = self.params.get('wait_for_process', False) datacenter = None if datacenter_name: datacenter = find_datacenter_by_name(self.content, datacenter_name) if not datacenter: module.fail_json(changed=False, msg="Unable to find %(datacenter)s datacenter" % module.params) cluster = None if cluster_name: cluster = find_cluster_by_name(self.content, cluster_name, datacenter) if not cluster: module.fail_json(changed=False, msg="Unable to find %(cluster)s cluster" % module.params) if module.params['vm_id_type'] == 'inventory_path': vm = find_vm_by_id(self.content, vm_id=module.params['vm_id'], vm_id_type="inventory_path", folder=folder) else: vm = find_vm_by_id(self.content, vm_id=module.params['vm_id'], vm_id_type=module.params['vm_id_type'], datacenter=datacenter, cluster=cluster) if not vm: module.fail_json(msg='Unable to find virtual machine.') tools_status = vm.guest.toolsStatus if tools_status in ['toolsNotInstalled', 'toolsNotRunning']: self.module.fail_json(msg="VMwareTools is not installed or is not running in the guest." " VMware Tools are necessary to run this module.") try: self.execute_command(vm, module.params) except vmodl.RuntimeFault as runtime_fault: module.fail_json(changed=False, msg=to_native(runtime_fault.msg)) except vmodl.MethodFault as method_fault: module.fail_json(changed=False, msg=to_native(method_fault.msg)) except Exception as e: module.fail_json(changed=False, msg=to_native(e))
def __set_vm_obj_list(self, vm_list=None, cluster_obj=None): """ Function populate vm object list from list of vms Args: vm_list: List of vm names Returns: None """ if vm_list is None: vm_list = self.__vm_list if cluster_obj is None: cluster_obj = self.__cluster_obj if vm_list is not None: for vm in vm_list: if self.module.check_mode is False: # Get host data vm_obj = find_vm_by_id(content=self.content, vm_id=vm, vm_id_type='vm_name', cluster=cluster_obj) if vm_obj is None: raise Exception("VM %s does not exist in cluster %s" % (vm, self.__cluster_name)) self.__vm_obj_list.append(vm_obj)
def _get_vm(self): vms = [] if self.uuid: if self.use_instance_uuid: vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="use_instance_uuid") else: vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid") if vm_obj is None: self.module.fail_json( msg="Failed to find the virtual machine with UUID : %s" % self.uuid) vms = [vm_obj] elif self.name: objects = self.get_managed_objects_properties( vim_type=vim.VirtualMachine, properties=['name']) for temp_vm_object in objects: if temp_vm_object.obj.name == self.name: vms.append(temp_vm_object.obj) elif self.moid: vm_obj = VmomiSupport.templateOf('VirtualMachine')( self.module.params['moid'], self.si._stub) if vm_obj: vms.append(vm_obj) if vms: if self.params.get('name_match') == 'first': self.vm = vms[0] elif self.params.get('name_match') == 'last': self.vm = vms[-1] else: self.module.fail_json( msg="Failed to find virtual machine using %s" % (self.name or self.uuid or self.moid))
def get_vm(self): """ Find unique virtual machine either by UUID or Name. Returns: virtual machine object if found, else None. """ vms = [] if self.vm_uuid: if not self.use_instance_uuid: vm_obj = find_vm_by_id(self.content, vm_id=self.params['vm_uuid'], vm_id_type="uuid") elif self.use_instance_uuid: vm_obj = find_vm_by_id(self.content, vm_id=self.params['vm_uuid'], vm_id_type="instance_uuid") vms = [vm_obj] elif self.vm_name: objects = self.get_managed_objects_properties( vim_type=vim.VirtualMachine, properties=['name']) for temp_vm_object in objects: if len(temp_vm_object.propSet) != 1: continue if temp_vm_object.obj.name == self.vm_name: vms.append(temp_vm_object.obj) break elif self.moid: vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.moid, self.si._stub) if vm_obj: vms.append(vm_obj) if len(vms) > 1: self.module.fail_json( msg="Multiple virtual machines with same name %s found." " Please specify vm_uuid instead of vm_name." % self.vm_name) if vms: self.vm = vms[0]
def getvm_folder_paths(self): results = [] vms = [] if self.uuid: if self.use_instance_uuid: vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="instance_uuid") else: vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid") if vm_obj is None: self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid) vms = [vm_obj] elif self.name: objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name']) for temp_vm_object in objects: if temp_vm_object.obj.name == self.name: vms.append(temp_vm_object.obj) for vm in vms: folder_path = self.get_vm_path(self.content, vm) results.append(folder_path) return results
def get_all_vms_info(self, vms_list=None): """ Get all VM objects using name from given cluster Args: vms_list: List of VM names Returns: List of VM managed objects """ vm_obj_list = [] if vms_list is None: vms_list = self.vm_list for vm_name in vms_list: vm_obj = find_vm_by_id(content=self.content, vm_id=vm_name, vm_id_type='vm_name', cluster=self.cluster_obj) if vm_obj is None: self.module.fail_json( msg="Failed to find the virtual machine %s " "in given cluster %s" % (vm_name, self.cluster_name)) vm_obj_list.append(vm_obj) return vm_obj_list