def __init__(self, module): super(VmwreClusterInfoManager, self).__init__(module) datacenter = self.params.get('datacenter') cluster_name = self.params.get('cluster_name') self.schema = self.params.get('schema') self.properties = self.params.get('properties') 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] else: self.cluster_objs = [] datacenters = self.get_datacenter_info() for datacenter in datacenters: datacenter_obj = find_datacenter_by_name( self.content, datacenter_name=datacenter['name']) [ self.cluster_objs.append(x) for x in self.get_all_cluster_objs(parent=datacenter_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.__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)
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'] 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, datacenter_name=self.datacenter) 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 self.module.warn( "The default for enable will change from false to true in a future version to make the behavior more consistent with other modules." "Please make sure your playbooks don't rely on the default being false so you don't run into problems." )
def state_add_rp(self): changed = True if self.module.check_mode: self.module.exit_json(changed=changed) 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) rp_spec = self.generate_rp_config() rootResourcePool = self.cluster_obj.resourcePool rootResourcePool.CreateResourcePool(self.resource_pool, rp_spec) resource_pool_config = self.generate_rp_config_return_value(True) self.module.exit_json(changed=changed, resource_pool_config=resource_pool_config)
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(VMwareCluster, self).__init__(module) self.cluster_name = module.params['cluster_name'] self.datacenter_name = module.params['datacenter'] self.enable_ha = module.params['enable'] 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, datacenter_name=self.datacenter) 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, False) else: self.changed_advanced_settings = None self.module.warn("The default for enable will change from false to true in a future version to make the behavior more consistent with other modules." "Please make sure your playbooks don't rely on the default being false so you don't run into problems.")
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(VmwareDrsFactManager, 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_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, datacenter_name=self.datacenter) 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): 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 __init__(self, module): super(VMwareShellManager, self).__init__(module) datacenter_name = module.params['datacenter'] cluster_name = module.params['cluster'] folder = module.params['folder'] try: self.pm = self.content.guestOperationsManager.processManager except vmodl.fault.ManagedObjectNotFound: pass 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 __init__(self, module): super(VMwareResourcePool, self).__init__(module) self.datacenter = module.params['datacenter'] self.resource_pool = module.params['resource_pool'] self.hostname = module.params['hostname'] self.username = module.params['username'] self.password = module.params['password'] self.state = module.params['state'] self.mem_shares = module.params['mem_shares'] self.mem_allocation_shares = module.params['mem_allocation_shares'] self.mem_limit = module.params['mem_limit'] self.mem_reservation = module.params['mem_reservation'] self.mem_expandable_reservations = module.params[ 'mem_expandable_reservations'] self.cpu_shares = module.params['cpu_shares'] self.cpu_allocation_shares = module.params['cpu_allocation_shares'] self.cpu_limit = module.params['cpu_limit'] self.cpu_reservation = module.params['cpu_reservation'] self.cpu_expandable_reservations = module.params[ 'cpu_expandable_reservations'] self.resource_pool_obj = None 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) if module.params['cluster']: self.compute_resource_obj = find_cluster_by_name(self.content, module.params['cluster'], datacenter=self.dc_obj) if self.compute_resource_obj is None: self.module.fail_json(msg="Unable to find cluster with name %s" % module.params['cluster']) if module.params['esxi_hostname']: self.compute_resource_obj = find_object_by_name(self.content, module.params['esxi_hostname'], [vim.ComputeResource], folder=self.dc_obj.hostFolder) if self.compute_resource_obj is None: self.module.fail_json(msg="Unable to find host with name %s" % module.params['esxi_hostname'])
def __init__(self, module): super(VMwareCluster, self).__init__(module) self.cluster_name = module.params['cluster_name'] self.datacenter_name = module.params['datacenter'] self.enable_vsan = module.params['enable_vsan'] self.datacenter = None self.cluster = None self.advanced_options = 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, datacenter_name=self.datacenter) if self.cluster is None: self.module.fail_json(msg="Cluster %s does not exist." % self.cluster_name) if module.params['advanced_options'] is not None: self.advanced_options = module.params['advanced_options'] client_stub = self.si._GetStub() ssl_context = client_stub.schemeArgs.get('context') apiVersion = vsanapiutils.GetLatestVmodlVersion( module.params['hostname']) vcMos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=apiVersion) self.vsanClusterConfigSystem = vcMos['vsan-cluster-config-system']
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'] 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, datacenter_name=self.datacenter) 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, 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 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(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(VMwareCluster, self).__init__(module) self.cluster_name = module.params['cluster_name'] self.datacenter_name = module.params['datacenter'] self.enable_vsan = module.params['enable_vsan'] 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)
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): super(VMwareCluster, self).__init__(module) self.cluster_name = module.params['cluster_name'] self.datacenter_name = module.params['datacenter'] self.enable_dpm = module.params['enable_dpm'] self.default_dpm_behaviour = module.params['default_dpm_behaviour'] self.host_power_action_rate = module.params['host_power_action_rate'] 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, datacenter_name=self.datacenter) if self.cluster is None: self.module.fail_json(msg="Cluster %s does not exist." % self.cluster_name)
def __init__(self, module): super(VMwareCluster, self).__init__(module) self.cluster_name = module.params['cluster_name'] self.datacenter_name = module.params['datacenter'] self.enable_vsan = module.params['enable'] self.datacenter = None self.cluster = None self.advanced_options = 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, datacenter_name=self.datacenter) if self.cluster is None: self.module.fail_json(msg="Cluster %s does not exist." % self.cluster_name) if module.params['advanced_options'] is not None: self.advanced_options = module.params['advanced_options'] client_stub = self.si._GetStub() ssl_context = client_stub.schemeArgs.get('context') apiVersion = vsanapiutils.GetLatestVmodlVersion( module.params['hostname']) vcMos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=apiVersion) self.vsanClusterConfigSystem = vcMos['vsan-cluster-config-system'] self.module.warn( "The default for enable will change from false to true in a future version to make the behavior more consistent with other modules." "Please make sure your playbooks don't rely on the default being false so you don't run into problems." )
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 self.destination_datacenter = self.params.get('destination_datacenter', 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) if self.host_object is None: self.module.fail_json(msg="Unable to find destination host %s" % dest_host_name) # Get Datacenter if specified by user dest_datacenter = self.destination_datacenter if dest_datacenter is not None: datacenter_object = find_datacenter_by_name(content=self.content, datacenter_name=dest_datacenter) if datacenter_object: dest_datacenter = datacenter_object # 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, datacenter_name=dest_datacenter) # 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 find 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)
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: 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 not module.check_mode: 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) # Do not 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'