template import from outside of templates dir

761 views
Skip to first unread message

Jacob Weber

unread,
Feb 11, 2015, 2:11:08 PM2/11/15
to ansible...@googlegroups.com
One of my roles uses a template, which has an {% import ... %} statement to include data from another template.

I'd like this other template to be outside of the role's "templates" directory, since it will be shared by multiple roles.

If I try including a relative path:
  {% import ../../../foo.j2 as foo with context %}
it fails with the error "Cannot find/not allowed to load (include) template". I think this is because Jinja2 won't include paths with ".." in them.

But it also fails if I include an absolute path, using playbook_dir:

  {% import playbook_dir ~ '/foo.j2' as foo with context %}

Why would that second version fail? Is there another way to do this?

Thanks,
Jacob

Giovanni Tirloni

unread,
Feb 12, 2015, 8:59:08 PM2/12/15
to ansible...@googlegroups.com
On Wed, 11 Feb 2015 11:11 -0800, Jacob Weber <weber...@gmail.com>
wrote:
> One of my roles uses a template, which has an {% import ... %} statement
> to
> include data from another template.
>
> I'd like this other template to be outside of the role's "templates"
> directory, since it will be shared by multiple roles.
>
> If I try including a relative path:
> {% import ../../../foo.j2 as foo with context %}
> it fails with the error "Cannot find/not allowed to load (include)
> template". I think this is because Jinja2 won't include paths with ".."
> in
> them.
>
> But it also fails if I include an absolute path, using playbook_dir:
>
> {% import playbook_dir ~ '/foo.j2' as foo with context %}

The "template" plugin will use the runner's basedir, so try to specify a
path relative to the playbook's directory.

Giovanni

Jacob Weber

unread,
Feb 12, 2015, 10:05:53 PM2/12/15
to ansible...@googlegroups.com
Thanks -- that worked, e.g.:

{% import 'roles/other_role/templates/foo.j2' as foo with context %}

Jacob

Reply all
Reply to author
Forward
0 new messages