# Problem with includes in templates

**URL:** <https://forum.ansible.com/t/problem-with-includes-in-templates/14998>\
**Category:** Ansible Project\
**Created:** [April 11, 2014, 10:05am UTC](https://forum.ansible.com/t/problem-with-includes-in-templates/14998 "2014-04-11T10:05:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Klein](https://avatars.discourse-cdn.com/v4/letter/t/8e8cbc/32.png) [@Thomas\_Klein](https://forum.ansible.com/u/Thomas_Klein)\
**Post date:** [April 11, 2014, 10:05am UTC](https://forum.ansible.com/t/problem-with-includes-in-templates/14998/1 "2014-04-11T10:05:51Z")

</div>

Hi,

i’m working with ansible 1.5 and encounter a problem when working with includes in templates.

In one template file i have this code snippet:  
{% for group in group\_names %}  
{% include “debug\_include.j2” with context %}  
{% endfor %}

In the included template ‘debug\_include.j2’ i have this code:  
This is the included Template. The current group is {{group}}

The documentation claims that included templates have the same variable context as the main file.  
So i would expect to see something like:  
This is the included Template. The current group is mailserver

Instead i get an error on the commandline when executing this play:  
TASK: [common-linux | dump all variables] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ltesthost] =\> {‘msg’: “One or more undefined variables: ‘group’ is undefined”, ‘failed’: True}  
fatal: [testhost] =\> {‘msg’: “One or more undefined variables: ‘group’ is undefined”, ‘failed’: True}

FATAL: all hosts have already failed – aborting

Did i miss somthing here ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 9:09pm UTC](https://forum.ansible.com/t/problem-with-includes-in-templates/14998/2 "2014-04-15T21:09:12Z")

</div>

Yep, this is a known thing, Jinja2 includes (however those work) aren’t well tolerated in Ansible right now.

History in github indicates these used to work – so I’d welcome a ticket. A pull request to add them would be even better.

(They are not very widely used/wanted at all, so it may take a while to get to this).

---

<div class="post-metadata">

**Author:** ![Brice\_Burgess](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Brice\_Burgess](https://forum.ansible.com/u/Brice_Burgess)\
**Post date:** [April 17, 2014, 5:00pm UTC](https://forum.ansible.com/t/problem-with-includes-in-templates/14998/3 "2014-04-17T17:00:59Z")

</div>

FWIW jinja2/template includes are working for me in 1.5 with a few notes;

- context is automatically inherited, no need to pass with context

- {% include filename %} only accepts relative paths for filename. eg.

- {% include “inc/filename.j2” %} works,
- {% include “/etc/ansible/templates/inc/filename.j2” %} does not

Here’s an example;

Main template;  
[https://github.com/iceburg-net/ansible-pcd/blob/master/roles/applications/apache\_sites/templates/virtualhost\_http.j2](https://github.com/iceburg-net/ansible-pcd/blob/master/roles/applications/apache_sites/templates/virtualhost_http.j2)

Included template;  
[https://github.com/iceburg-net/ansible-pcd/blob/master/roles/applications/apache\_sites/templates/includes/wordpress-3.j2](https://github.com/iceburg-net/ansible-pcd/blob/master/roles/applications/apache_sites/templates/includes/wordpress-3.j2)

[ps the pcd project is in an embarrassing state && needs a reorg/refactor 🙂]
