diff --git a/lib/ansible/plugins/lookup/template.py b/lib/ansible/plugins/lookup/template.py index 4ab0ad1ed02..e335b9339f3 100644 --- a/lib/ansible/plugins/lookup/template.py +++ b/lib/ansible/plugins/lookup/template.py @@ -49,6 +49,14 @@ class LookupModule(LookupBase): # set jinja2 internal search path for includes if 'ansible_search_path' in variables: searchpath = variables['ansible_search_path'] + # 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 + # addition to our original search paths. + newsearchpath = [] + for p in searchpath: + newsearchpath.append(os.path.join(p, 'templates')) + newsearchpath.append(p) + searchpath = newsearchpath else: searchpath = [self._loader._basedir, os.path.dirname(lookupfile)] self._templar.environment.loader.searchpath = searchpath diff --git a/test/integration/targets/lookups/tasks/main.yml b/test/integration/targets/lookups/tasks/main.yml index 86a627b33c5..ddea035d1b2 100644 --- a/test/integration/targets/lookups/tasks/main.yml +++ b/test/integration/targets/lookups/tasks/main.yml @@ -268,3 +268,14 @@ - product.results[6]['item'] == ["C", "1"] - product.results[7]['item'] == ["C", "2"] - product.results[8]['item'] == ["C", "3"] + +# Template lookups + +# ref #18526 +- name: Test that we have a proper jinja search path in template lookup + set_fact: + hello_world: "{{ lookup('template', 'hello.txt') }}" + +- assert: + that: + - "hello_world|trim == 'Hello world!'" diff --git a/test/integration/targets/lookups/templates/hello.txt b/test/integration/targets/lookups/templates/hello.txt new file mode 100644 index 00000000000..be15a4f3334 --- /dev/null +++ b/test/integration/targets/lookups/templates/hello.txt @@ -0,0 +1 @@ +Hello {% include 'world.txt' %}! diff --git a/test/integration/targets/lookups/templates/world.txt b/test/integration/targets/lookups/templates/world.txt new file mode 100644 index 00000000000..cc628ccd107 --- /dev/null +++ b/test/integration/targets/lookups/templates/world.txt @@ -0,0 +1 @@ +world