From e938f554b74c35cc3a13f72bce46ebe5fb3aab3d Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 2 Dec 2014 10:58:14 -0500 Subject: [PATCH 1/2] better exception handling for unexpected exceptions --- lib/ansible/runner/__init__.py | 4 ++++ lib/ansible/utils/template.py | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 8da794ba6ff..b1652d86e34 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -733,6 +733,10 @@ class Runner(object): result = utils.jsonify(dict(changed=False, skipped=True)) self.callbacks.on_skipped(host, None) return ReturnData(host=host, result=result) + except errors.AnsibleError, e: + raise + except Exception, e: + raise errors.AnsibleError("Unexpected error while executing task: %s" % str(e)) # strip out any jinja2 template syntax within # the data returned by the lookup plugin diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index c2b14d8454b..3e7f5e4d811 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -89,13 +89,12 @@ def lookup(name, *args, **kwargs): tvars = kwargs.get('vars', None) if instance is not None: - # safely catch run failures per #5059 try: ran = instance.run(*args, inject=tvars, **kwargs) - except errors.AnsibleUndefinedVariable: + except errors.AnsibleError: raise except Exception, e: - ran = None + raise errors.AnsibleError('Unexpected error in during lookup: %s' % e) if ran: ran = ",".join(ran) return ran From 97408fe5b25b0cb2b58dfb34ffffd01c8da8fd51 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 4 Dec 2014 09:14:53 -0500 Subject: [PATCH 2/2] Revert "Make listify respect the global setting for undefined variables." This 'mostly' reverts commit 2769098fe7fcb51302cc8fabe9a1ff3f51aeec6f. Conflicts: lib/ansible/utils/__init__.py test/units/TestUtils.py --- lib/ansible/utils/__init__.py | 7 +------ test/units/TestUtils.py | 12 ++---------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 1541be5783c..a735e9c0b0e 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -48,7 +48,6 @@ import sys import json import subprocess import contextlib -import jinja2.exceptions from vault import VaultLib @@ -1469,15 +1468,11 @@ def listify_lookup_plugin_terms(terms, basedir, inject): # if not already a list, get ready to evaluate with Jinja2 # not sure why the "/" is in above code :) try: - new_terms = template.template(basedir, terms, inject, convert_bare=True, fail_on_undefined=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR) + new_terms = template.template(basedir, "{{ %s }}" % terms, inject) if isinstance(new_terms, basestring) and "{{" in new_terms: pass else: terms = new_terms - except errors.AnsibleUndefinedVariable: - raise - except jinja2.exceptions.UndefinedError, e: - raise errors.AnsibleUndefinedVariable('undefined variable in items: %s' % e) except: pass diff --git a/test/units/TestUtils.py b/test/units/TestUtils.py index d93fc70329b..99dd24565c6 100644 --- a/test/units/TestUtils.py +++ b/test/units/TestUtils.py @@ -566,17 +566,9 @@ class TestUtils(unittest.TestCase): def test_listify_lookup_plugin_terms(self): basedir = os.path.dirname(__file__) - # Straight lookups - self.assertEqual(ansible.utils.listify_lookup_plugin_terms('things', basedir, dict(things=[])), []) - self.assertEqual(ansible.utils.listify_lookup_plugin_terms('things', basedir, dict(things=['one', 'two'])), ['one', 'two']) - - # Variable interpolation - self.assertEqual(ansible.utils.listify_lookup_plugin_terms('things', basedir, dict(things=['{{ foo }}', '{{ bar }}'], foo="hello", bar="world")), - ['hello', 'world']) - with self.assertRaises(ansible.errors.AnsibleError) as ex: - ansible.utils.listify_lookup_plugin_terms('things', basedir, dict(things=['{{ foo }}', '{{ bar_typo }}'], foo="hello", bar="world")) - self.assertTrue("undefined variable in items: 'bar_typo'" in ex.exception.msg) + #self.assertEqual(ansible.utils.listify_lookup_plugin_terms('things', basedir, dict(things=[])), []) + #self.assertEqual(ansible.utils.listify_lookup_plugin_terms('things', basedir, dict(things=['one', 'two'])), ['one', 'two']) def test_deprecated(self): sys_stderr = sys.stderr