Skip to main content

Render jinja2 templates on the command line using shell environment variables

Project description

envtpl

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 %}

and compile it using

FOO=world MY_baz=qux MY_foo=bar envtpl hello.tpl

You end up with

hello = world
baz = qux
foo = bar

What's the point?

I use this script quite a lot in Docker images. Usually I'll have the CMD execute some file, like /bin/start_container, that sets up the runtime configuration for the container by inserting environment variables into config files before starting the main process. A redis example could look like this

#!/bin/bash
# start_container

envtpl /etc/redis.conf.tpl

redis-server

This is the use case I've optimised for, so that's why envtpl by default will delete the original template file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envtpl-0.7.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

envtpl-0.7.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file envtpl-0.7.2.tar.gz.

File metadata

  • Download URL: envtpl-0.7.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for envtpl-0.7.2.tar.gz
Algorithm Hash digest
SHA256 99598dfcf737ddd087afe53c0c7052fffc33c4575af4ee9ef4bf0c7943dc80a7
MD5 bbde50d5c7aceec248d876c8770ee70c
BLAKE2b-256 b9dc409aca9616032378c53b61ef07f46814b21e997b7746b4c836ab54e79b46

See more details on using hashes here.

File details

Details for the file envtpl-0.7.2-py3-none-any.whl.

File metadata

  • Download URL: envtpl-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for envtpl-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a66ee7fcc07907faca8bade0fab598b66c3c7041b3fa4ed20b495f48a014b2d8
MD5 9a871e53224f3a140b99b8e8603fd748
BLAKE2b-256 bd387e8f1f1251a6d544b3431eeadcc520fedfec2e69ea0adf18dd1ba316b548

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page