From dbd755e0f45d157ddf6326252043fa60f7ee80e7 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 4 Aug 2015 22:49:42 -0400 Subject: [PATCH] Fix jinja2 template search pathing Fixes #9933 --- lib/ansible/plugins/action/template.py | 5 +++++ lib/ansible/plugins/lookup/template.py | 7 +++++++ lib/ansible/template/__init__.py | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py index 81b291ff49..358d7d3164 100644 --- a/lib/ansible/plugins/action/template.py +++ b/lib/ansible/plugins/action/template.py @@ -111,6 +111,11 @@ class ActionModule(ActionBase): time.localtime(os.path.getmtime(source)) ) + self._templar.environment.searchpath = [self._loader._basedir, os.path.dirname(source)] + if self._task._role is not None: + self._templar.environment.searchpath.insert(1, C.DEFAULT_ROLES_PATH) + self._templar.environment.searchpath.insert(1, self._task._role._role_path) + old_vars = self._templar._available_variables self._templar.set_available_variables(temp_vars) resultant = self._templar.template(template_data, preserve_trailing_newlines=True, convert_data=False) diff --git a/lib/ansible/plugins/lookup/template.py b/lib/ansible/plugins/lookup/template.py index 311904b280..8f793a2066 100644 --- a/lib/ansible/plugins/lookup/template.py +++ b/lib/ansible/plugins/lookup/template.py @@ -19,6 +19,7 @@ __metaclass__ = type import os +from ansible import constants as C from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase @@ -41,6 +42,12 @@ class LookupModule(LookupBase): if lookupfile and os.path.exists(lookupfile): with open(lookupfile, 'r') as f: template_data = f.read() + + self._templar.environment.searchpath = [self._loader._basedir, os.path.dirname(lookupfile)] + if 'role_path' in variables: + self._templar.environment.searchpath.insert(1, C.DEFAULT_ROLES_PATH) + self._templar.environment.searchpath.insert(1, variables['role_path']) + res = self._templar.template(template_data, preserve_trailing_newlines=True) ret.append(res) else: diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 574359955d..ec9866c314 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -82,7 +82,7 @@ class Templar: undefined=StrictUndefined, extensions=self._get_extensions(), finalize=self._finalize, - loader=FileSystemLoader('.'), + loader=FileSystemLoader(self._basedir), ) self.environment.template_class = AnsibleJ2Template