levant
levant copied to clipboard
template: Add include function
The include function allows templates to be composed of other templates.
The motivation for this change is to allow composition of complex templates from simpler components. This is currently difficult because the only mechanism available for composing a template from multiple files is the fileContents function, which copies contents of files ad-verbatim. This requires that all parameters of the component files are known beforehand, which is often not the case, and does not allow the component templates to use inputs from variables.
Fixes #305
Hi @jrasell @angrycub, is this something you would consider including in the project? My company is planning on using levant extensively in our deployment stack and this would be a great help in keeping templates composable & DRY.
I'm happy to make any updates needed to get this in line with the project's standards. Thanks!
@atavakoliyext thanks for this PR; i'll try and get a first pass review done this week!
@jrasell Thank you!
@jrasell thanks for taking a look. I'm familiar with that feature of text/template, but was unable to find a way to use it in levant without putting those defines in the same files that use them. Since one of motivations of this PR is to let users define reusable snippets elsewhere and use them across multiple jobs, I went with a custom function to give a way to specify the paths to those other files.
Another approach could be to add a new flag for specifying additional template files to load before executing the main template (e.g. a -templates=<dir> that loads any <dir>/_*.tpl files first). That would bring this feature closer to how nomad-pack does it. The downside would be that users would need to be aware of the locations of any additional template files at rendering time, rather than that being a static property of the template itself. nomad-pack doesn't have this downside because the structure of packs enforces where those helpers must exist.
Hi @jrasell Do you have any further thoughts on the above?
Looking into reducing boilerplate and standardization of database deployment in my nomad jobs. The template function would be so useful!
I don't see myself using levant without that. ~~Maybe some multi-pass wrapper with fileContents, assuming that will work.~~ Just discovered this is HCL1 only, so maybe for ancient nomad versions. Jinja/ansible looks like a way to go at the moment.
It was hard not to notice the project is not maintained anymore. No HCL2 support (https://github.com/hashicorp/levant/pull/398), really? Not sure what this mysterious CRT onboarding is (#447), but maybe the start of the thing levant need to bounce back.
Bummer.
I understand nomad-pack is the futur of jobfile rendering, but, waiting for it to be production ready, any chance to have this feature merged ? Levant is used today, and will likely still be in a few years.