Jinja2 template substitution tool
Project description
j2subst - Jinja2 template substitution tool
j2subst is a command-line tool for processing Jinja2 templates with configuration data from multiple sources. It's designed for use in CI/CD pipelines and configuration management workflows.
Features
- Multiple configuration formats: Supports YAML, JSON, and TOML configuration files
- Flexible template resolution: Automatic template path resolution with placeholders
- Environment variables: Access to environment variables within templates
- Python module integration: Import Python modules for advanced template logic
- Built-in functions & filters: Comprehensive set of built-in Jinja2 filters and functions
- CI/CD aware: Special behavior for CI/CD environments
- Dump mode: Export configuration data for debugging and inspection
Installation
From PyPI
pip install j2subst
Using Docker
docker pull docker.io/rockdrilla/j2subst
Quick start
Basic usage
Process a single template file:
j2subst template.j2
This will process template.j2 and output to template (removing the .j2 extension).
Input/output specification
Process template with explicit input and output:
j2subst input.j2 output.txt
Use stdin/stdout:
cat template.j2 | j2subst - > output.txt
j2subst input.j2 -
Directory processing
Process all templates in a directory (no recursion):
j2subst /path/to/templates/
Control recursion depth:
j2subst --depth 3 /path/to/templates/
Example template
# config.j2
server_name: {{ server.name }}
database_url: {{ database.url }}
environment: {{ env.ENVIRONMENT }}
With configuration file config.yml:
server:
name: myserver
database:
url: postgresql://localhost/mydb
Along with dictionaries above, the following variables are available:
{{ is_ci }}-Trueif running in CI/CD environment,Falseotherwise{{ j2subst_file }}- path to the currently processed template{{ j2subst_origin }}- directory of the currently processed template
Nota bene: j2subst_file and j2subst_origin are set to None when processing template from stdin.
Example:
{% if is_ci %}
Running in CI environment
{% endif %}
{%- if j2subst_origin %}
{#- accessing files "near" template file -#}
{%- endif %}
Docker usage
Docker image docker.io/rockdrilla/j2subst has several extra things done:
- entrypoint is set to
j2substscript; - current working directory is set to "
/w" and it's volume. - environment variable
J2SUBST_PYTHON_MODULESis set to"netaddr psutil"- these packages are installed viapipand they are not dependencies in any kind, but provided for better usability; see filedocker/requirements-extra.txt.
To simplify usage, the one may define shell alias:
alias j2subst='docker run --rm -v "${PWD}:/w" docker.io/rockdrilla/j2subst '
Configuration
Configuration files/directories
j2subst can load configuration from multiple sources:
j2subst -c config.yml template.j2
j2subst -c /path/to/configs/ template.j2
Supported formats:
- YAML (
.yaml,.yml) - JSON (
.json) - TOML (
.toml)
Template paths
Specify template search paths:
j2subst -t /custom/templates:/other/templates template.j2
Default template path is "@{ORIGIN}:@{CWD}".
Template path placeholders:
@{CWD}- current working directory@{ORIGIN}- directory containing the currently processed template file. This placeholder dynamically updates when processing multiple templates, always pointing to the directory of the template currently being rendered.
Nota bene: @{ORIGIN} is unavailable when processing template from stdin.
Command line options
Core options
--verbose, -v- Increase verbosity (can be used multiple times)--quiet, -q- Enable quiet mode (overrides "--verbose")--debug, -D- Enable debug mode (prints debug messages to stderr; overrides "--quiet")--strict, -s- Enable strict mode (warnings become errors)--force, -f- Enable force mode (overwrite existing files)--unlink, -u- Delete template files after processing
Configuration options
--config-path, -c PATH- Colon-separated list of config files/directories--template-path, -t PATH- Colon-separated list of template directories--depth, -d INTEGER- Set recursion depth for directory processing (1-20)
Advanced options
-
--dump [FORMAT]- Dump configuration to stdout (YAML/JSON) and exit -
--python-modules LIST- Space-separated list of Python modules to import
Help options
--help-cicd- Show help for CI/CD behavior--help-click- Show help for Click behavior--help-dump- Show help for dump mode--help-env- Show help for environment variables--help-template-path- Show help for template paths
Environment variables
Corresponding environment variables are also supported:
|------------------------+------------------+---------|
| Environment variable | Flag option | Type |
|------------------------+------------------+---------|
| J2SUBST_VERBOSE | --verbose | integer |
| J2SUBST_QUIET | --quiet | flag |
| J2SUBST_DEBUG | --debug | flag |
| J2SUBST_STRICT | --strict | flag |
| J2SUBST_FORCE | --force | flag |
| J2SUBST_UNLINK | --unlink | flag |
| J2SUBST_DEPTH | --depth | integer |
| J2SUBST_CONFIG_PATH | --config-path | string |
| J2SUBST_TEMPLATE_PATH | --template-path | string |
| J2SUBST_PYTHON_MODULES | --python-modules | string |
|------------------------+------------------+---------|
See Click documentation for more details about how Click handles environment variables, especially for flag options.
CI/CD behavior
- option
--depth/ variableJ2SUBST_DEPTHdefaults to20if running in CI/CD and1otherwise. - if argument list is empty then it set to current working directory.
Built-in Python modules
The following Python modules are available by default in templates:
datetimehashlibos_path(alias foros.path)pathlibresecretsstring
Built-in functions
Available built-in functions:
bool,filter,isinstance,len,list,repr,set,sorted,str,type
Reference: Built-in Functions
Built-in filters
Along with default Jinja2 filters, J2subst provides extra filters, see dedicated documentation here.
Direct link to GitHub: doc/filters.md
Examples
Dump configuration
j2subst --dump
j2subst --dump json
j2subst -c config.yml --dump
Import custom Python modules
j2subst --python-modules "myjson:json math" template.j2
This imports:
jsonmodule asmyjsonmathmodule asmath
Development
Building Docker image
export IMAGE_VERSION=wip
./docker/build-scripts/image-base.sh
./docker/build-scripts/image.sh
License
Apache-2.0
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file j2subst-0.0.6.tar.gz.
File metadata
- Download URL: j2subst-0.0.6.tar.gz
- Upload date:
- Size: 26.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c31ae5917fe7b86e3498694fe4cc4870c1667df99eec216fe495e5774118fc14
|
|
| MD5 |
087708942da419c236aeb14783e1fffa
|
|
| BLAKE2b-256 |
7ce48946e145062ca3bdb208a4fb6e409396ae4e4ad82aab9b83c5e5db3da965
|
File details
Details for the file j2subst-0.0.6-py3-none-any.whl.
File metadata
- Download URL: j2subst-0.0.6-py3-none-any.whl
- Upload date:
- Size: 27.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5ca530b95c39db61158b9afbe7800b82f09d96378d9ec2893a998d8b7835ef3
|
|
| MD5 |
fd1a83fcd3118522379f997e1d4f144d
|
|
| BLAKE2b-256 |
29036b0f15b8514ddee499c0529f179d83666f4dc43eb0e4c992577163887402
|