From 8de25db6813eaa5c46b86e709b3925035e6f9995 Mon Sep 17 00:00:00 2001 From: Kei Nohguchi Date: Mon, 16 May 2016 07:05:08 -0700 Subject: [PATCH] net_template.py: Fix jinja2 template file search path (#15134) The change is needed to support the multiple include statements inside the jinja2 template file, as in '{% include ['another.j2'] %}'. statement. I need this capability, as OpenSwitch `switch` role needs to handle multiple *.j2 files and supporting the include statement inside jinja2 file is essential, otherwise I need to combine multiple template files into a single file, which easily causes conflicts between developers working on different parts of the teamplate, ports and interface. --- lib/ansible/plugins/action/net_template.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/ansible/plugins/action/net_template.py b/lib/ansible/plugins/action/net_template.py index c626c8dc6bc..3d27cac5f25 100644 --- a/lib/ansible/plugins/action/net_template.py +++ b/lib/ansible/plugins/action/net_template.py @@ -93,6 +93,17 @@ class ActionModule(ActionBase): except IOError: return dict(failed=True, msg='unable to load src file') + # Create a template search path in the following order: + # [working_path, self_role_path, dependent_role_paths, dirname(source)] + searchpath = [working_path] + if self._task._role is not None: + searchpath.append(self._task._role._role_path) + dep_chain = self._task._block.get_dep_chain() + if dep_chain is not None: + for role in dep_chain: + searchpath.append(role._role_path) + searchpath.append(os.path.dirname(source)) + self._templar.environment.loader.searchpath = searchpath self._task.args['src'] = self._templar.template(template_data)