From 61a30e5f49c14319c43f9321631a7c3f6f8b6554 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 3 Dec 2014 07:26:42 -0500 Subject: [PATCH 1/2] better exception handling with delegated hosts --- lib/ansible/inventory/__init__.py | 4 ++-- lib/ansible/runner/__init__.py | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 7d279b7b4d..2048046d3c 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -420,7 +420,7 @@ class Inventory(object): group = self.get_group(groupname) if group is None: - raise Exception("group not found: %s" % groupname) + raise errors.AnsibleError("group not found: %s" % groupname) vars = {} @@ -439,7 +439,7 @@ class Inventory(object): host = self.get_host(hostname) if not host: - raise Exception("host not found: %s" % hostname) + raise errors.AnsibleError("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 f0de42764a..fad769c4ed 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -394,20 +394,20 @@ class Runner(object): actual_user = inject.get('ansible_ssh_user', self.remote_user) thisuser = None - if host in inject['hostvars']: - 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 + try: + if host in inject['hostvars']: + 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 + 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 errors.AnsibleException, 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 From 1c5f62529521ccf64b4c62629ceb171e6314d6e9 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 3 Dec 2014 14:19:11 -0500 Subject: [PATCH 2/2] corrected exception name --- lib/ansible/runner/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index fad769c4ed..5ee79e609c 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -404,7 +404,7 @@ class Runner(object): 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 errors.AnsibleException, e: + except errors.AnsibleError, e: # the hostname was not found in the inventory, so # we just ignore this and try the next method pass