Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use host facts when syncing inventory from AAP #261

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions broker/providers/ansible_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,16 @@ def _get_failure_messages(self, workflow):
return failure_messages

def _compile_host_info(self, host):
# attempt to get the hostname from the host variables and then facts
if not (hostname := host.variables.get("fqdn")):
if hasattr(host_facts := host.related.ansible_facts.get(), "results"):
hostname = host_facts.results[0].ansible_facts.get("ansible_fqdn")
host_facts = host.related.ansible_facts.get()

# Get the hostname from host variables or facts
hostname = (
host.variables.get("fqdn")
or getattr(host_facts, "ansible_fqdn", None)
# Workaround for OSP hosts that have lost their hostname
or host.variables.get("openstack", {}).get("metadata", {}).get("fqdn", None)
)

host_info = {
"name": host.name,
"type": host.type,
Expand All @@ -357,33 +363,38 @@ def _compile_host_info(self, host):
"tower_inventory": self._translate_inventory(host.inventory),
"_broker_provider": "AnsibleTower",
"_broker_provider_instance": self.instance,
"_broker_args": getattr(host, "_broker_args", {}),
# Get _broker_args from host facts if present
"_broker_args": getattr(
host_facts, "_broker_args", self._get_broker_args_from_job(host)
),
}

return host_info

def _get_broker_args_from_job(self, host):
"""Get _broker_args from the source workflow job or last job."""
_broker_args = {}

try:
create_job = self.v2.jobs.get(id=host.get_related("job_events").results[0].job)
create_job = create_job.results[0].get_related("source_workflow_job")
host_info["_broker_args"]["workflow"] = create_job.name
_broker_args["workflow"] = create_job.name
except (IndexError, awxkit.exceptions.Unknown): # Unknown is a Gateway Timeout
if "last_job" in host.related:
# potentially not create job, but easier processing below
create_job = host.get_related("last_job")
try:
host_info["_broker_args"]["workflow"] = host.get_related(
"last_job"
).summary_fields.source_workflow_job.name
_broker_args["workflow"] = create_job.summary_fields.source_workflow_job.name
except Exception as err: # noqa: BLE001
logger.debug(f"Tell Jake that the exception here is: {err}!")
logger.warning(f"Unable to determine workflow for {host_info['hostname']}")
logger.warning(f"Unable to determine workflow for {host.name}")
else:
return host_info
return _broker_args
create_vars = json.loads(create_job.extra_vars)
host_info["_broker_args"].update(
_broker_args.update(
{arg: val for arg, val in create_vars.items() if val and isinstance(val, str)}
)
# temporary workaround for OSP hosts that have lost their hostname
if not host_info["hostname"] and host.variables.get("openstack"):
host_info["hostname"] = host.variables["openstack"]["metadata"].get("fqdn")
return host_info
return _broker_args

@cached_property
def inventory(self):
Expand Down Expand Up @@ -422,6 +433,9 @@ def construct_host(self, provider_params, host_classes, **kwargs):
if "tower_inventory" in job_attrs
else self._translate_inventory(job.summary_fields.inventory)
}

misc_attrs["ip"] = kwargs.pop("ip", None)

job_attrs = helpers.flatten_dict(job_attrs)
logger.debug(job_attrs)
hostname, name, host_type = None, None, "host"
Expand Down
Loading