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
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:
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).
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.
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