def __init__(self, module): super(VMwareCluster, self).__init__(module) self.cluster_name = module.params['cluster_name'] self.datacenter_name = module.params['datacenter'] self.enable_ha = module.params['enable_ha'] self.datacenter = None self.cluster = None self.host_isolation_response = getattr(vim.cluster.DasVmSettings.IsolationResponse, self.params.get('host_isolation_response')) if self.enable_ha and ( self.params.get('slot_based_admission_control') or self.params.get('reservation_based_admission_control') or self.params.get('failover_host_admission_control')): self.ha_admission_control = True else: self.ha_admission_control = False self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name) if self.datacenter is None: self.module.fail_json(msg="Datacenter %s does not exist." % self.datacenter_name) self.cluster = self.find_cluster_by_name(cluster_name=self.cluster_name) if self.cluster is None: self.module.fail_json(msg="Cluster %s does not exist." % self.cluster_name) self.advanced_settings = self.params.get('advanced_settings') if self.advanced_settings: self.changed_advanced_settings = option_diff(self.advanced_settings, self.cluster.configurationEx.dasConfig.option) else: self.changed_advanced_settings = None
def state_add_rp(self): changed = True rp_spec = vim.ResourceConfigSpec() cpu_alloc = vim.ResourceAllocationInfo() cpu_alloc.expandableReservation = self.cpu_expandable_reservations cpu_alloc.limit = int(self.cpu_limit) cpu_alloc.reservation = int(self.cpu_reservation) cpu_alloc_shares = vim.SharesInfo() cpu_alloc_shares.level = self.cpu_shares cpu_alloc.shares = cpu_alloc_shares rp_spec.cpuAllocation = cpu_alloc mem_alloc = vim.ResourceAllocationInfo() mem_alloc.limit = int(self.mem_limit) mem_alloc.expandableReservation = self.mem_expandable_reservations mem_alloc.reservation = int(self.mem_reservation) mem_alloc_shares = vim.SharesInfo() mem_alloc_shares.level = self.mem_shares mem_alloc.shares = mem_alloc_shares rp_spec.memoryAllocation = mem_alloc self.dc_obj = find_datacenter_by_name(self.content, self.datacenter) if self.dc_obj is None: self.module.fail_json(msg="Unable to find datacenter with name %s" % self.datacenter) self.cluster_obj = find_cluster_by_name(self.content, self.cluster, datacenter=self.dc_obj) if self.cluster_obj is None: self.module.fail_json(msg="Unable to find cluster with name %s" % self.cluster) rootResourcePool = self.cluster_obj.resourcePool rootResourcePool.CreateResourcePool(self.resource_pool, rp_spec) self.module.exit_json(changed=changed)
def __init__(self, module): super(VmwareDrsInfoManager, self).__init__(module) datacenter_name = self.params.get('datacenter', None) if datacenter_name: datacenter_obj = find_datacenter_by_name( self.content, datacenter_name=datacenter_name) self.cluster_obj_list = [] if datacenter_obj: folder = datacenter_obj.hostFolder self.cluster_obj_list = get_all_objs( self.content, [vim.ClusterComputeResource], folder) else: self.module.fail_json(changed=False, msg="Datacenter '%s' not found" % datacenter_name) cluster_name = self.params.get('cluster_name', None) if cluster_name: cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name) if cluster_obj is None: self.module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name) else: self.cluster_obj_list = [cluster_obj]
def check_evc_configuration(self): """ Check evc configuration Returns: 'Present' if evc enabled, else 'absent' """ try: self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name) if self.datacenter is None: self.module.fail_json(msg="Datacenter '%s' does not exist." % self.datacenter_name) self.cluster = self.find_cluster_by_name( cluster_name=self.cluster_name, datacenter_name=self.datacenter) if self.cluster is None: self.module.fail_json(msg="Cluster '%s' does not exist." % self.cluster_name) self.evcm = self.cluster.EvcManager() if not self.evcm: self.module.fail_json( msg="Unable to get EVC manager for cluster '%s'." % self.cluster_name) self.evc_state = self.evcm.evcState self.current_evc_mode = self.evc_state.currentEVCModeKey if not self.current_evc_mode: return 'absent' return 'present' except Exception as generic_exc: self.module.fail_json(msg="Failed to check configuration" " due to generic exception %s" % to_native(generic_exc))
def __init__(self, module): super(VMwareCluster, self).__init__(module) self.cluster_name = module.params['cluster_name'] self.datacenter_name = module.params['datacenter'] self.enable_drs = module.params['enable_drs'] self.datacenter = None self.cluster = None self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name) if self.datacenter is None: self.module.fail_json(msg="Datacenter %s does not exist." % self.datacenter_name) self.cluster = self.find_cluster_by_name( cluster_name=self.cluster_name) if self.cluster is None: self.module.fail_json(msg="Cluster %s does not exist." % self.cluster_name) self.advanced_settings = self.params.get('advanced_settings') if self.advanced_settings: self.changed_advanced_settings = option_diff( self.advanced_settings, self.cluster.configurationEx.drsConfig.option) else: self.changed_advanced_settings = None
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(VmwareFolderManager, self).__init__(module) datacenter_name = self.params.get('datacenter', None) self.datacenter_obj = find_datacenter_by_name(self.content, datacenter_name=datacenter_name) if self.datacenter_obj is None: self.module.fail_json(msg="Failed to find datacenter %s" % datacenter_name) self.datacenter_folder_type = { 'vm': self.datacenter_obj.vmFolder, 'host': self.datacenter_obj.hostFolder, 'datastore': self.datacenter_obj.datastoreFolder, 'network': self.datacenter_obj.networkFolder, }
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 get_datacenter(self): try: datacenter_obj = find_datacenter_by_name(self.content, self.datacenter_name) return datacenter_obj except (vmodl.MethodFault, vmodl.RuntimeFault) as runtime_fault: self.module.fail_json( msg="Failed to get datacenter '%s'" " due to : %s" % (self.datacenter_name, to_native(runtime_fault.msg))) except Exception as generic_exc: self.module.fail_json( msg="Failed to get datacenter" " '%s' due to generic error: %s" % (self.datacenter_name, to_native(generic_exc)))
def __init__(self, module): super(VmwreClusterInfoManager, self).__init__(module) datacenter = self.params.get('datacenter') cluster_name = self.params.get('cluster_name') self.cluster_objs = [] if datacenter: datacenter_obj = find_datacenter_by_name(self.content, datacenter_name=datacenter) if datacenter_obj is None: self.module.fail_json(msg="Failed to find datacenter '%s'" % datacenter) self.cluster_objs = self.get_all_cluster_objs(parent=datacenter_obj) elif cluster_name: cluster_obj = find_cluster_by_name(self.content, cluster_name=cluster_name) if cluster_obj is None: self.module.fail_json(msg="Failed to find cluster '%s'" % cluster_name) self.cluster_objs = [cluster_obj]
def __init__(self, module): """ Doctring: Init """ super(VmwareVmHostRuleDrs, self).__init__(module) self.__datacenter_name = module.params.get('datacenter', None) self.__datacenter_obj = None self.__cluster_name = module.params['cluster_name'] self.__cluster_obj = None self.__vm_group_name = module.params.get('vm_group_name', None) self.__host_group_name = module.params.get('host_group_name', None) self.__rule_name = module.params['drs_rule_name'] self.__enabled = module.params['enabled'] self.__mandatory = module.params['mandatory'] self.__affinity_rule = module.params['affinity_rule'] self.__state = module.params['state'] self.__msg = 'Nothing to see here...' self.__result = dict() self.__changed = False if self.__datacenter_name is not None: self.__datacenter_obj = find_datacenter_by_name( self.content, self.__datacenter_name) if self.__datacenter_obj is None and module.check_mode is False: raise Exception("Datacenter '%s' not found" % self.__datacenter_name) self.__cluster_obj = find_cluster_by_name( content=self.content, cluster_name=self.__cluster_name, datacenter=self.__datacenter_obj) # Throw error if cluster does not exist if self.__cluster_obj is None and module.check_mode is False: raise Exception("Cluster '%s' not found" % self.__cluster_name) # Dont populate lists if we are deleting group if self.__state == 'present': # Get list of vm groups only if state is present self.__vm_group_obj = self.__get_group_by_name( group_name=self.__vm_group_name) self.__host_group_obj = self.__get_group_by_name( group_name=self.__host_group_name, host_group=True)
def __init__(self, module, datacenter_name, cluster_name=None): """ Doctring: Init """ super(VmwareDrsGroupFactManager, self).__init__(module) self.__datacenter_name = datacenter_name self.__datacenter_obj = None self.__cluster_name = cluster_name self.__cluster_obj = None self.__msg = 'Nothing to see here...' self.__result = dict() self.__changed = False if datacenter_name: datacenter_obj = find_datacenter_by_name( self.content, datacenter_name=datacenter_name) self.cluster_obj_list = [] if datacenter_obj: folder = datacenter_obj.hostFolder self.cluster_obj_list = get_all_objs( self.content, [vim.ClusterComputeResource], folder) else: raise Exception("Datacenter '%s' not found" % self.__datacenter_name) if cluster_name: cluster_obj = self.find_cluster_by_name( cluster_name=self.__cluster_name) if cluster_obj is None: raise Exception("Cluster '%s' not found" % self.__cluster_name) else: self.cluster_obj_list = [cluster_obj]
def check_cluster_configuration(self): """ Check cluster configuration Returns: 'Present' if cluster exists, else 'absent' """ try: self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name) if self.datacenter is None: self.module.fail_json(msg="Datacenter %s does not exist." % self.datacenter_name) self.cluster = self.find_cluster_by_name(cluster_name=self.cluster_name) if self.cluster is None: return 'absent' return 'present' except vmodl.RuntimeFault as runtime_fault: self.module.fail_json(msg=to_native(runtime_fault.msg)) except vmodl.MethodFault as method_fault: self.module.fail_json(msg=to_native(method_fault.msg)) except Exception as generic_exc: self.module.fail_json(msg="Failed to check configuration" " due to generic exception %s" % to_native(generic_exc))
def __init__(self, module, cluster_name, group_name, state, datacenter_name=None, vm_list=None, host_list=None): """ Init """ super(VmwareDrsGroupManager, self).__init__(module) self.__datacenter_name = datacenter_name self.__datacenter_obj = None self.__cluster_name = cluster_name self.__cluster_obj = None self.__group_name = group_name self.__group_obj = None self.__operation = None self.__vm_list = vm_list self.__vm_obj_list = [] self.__host_list = host_list self.__host_obj_list = [] self.__msg = 'Nothing to see here...' self.__result = dict() self.__changed = False self.__state = state if datacenter_name is not None: self.__datacenter_obj = find_datacenter_by_name( self.content, self.__datacenter_name) if self.__datacenter_obj is None and module.check_mode is False: raise Exception("Datacenter '%s' not found" % self.__datacenter_name) self.__cluster_obj = find_cluster_by_name( content=self.content, cluster_name=self.__cluster_name, datacenter=self.__datacenter_obj) # Throw error if cluster does not exist if self.__cluster_obj is None: if module.check_mode is False: raise Exception("Cluster '%s' not found" % self.__cluster_name) else: # get group self.__group_obj = self.__get_group_by_name() # Set result here. If nothing is to be updated, result is already set self.__set_result(self.__group_obj) # Dont populate lists if we are deleting group if state == 'present': if self.__group_obj: self.__operation = 'edit' else: self.__operation = 'add' if self.__vm_list is not None: self.__set_vm_obj_list(vm_list=self.__vm_list) if self.__host_list is not None: self.__set_host_obj_list(host_list=self.__host_list) else: self.__operation = 'remove'