def construct_host(self, provider_params, host_classes, **kwargs): """ Constructs host to be read by Ansible Tower :param provider_params: dictionary of what the provider returns when initially creating the vm :param host_classes: host object :return: broker object of constructed host instance """ if provider_params: job = provider_params job_attrs = self._merge_artifacts( job, strategy=kwargs.get("strategy", "merge") ) job_attrs = helpers.flatten_dict(job_attrs) logger.debug(job_attrs) hostname, name, host_type = None, None, "host" for key, value in job_attrs.items(): if key.endswith("fqdn") and not hostname: hostname = value if not isinstance(value, list) else value[0] if key == "vm_provisioned" and not name: name = value if not isinstance(value, list) else value[0] if key.endswith("host_type"): host_type = value if value in host_classes else host_type if not hostname: raise Exception(f"No hostname found in job attributes:\n{job_attrs}") logger.debug(f"hostname: {hostname}, name: {name}, host type: {host_type}") host_inst = host_classes[host_type](hostname=hostname, name=name, **kwargs) else: host_inst = host_classes[kwargs.get("type")](**kwargs) self._set_attributes(host_inst, broker_args=kwargs) return host_inst
def construct_host(self, provider_params, host_classes, **kwargs): """Constructs host to be read by Ansible Tower :param provider_params: dictionary of what the provider returns when initially creating the vm :param host_classes: host object :return: broker object of constructed host instance """ if provider_params: job = provider_params job_attrs = self._merge_artifacts(job, strategy=kwargs.get( "strategy", "last")) # pull information about the job arguments job_extra_vars = json.loads(job.extra_vars) # and update them if they have resolved values for key in job_extra_vars.keys(): job_extra_vars[key] = job_attrs.get(key) kwargs.update( {key: val for key, val in job_extra_vars.items() if val}) kwargs.update({key: val for key, val in job_attrs.items() if val}) if "tower_inventory" in job_attrs: kwargs["tower_inventory"] = job_attrs["tower_inventory"] job_attrs = helpers.flatten_dict(job_attrs) logger.debug(job_attrs) hostname, name, host_type = None, None, "host" for key, value in job_attrs.items(): if key.endswith("fqdn") and not hostname: hostname = value if not isinstance(value, list) else value[0] if key in ("name", "vm_provisioned") and not name: name = value if not isinstance(value, list) else value[0] if key.endswith("host_type"): host_type = value if value in host_classes else host_type if not hostname: raise Exception( f"No hostname found in job attributes:\n{job_attrs}") logger.debug( f"hostname: {hostname}, name: {name}, host type: {host_type}") host_inst = host_classes[host_type](**{ **kwargs, "hostname": hostname, "name": name }) else: host_inst = host_classes[kwargs.get("type")](**kwargs) self._set_attributes(host_inst, broker_args=kwargs) return host_inst
def construct_host(self, provider_params, host_classes, **kwargs): job = provider_params job_attrs = self._merge_artifacts(job, strategy=kwargs.get( "strategy", "latest")) job_attrs = flatten_dict(job_attrs) logger.debug(job_attrs) hostname, host_type = None, "host" for key, value in job_attrs.items(): if key.endswith("fqdn"): hostname = value if not isinstance(value, list) else value[0] if key.endswith("host_type"): host_type = value logger.debug(f"hostname: {hostname}, host type: {host_type}") if hostname: host_inst = host_classes[host_type](hostname=hostname, **kwargs) self._set_attributes(host_inst) return host_inst raise Exception(f"No hostname found in job attributes:\n{job_attrs}")