# how to include template file inside other template file ?

**URL:** <https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566>\
**Category:** Ansible Project\
**Created:** [January 22, 2023, 6:54am UTC](https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566 "2023-01-22T06:54:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [January 22, 2023, 6:54am UTC](https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566/1 "2023-01-22T06:54:56Z")

</div>

I have a template file called **main.conf.j2** and this is distributed to all of my servers. There additional server that need to have a single configuration that should include the configuration of the **main.conf.j2** file and additional configuration – I have named the file **extra.conf.j2**

My question is if there is a way to import the main.conf.j2 file inside the extra.conf.j2? the reason is that in case of a changes in the main.conf.j2 I want to manage only a single file

**main.conf.j2** – only in example

memory = {{memory.input}}

cpu = {{cpu.input}}

domain = {{domain.input}}

**extra.conf.j2** – only in example

number.of.server = {{num\_of\_servers.input}}

\< here I want to include the main.conf.j2\>

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 22, 2023, 10:22pm UTC](https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566/2 "2023-01-22T22:22:21Z")

</div>

I don’t know of a way to include one .j2 template into another, but you can have parts of your main.conf.j2 render only on a specific host, or only on hosts in specific groups. This achieves your goal of only having to manage a single file. (Also, I like to include a note to myself at the top of such files indicating where they came from.) Behold:

```auto
######################################################
# canonical source: roles/foo/templates/main.conf.j2 #
######################################################

{% if inventory_hostname == 'extra.server.name.net' %}
number.of.server = {{ num_of_servers.input }}
{% endif %}
memory = {{ memory.input }}
cpu = {{ cpu.input }}
domain = {{ domain.input }}

```

That will render with one extra line on host [extra.server.name.net](http://extra.server.name.net) but other than that identically on all your hosts (assuming those variable have the same value from each host’s perspective of course).

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 22, 2023, 10:52pm UTC](https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566/3 "2023-01-22T22:52:22Z")

</div>

It’s actually quite easy to include or import .j2 templates into other .j2 templates. There are some subtle and nuanced differences which you should understand before you go much further, but in your case a simple include will do the trick.

```auto
**# main.conf.j2**  **memory = {{ memory.input }}**  **cpu = {{ cpu.input }}**  **domain = {{ domain.input }}**

```

and

```auto
**# extra.conf.j2**  **number.of.server = {{ num_of_servers.input }}**  **{% include 'main.conf.j2' %}**

```

That’s it. You were within a few characters of the answer in your original question.

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [January 24, 2023, 6:40am UTC](https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566/4 "2023-01-24T06:40:10Z")

</div>

Thank you for the answer - It is working But …

When i’m managing both **main.conf.j2** and **extra.conf.j2** under the same folder everything works - But when i’m moving **main.conf.j2** to a different folder (/opt) and updating the template accordingly i’m getting an error that file can not be found

**# extra.conf.j2**  
number.of.server = {{ num\_of\_servers.input }}  
{% include ‘\*\*/opt/\*\*main.conf.j2’ %}

ב-יום שני, 23 בינואר 2023 בשעה 00:52:34 UTC+2, [uto...@gmail.com](mailto:uto...@gmail.com) כתב/ה:

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [January 24, 2023, 8:50am UTC](https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566/5 "2023-01-24T08:50:02Z")

</div>

IIRC this is not possible, when including templates you cannot  
reference templates outside of the template directory...

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 24, 2023, 11:10am UTC](https://forum.ansible.com/t/how-to-include-template-file-inside-other-template-file/37566/6 "2023-01-24T11:10:14Z")

</div>

However, you can make it work anyway if your **templates/main.conf.j2** is a symbolic link to **/opt/main.conf.j2**.
