Implement support for setting host aliases

Hosts in Ansible are unique per playbook because there is no concept
of persistence between different playbook runs. A host could be named
"webserver" and be a completely different host in two playbook runs.

In order to make it possible to track a given host across different
playbook runs, it is now possible to set a host alias.
Host aliases are explicitely provided by the user as the "ara_host_alias"
hostvar which defaults to the inventory hostname if it is not set.

Change-Id: Iaa3b0ce57968b9e4977d25a559e21e67a37b1d62
This commit is contained in:
David Moreau Simard 2019-01-10 15:28:44 -05:00
parent b129ad91c7
commit 49ce188ab8
No known key found for this signature in database
GPG Key ID: CBEB466764A9E621
1 changed files with 6 additions and 9 deletions

View File

@ -178,7 +178,10 @@ class CallbackModule(CallbackBase):
)
# Record all the hosts involved in the play
self._load_hosts(play._variable_manager._inventory._restriction)
for host in play.hosts:
hostvars = play_vars["hostvars"][host] if host in play_vars["hostvars"] else {}
host_alias = hostvars["ara_host_alias"] if "ara_host_alias" in hostvars else host
self._get_or_create_host(host=host, host_alias=host_alias)
return self.play
@ -294,21 +297,15 @@ class CallbackModule(CallbackBase):
for file_ in files:
self._get_or_create_file(file_)
def _get_or_create_host(self, host):
def _get_or_create_host(self, host, host_alias=None):
self.log.debug("Getting or creating host: %s" % host)
query = dict(playbook=self.playbook["id"], name=host)
playbook_host = self._get_one_item("/api/v1/hosts", **query)
if not playbook_host:
# TODO: Implement logic for computing the host alias
playbook_host = self.client.post("/api/v1/hosts", name=host, alias=host, playbook=self.playbook["id"])
playbook_host = self.client.post("/api/v1/hosts", name=host, alias=host_alias, playbook=self.playbook["id"])
return playbook_host
def _load_hosts(self, hosts):
self.log.debug("Loading %s hosts(s)..." % len(hosts))
for host in hosts:
self._get_or_create_host(host)
def _load_result(self, result, status, **kwargs):
"""
This method is called when an individual task instance on a single