Package: jinjanator
jinjanator: CLI tool for rendering Jinja2 templatesFeatures:Jinja2 templatingINI, YAML, JSON data sources supportedEnvironment variables can be used with or without data filesPlugins can provide additional formats, filters, tests, and global
functions (seejinjanator-pluginsfor details)Installationpip install jinjanatorAvailable Pluginsjinjanator-plugin-ansible-
makes Ansible's 'core' filters and tests available during template
renderingjinjanator-plugin-format-toml-
provides a TOML parser for input data filesjinjanator-plugin-format-xml-
provides an XML parser for input data filesTutorialSuppose you have an NGINX configuration file template,nginx.j2:server {
  listen 80;
  server_name {{ nginx.hostname }};

  root {{ nginx.webroot }};
  index index.htm;
}And you have a JSON file with the data,nginx.json:{"nginx":{"hostname":"localhost","webroot":"/var/www/project"}}This is how you render it into a working configuration file:$jinjanatenginx.j2nginx.json>nginx.confThe output is saved tonginx.conf:server {
  listen 80;
  server_name localhost;

  root /var/www/project;
  index index.htm;
}Alternatively, you can use the-o nginx.confor--output-file nginx.confoptions to write directly to the file.Tutorial with environment variablesSuppose, you have a very simple template,person.xml.j2:<data><name>{{ name }}</name><age>{{ age }}</age></data>What is the easiest way to use jinjanator here?
Use environment variables in your Bash script:$exportname=Andrew
$exportage=31$jinjanate/tmp/person.xml.j2
<data><name>Andrew</name><age>31</age></data>Using environment variablesEven when you use a data file as the data source, you can always
access environment variables using theenv()function:Username: {{ login }}
Password: {{ env("APP_PASSWORD") }}Or, if you prefer, as a filter:Username: {{ login }}
Password: {{ "APP_PASSWORD" | env }}CLI Referencejinjanateaccepts the following arguments:template: Jinja2 template file to renderdata: (optional) path to the data used for rendering.
The default is-: use stdin.Options:--format FMT, -f FMT: format for the data file. The default is?: guess from file extension. Supported formats are YAML (.yaml or
.yml), JSON (.json), INI (.ini), and dotenv (.env), plus any formats
provided by plugins you have installed.--format-option OPT: option to be passed to the parser for the
data format selected with--format(or auto-selected). This can be
specified multiple times. Refer to the documentation for the format
itself to learn whether it supports any options.--help, -h: generates a help message describing usage of the tool.--import-env VAR, -e VAR: import all environment variables into
the template asVAR.  To import environment variables into the
global scope, give it an empty string:--import-env=.  (This
will overwrite any existing variables with the same names!)--output-file OUTFILE, -o OUTFILE: Write rendered template to a
file.--quiet: Avoid generating any output on stderr.--undefined: Allow undefined variables to be used in templates (no
error will be raised).--version: prints the version of the tool and the Jinja2 package installed.There is some special behavior with environment variables:Whendatais not provided (data is-),--formatdefaults toenvand thus reads environment variables.Usage ExamplesRender a template using INI-file data source:$ jinjanate config.j2 data.iniRender using JSON data source:$ jinjanate config.j2 data.jsonRender using YAML data source:$ jinjanate config.j2 data.yamlRender using JSON data on stdin:$ curl http://example.com/service.json | jinjanate --format=json config.j2 -Render using environment variables:$ jinjanate config.j2Or use environment variables from a file:$ jinjanate config.j2 data.envOr pipe it: (note that you'll have to use "-" in this particular case):$ jinjanate --format=env config.j2 - < data.envData FormatsdotenvData input from environment variables.OptionsThis format does not support any options.UsageRender directly from the current environment variable values:$ jinjanate config.j2Or alternatively, read the values from a dotenv file:NGINX_HOSTNAME=localhost
NGINX_WEBROOT=/var/www/project
NGINX_LOGS=/var/log/nginx/And render with:$ jinjanate config.j2 data.envOr:$ env | jinjanate --format=env config.j2If you're going to pipe a dotenv file intojinjanate, you'll need to
use "-" as the second argument:$ jinjanate config.j2 - < data.envINIINI data input format.OptionsThis format does not support any options.Usagedata.ini:[nginx]
hostname=localhost
webroot=/var/www/project
logs=/var/log/nginxUsage:$ jinjanate config.j2 data.iniOr:$ cat data.ini | jinjanate --format=ini config.j2JSONJSON data input format.Optionsarray-name: accepts a single string (e.g.array-name=foo), which
must be a valid Python identifier and not a Python keyword. If this
option is specified, and the JSON data provided is anarray(sequence, list), the specified name will be used to make the data
available to the Jinja2 template. Errors will be generated ifarraydata is provided and this option is not specified, or if
this option is specified and the data provided is anobject.Usagedata.json:{
    "nginx":{
        "hostname": "localhost",
        "webroot": "/var/www/project",
        "logs": "/var/log/nginx"
    }
}Usage:$ jinjanate config.j2 data.jsonOr:$ cat data.json | jinjanate --format=ini config.j2YAMLYAML data input format.Optionssequence-name: accepts a single string (e.g.sequence-name=foo),
which must be a valid Python identifier and not a Python keyword. If
this option is specified, and the YAML data provided is asequence(array, list), the specified name will be used to make the data
available to the Jinja2 template. Errors will be generated ifsequencedata is provided and this option is not specified, or if
this option is specified and the data provided is amapping.Usagedata.yaml:nginx:
  hostname: localhost
  webroot: /var/www/project
  logs: /var/log/nginxUsage:$ jinjanate config.j2 data.ymlOr:$ cat data.yml | jinjanate --format=yaml config.j2Filtersenv(varname, default=None)Use an environment variable's value in the template.This filter is available even when your data source is something other
than the environment.Example:User: {{ user_login }}
Pass: {{ "USER_PASSWORD" | env }}You can provide a default value:Pass: {{ "USER_PASSWORD" | env("-none-") }}For your convenience, it's also available as a global function:User: {{ user_login }}
Pass: {{ env("USER_PASSWORD") }}Notice that there must be quotes around the environment variable name
when it is a literal string.Release InformationAdditionsAdded Python 3.12 support.
[#23](https://github.com/kpfleming/jinjanator/issues/23)→ Full Changelog