def set_vnc_extraconfig(content, vm, enabled, ip, port, password): result = dict( changed=False, failed=False, ) # set new values key_prefix = "remotedisplay.vnc." new_values = dict() for key in ['enabled', 'ip', 'port', 'password']: new_values[key_prefix + key] = "" if enabled: new_values[key_prefix + "enabled"] = "true" new_values[key_prefix + "password"] = str(password).strip() new_values[key_prefix + "ip"] = str(ip).strip() new_values[key_prefix + "port"] = str(port).strip() # get current vnc config current_values = get_vnc_extraconfig(vm) # check if any value is changed reconfig_vm = False for key, val in new_values.items(): key = key.replace(key_prefix, "") current_value = current_values.get(key, "") # enabled is not case-sensitive if key == "enabled": current_value = current_value.lower() val = val.lower() if current_value != val: reconfig_vm = True if not reconfig_vm: return result # reconfigure vm spec = vim.vm.ConfigSpec() spec.extraConfig = [] for key, val in new_values.items(): opt = vim.option.OptionValue() opt.key = key opt.value = val spec.extraConfig.append(opt) task = vm.ReconfigVM_Task(spec) try: wait_for_task(task) except TaskError as task_err: result['failed'] = True result['msg'] = to_native(task_err) if task.info.state == 'error': result['failed'] = True result['msg'] = task.info.error.msg else: result['changed'] = True result['instance'] = gather_vm_facts(content, vm) return result
def deploy(self): facts = {} if self.params['inject_ovf_env']: self.inject_ovf_env() if self.params['power_on']: task = self.entity.PowerOn() if self.params['wait']: wait_for_task(task) if self.params['wait_for_ip_address']: _facts = wait_for_vm_ip(self.si, self.entity) if not _facts: self.module.fail_json( msg='Waiting for IP address timed out') facts.update(_facts) if not facts: facts.update(gather_vm_facts(self.si, self.entity)) return facts
def get_lease(self): datastore, datacenter, resource_pool, network_mappings = self.get_objects( ) params = { 'diskProvisioning': self.params['disk_provisioning'], } if self.params['name']: params['entityName'] = self.params['name'] if network_mappings: params['networkMapping'] = network_mappings if self.params['deployment_option']: params['deploymentOption'] = self.params['deployment_option'] if self.params['properties']: params['propertyMapping'] = [] for key, value in self.params['properties'].items(): property_mapping = vim.KeyValue() property_mapping.key = key property_mapping.value = str(value) if isinstance( value, bool) else value params['propertyMapping'].append(property_mapping) if self.params['folder']: folder = self.si.searchIndex.FindByInventoryPath( self.params['folder']) if not folder: self.module.fail_json( msg="Unable to find the specified folder %(folder)s" % self.params) else: folder = datacenter.vmFolder spec_params = vim.OvfManager.CreateImportSpecParams(**params) ovf_descriptor = self.get_ovf_descriptor() self.import_spec = self.si.ovfManager.CreateImportSpec( ovf_descriptor, resource_pool, datastore, spec_params) errors = [ to_native(e.msg) for e in getattr(self.import_spec, 'error', []) ] if self.params['fail_on_spec_warnings']: errors.extend((to_native(w.msg) for w in getattr(self.import_spec, 'warning', []))) if errors: self.module.fail_json( msg='Failure validating OVF import spec: %s' % '. '.join(errors)) for warning in getattr(self.import_spec, 'warning', []): self.module.warn('Problem validating OVF import spec: %s' % to_native(warning.msg)) if not self.params['allow_duplicates']: name = self.import_spec.importSpec.configSpec.name match = find_vm_by_name(self.si, name, folder=folder) if match: self.module.exit_json(instance=gather_vm_facts(self.si, match), changed=False) if self.module.check_mode: self.module.exit_json(changed=True, instance={'hw_name': name}) try: self.lease = resource_pool.ImportVApp(self.import_spec.importSpec, folder) except vmodl.fault.SystemError as e: self.module.fail_json(msg='Failed to start import: %s' % to_native(e.msg)) while self.lease.state != vim.HttpNfcLease.State.ready: time.sleep(0.1) self.entity = self.lease.info.entity return self.lease, self.import_spec
def gather_facts(self, vm): return gather_vm_facts(self.content, vm)