From 82c95e07b5557c1352bdcb783e91db80a553c92b Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Mon, 7 Jan 2019 11:33:39 +0100 Subject: [PATCH] Fix searchpath in the template lookup (#50488) --- changelogs/fragments/template-lookup-searchpath-fix.yaml | 2 ++ lib/ansible/plugins/lookup/template.py | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/template-lookup-searchpath-fix.yaml diff --git a/changelogs/fragments/template-lookup-searchpath-fix.yaml b/changelogs/fragments/template-lookup-searchpath-fix.yaml new file mode 100644 index 00000000000..7718ca4756e --- /dev/null +++ b/changelogs/fragments/template-lookup-searchpath-fix.yaml @@ -0,0 +1,2 @@ +bugfixes: + - Fix searchpath in the template lookup to work the same way as in the template module. diff --git a/lib/ansible/plugins/lookup/template.py b/lib/ansible/plugins/lookup/template.py index e649dd034a8..a5b2d8a16ce 100644 --- a/lib/ansible/plugins/lookup/template.py +++ b/lib/ansible/plugins/lookup/template.py @@ -77,7 +77,7 @@ class LookupModule(LookupBase): template_data = to_text(f.read(), errors='surrogate_or_strict') # set jinja2 internal search path for includes - searchpath = variables.get('ansible_search_path') + searchpath = variables.get('ansible_search_path', []) if searchpath: # our search paths aren't actually the proper ones for jinja includes. # We want to search into the 'templates' subdir of each search path in @@ -87,8 +87,8 @@ class LookupModule(LookupBase): newsearchpath.append(os.path.join(p, 'templates')) newsearchpath.append(p) searchpath = newsearchpath - else: - searchpath = [self._loader._basedir, os.path.dirname(lookupfile)] + searchpath.insert(0, os.path.dirname(lookupfile)) + self._templar.environment.loader.searchpath = searchpath if variable_start_string is not None: self._templar.environment.variable_start_string = variable_start_string