From 49ce188ab88a6948db651163b9cd7973117fa2b1 Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Thu, 10 Jan 2019 15:28:44 -0500 Subject: [PATCH] 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 --- ara/plugins/callback/ara_default.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/ara/plugins/callback/ara_default.py b/ara/plugins/callback/ara_default.py index 83801cf..dd8bc4e 100644 --- a/ara/plugins/callback/ara_default.py +++ b/ara/plugins/callback/ara_default.py @@ -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