how to include template file inside other template file ?

556 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Jan 22, 2023, 1:54:57 AM1/22/23
to Ansible Project

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>

Todd Lewis

unread,
Jan 22, 2023, 5:22:34 PM1/22/23
to ansible...@googlegroups.com, uto...@gmail.com
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:
######################################################
# 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 but other than that identically on all your hosts (assuming those variable have the same value from each host's perspective of course).

Todd Lewis

unread,
Jan 22, 2023, 5:52:34 PM1/22/23
to ansible...@googlegroups.com, dudu.c...@gmail.com, uto...@gmail.com
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.
# main.conf.j2

memory = {{ memory.input }}
cpu = {{ cpu.input }}
domain = {{ domain.input }}

    and
# 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.


On 1/22/23 1:54 AM, dudu.c...@gmail.com wrote:

dudu.c...@gmail.com

unread,
Jan 24, 2023, 1:40:11 AM1/24/23
to Ansible Project
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 כתב/ה:

Dick Visser

unread,
Jan 24, 2023, 3:50:30 AM1/24/23
to ansible...@googlegroups.com
IIRC this is not possible, when including templates you cannot
reference templates outside of the template directory...
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7050c7db-ac06-4a96-8ee1-1f80eb56ac80n%40googlegroups.com.

Todd Lewis

unread,
Jan 24, 2023, 6:10:14 AM1/24/23
to Ansible Project
However, you can make it work anyway if your templates/main.conf.j2 is a symbolic link to /opt/main.conf.j2.
Reply all
Reply to author
Forward
0 new messages