Page 1 of 1

Templating Engine

Published: October 3, 2016 - 5:19 PM
by Aguay
Hello,

I'd like to know if there's some kind of templating engine.

For example, in a configuration file, you could put "{{ hostname }}" and it would retrieve the hostname.

This would allow for the generation of unique configuration files for each workstation. I haven't found much information on this. If anyone has any, I'd be interested.

And even if some people have workarounds, I'd be interested too.

Thanks in advance!

Re: Templating Engine

Published: October 5, 2016 - 11:50 AM
by Aguay
I'm going to answer myself in order to perhaps help other users :)

Jinja2 is present on each machine with the clients, so it is possible to use this library to create configuration templates.

Here is an excerpt from the package using Jinja2:

Code: Select all

from jinja2 import Environment, FileSystemLoader

THIS_DIR = os.getcwd()
j2_env = Environment(loader=FileSystemLoader(THIS_DIR),
                         trim_blocks=True)
template = j2_env.get_template('template.yml')
file = open('config.yml', 'w')
file.write(template.render(value="Une valeur"))
file.close()
And in my template.yml file there's something like this:

Code: Select all

# template.yml

server_url: https://exemple.com
agent_name : {{ value }}
Therefore, during execution, we can put a variable in the field of the value to be applied to the template.

This is a very simple example, but it might help ;)

Re: Templating Engine

Published: October 26, 2016 - 2:31 PM
by Ekouyoja
Thanks for the info, I didn't even know it included Jinja2 to be able to make templates, which can be very useful for managing workstations. :D