From d19fe8d95d8e30a4e6c0dab319f2f76302e78a15 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Fri, 10 Oct 2014 01:18:18 -0500 Subject: [PATCH] Fetch vars for host directly when calculating the delegated user This fixes the case in which the delegated to host may not be in the specified hosts list, in which cases facts/vars for the host were not available in the injected hostvars. This also fixes the inventory variable fetching function, so that an unknown host raises a proper error as opposed to a NoneType exception. Fixes #8224 --- lib/ansible/inventory/__init__.py | 5 ++++- lib/ansible/runner/__init__.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 972042c9b14..7d279b7b4dc 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -437,7 +437,10 @@ class Inventory(object): def get_variables(self, hostname, update_cached=False, vault_password=None): - return self.get_host(hostname).get_variables() + host = self.get_host(hostname) + if not host: + raise Exception("host not found: %s" % hostname) + return host.get_variables() def get_host_variables(self, hostname, update_cached=False, vault_password=None): diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 831e5d10e22..41e5d6054da 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -367,6 +367,16 @@ class Runner(object): if inject['hostvars'][host].get('ansible_ssh_user'): # user for delegate host in inventory thisuser = inject['hostvars'][host].get('ansible_ssh_user') + else: + # look up the variables for the host directly from inventory + try: + host_vars = self.inventory.get_variables(host, vault_password=self.vault_pass) + if 'ansible_ssh_user' in host_vars: + thisuser = host_vars['ansible_ssh_user'] + except Exception, e: + # the hostname was not found in the inventory, so + # we just ignore this and try the next method + pass if thisuser is None and self.remote_user: # user defined by play/runner