1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #9286 from damncabbage/9008-undef-var-in-list

Makes listify_lookup_plugin_terms respect the global setting for undefined variables.
This commit is contained in:
James Cammarata 2014-10-08 13:01:47 -05:00
commit ad893ea867
2 changed files with 13 additions and 1 deletions

View file

@ -48,6 +48,7 @@ import sys
import json
import subprocess
import contextlib
import jinja2.exceptions
from vault import VaultLib
@ -1419,11 +1420,13 @@ 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, "{{ %s }}" % terms, inject)
new_terms = template.template(basedir, terms, inject, convert_bare=True, fail_on_undefined=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR)
if isinstance(new_terms, basestring) and "{{" in new_terms:
pass
else:
terms = new_terms
except jinja2.exceptions.UndefinedError, e:
raise errors.AnsibleUndefinedVariable('undefined variable in items: %s' % e)
except:
pass

View file

@ -541,11 +541,20 @@ 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)
def test_deprecated(self):
sys_stderr = sys.stderr
sys.stderr = StringIO.StringIO()