Render jinja2 templates on the command line using shell environment variables
Maintained. Last release 8 months ago, commits in 9 of the last 26 weeks. Weaker on release cadence.
pip install envtplenvtpl
Render jinja2 templates on the command line with shell environment variables
Installation
pip install envtpl
How-to
Say you have a configuration file called whatever.conf that looks like this
foo = 123
bar = "abc"
You can use envtpl to set foo and bar from the command line by creating a file called whatever.conf.tpl
foo = {{ FOO }}
bar = "{{ BAR }}"
If you run
FOO=123 BAR=abc envtpl < whatever.conf.tpl > whatever.conf
you'll get back the original whatever.conf.
You can also specify default values
foo = {{ FOO | default(123) }}
bar = "{{ BAR | default("abc") }}"
Running
FOO=456 envtpl < whatever.conf.tpl > whatever.conf
will generate
foo = 456
bar = "abc"
This is all standard Jinja2 syntax, so you can do things like
{% if BAZ is defined %}
foo = 123
{% else %}
foo = 456
{% endif %}
bar = "abc"
If an environment variable is missing, envtpl will throw an error
$ echo '{{ FOO }} {{ BAR }}' | FOO=123 envtpl
Error: 'BAR' is undefined
You can change this behaviour to insert empty strings instead by passing the --allow-missing flag.
Instead of reading from stdin and writing to stdout, you can pass the input filename as an optional positional argument,
and set the output filename with the --output-file (-o) argument.
envtpl -o whatever.conf whatever.conf.tpl
As a convenience, if you don't specify an output filename and the input filename ends with .tpl, the output filename will be the input filename without the .tpl extension, i.e.
envtpl whatever.conf.tpl
# is equivalent to
envtpl -o whatever.conf whatever.conf.tpl
By default, envtpl will delete the input template file. You can keep it by passing the --keep-template flag.
There's a special environment(prefix='') function that you can use as a kind of wildcard variable. If you have hello.tpl
hello = {{ FOO }}
{% for key, value in environment('MY_') %}{{ key }} = {{ value }}
{% endfor %}