# Extends template with ansible

**URL:** <https://forum.ansible.com/t/extends-template-with-ansible/20018>\
**Category:** Ansible Project\
**Created:** [December 15, 2015, 11:48am UTC](https://forum.ansible.com/t/extends-template-with-ansible/20018 "2015-12-15T11:48:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christophe\_Biguereau](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@Christophe\_Biguereau](https://forum.ansible.com/u/Christophe_Biguereau)\
**Post date:** [December 15, 2015, 11:48am UTC](https://forum.ansible.com/t/extends-template-with-ansible/20018/1 "2015-12-15T11:48:58Z")

</div>

Hi all,

I try to simplify my Nginx template by using extends template. To do that, I follow the Jinja2 [doc](http://jinja.pocoo.org/docs/dev/templates/#block-nesting-and-scope)

I create a child template:

**{% extends “nginx\_servers.j2” %}**  
**{% block listen %}**  
**listen {{ item.server.listen }};**  
**{% endblock %}**  
**{% block servername %}**  
**server\_name {{ item.server.server\_name }};**  
**{% endblock %}**  
**{% block root %}**

**# Root directory**  
**root {{ item.server.root }};**  
**{% endblock %}**  
**{% block ssl %}**

**# Ssl configuration**  
**ssl on;**  
**ssl\_certificate {{ item.server.ssl\_certificate }};**  
**ssl\_certificate\_key {{ item.server.ssl\_certificate\_key }};**  
**{% endblock %}**  
**{% block logs %}**

**# Logs**  
**access\_log {{ item.server.access\_log }};**  
**error\_log {{ item.server.error\_log }};**  
**{% endblock %}**  
**{% block location %}**  
**location {{ loc.name }} {**  
**{% for k,v in loc %}**  
**{{ k }} {{ v }};**  
**{% endfor %}**  
**}**  
**{% endblock %}**

Then I create my vhosts template ( nginx\_servers.j2 ) **:**

**server {**  
**{% block listen %}{% endblock %}**  
**{% if ‘server\_name’ in item.server %}**  
**{% block servername %}{% endblock %}**  
**{% endif %}**  
**{%if ‘root’ in item.server %}**  
**{% block root %}{% endblock %}**  
**{% endif %}**  
**{% if ‘ssl’ in item.server %}**  
**{% block ssl %}{% endblock %}**  
**{% endif %}**  
**{% if ‘access\_log’ in item.server %}**  
**{% block logs %}{% endblock %}**  
**{% endif %}**  
**{% for loc in item %}**  
**{% if loc.find(‘location’) != -1 %}**  
**{% block**  **location scoped %}{{ loc }}{% endblock %}**  
**{% endif %}**  
**{% endfor %}**  
**}**

My variable file:

**nginx\_servers:**  
**- server:**  
**name: “site1”**  
**listen: 80**  
**server\_name: “site1”**  
**location1:**  
**name: “/”**  
**try\_files: “$uri $uri/ /index.php”**  
**location2:**  
**name: “bla”**  
**auth\_basic: “"Restricted Area"”**  
**auth\_basic\_user\_file: “xxxxxxxxxxx”**  
**- server:**  
**name: “site2”**  
**listen: 443**  
**server\_name: “site2”**  
**location1:**  
**name: “/”**  
**try\_files: “$uri $uri/ /index.php”**  
**location2:**  
**name: “~ \.ini$”**  
**deny: “all”**  
**location3:**  
**name: “~ /\.ht”**  
**allow: “all”**

**}**

All the simple block works fine ( servername, listen … ), but when I run the location block, I get this error :  
**fatal: [127.0.0.1] =\> {‘msg’: “KeyError: ‘undefined variable: 0’”, ‘failed’: True}**

I try to just print loc variable in the block, but I get the same error.

Does this feature is available in Ansible ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Christophe\_Biguereau](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@Christophe\_Biguereau](https://forum.ansible.com/u/Christophe_Biguereau)\
**Post date:** [December 17, 2015, 8:32am UTC](https://forum.ansible.com/t/extends-template-with-ansible/20018/2 "2015-12-17T08:32:57Z")

</div>

Does anybody already use extends with variable in ansible ?
