Skip to content

feat(operations.files): add deploy-wide Jinja customization - #2104

Open
noirbizarre wants to merge 1 commit into
pyinfra-dev:3.xfrom
noirbizarre:feat/jinja-config
Open

noirbizarre wants to merge 1 commit into
pyinfra-dev:3.xfrom
noirbizarre:feat/jinja-config

Conversation

@noirbizarre

Copy link
Copy Markdown
Contributor

Allow deploys to configure Jinja environment options, filters, globals, and tests once through config:

from pyinfra import config


def is_positive(value: int) -> bool:
    return value > 0


config.JINJA_ENV_KWARGS = {"trim_blocks": True}
config.JINJA_FILTERS = {"shout": str.upper}
config.JINJA_GLOBALS = {"application": "myapp"}
config.JINJA_TESTS = {"positive": is_positive}

Templates can then use {{ application | shout }} and {% if count is positive %}. Registrations are also available to included and imported templates, while Jinja’s built-ins remain available.

Override defaults for an individual template using matching arguments:

from pyinfra.operations import files

files.template(
    src="templates/app.conf.j2",
    dest="/etc/app.conf",
    jinja_env_kwargs={"trim_blocks": False},
    jinja_globals={"application": "another-app"},
    jinja_filters={"shout": str.capitalize},
    jinja_tests={"positive": lambda value: value >= 0},
    count=3,
)

Each mapping is merged by name, with per-call entries taking precedence. Variables passed directly to files.template take precedence over configured globals.

Preserve deploy-local configuration during lazy operation execution and isolate customized templates from the filename-only cache. Includes documentation and regression coverage.

@wowi42 wowi42 added new feature operations Issues with operations. API API mode specific issues. labels Nov 21, 2026
@noirbizarre noirbizarre changed the title feat(files.template): add deploy-wide Jinja customization feat(operations.files): add deploy-wide Jinja customization Nov 21, 2026
@noirbizarre noirbizarre mentioned this pull request Nov 26, 2026
5 of 6 tasks

@wowi42 wowi42 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature needs two fixes before merge. Shallow Config.copy() snapshots share the Jinja mappings, so in-place changes can affect earlier operations and other hosts. Please copy each top-level Jinja mapping for the operation snapshot and add an in-place, two-host regression test. The secret-exposure issue is inline.

create_remote_dir: bool = True,
jinja_env_kwargs: dict[str, Any] | None = None,
jinja_filters: dict[str, Callable[..., Any]] | None = None,
jinja_globals: dict[str, Any] | None = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because jinja_globals is a named parameter, operation metadata stringifies the whole mapping and logs it. A value such as a password appears in plain text, unlike template **data. Please exclude these values from displayed args or apply equivalent masking, with a regression test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API API mode specific issues. new feature operations Issues with operations.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants