Use this when generating full configs from templates.
Configuration Templates
Configuration templates generate full device or service configs, such as a switch config or nginx.conf, from a base template plus per-device variables.
1. Configuration templates generate device/service configs.
2. One template per platform/role.
3. Validate with native tools before deploying.
4. Diff against running config.
5. Store rendered artifacts.
data → template → config → validate → deploy
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
python -c "from jinja2 import Environment; Environment().parse(open('<template.j2>').read())"Primary Commands
Configuration and operational commands for this Topic.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verify template output with fixtures, syntax validation and target diff Verify boundary
Render the Configuration Template from a versioned known fixture and compare the normalized output with a reviewed expected artifact while masking secrets. Validate syntax using the target parser, linter, schema or dry-run interface, then compare the intended target diff before any apply. Re-render the same inputs and expect byte-stable or deliberately normalized output so nondeterministic ordering is visible. A successful render shows the template engine completed, but it does not show the artifact is accepted by the target or that applying it yields the intended operational state.
Step 2 Command read-back and what it shows Verify boundary
Verification for Templates → Configuration Templates must distinguish state, not merely report that a command ran. With python -c "from jinja2 import Environment; Environment().parse(open('<template.j2>').read())", known-good means the versioned template parses with the intended filter set, renders deterministically from a known target context, includes only the expected platform-specific sections and passes downstream platform-aware validation; known-broken/suspicious means parse/filter errors, undefined or stale variables, nondeterministic output, wrong branch selection or downstream rejection of an apparently valid render. The Jinja parse/render validation shows template processing for the supplied context only, but it does not show source data freshness, target compatibility or safe deployment.
Step 3 Trace variable precedence before debugging the target device or service Narrow the fault
If a Configuration Template renders the wrong value, inspect the effective variable after precedence resolution rather than editing the template immediately. If a required variable is absent, fail closed with its source/key; if the value is correct but output syntax is wrong, inspect quoting, escaping, conditionals and whitespace for the target format. A render that looks correct textually can still violate platform grammar, so run a parser/linter or target dry-run when available. If the artifact validates but deployment fails, leave template troubleshooting and follow the deployment/target boundary with that exact rendered revision.
Step 4 Common mistakes Narrow the fault
- Templates per device instead of per role.
Step 5 Observation-driven branch and next-check logic Narrow the fault
When Templates → Configuration Templates fails, reproduce the smallest original case and capture python -c "from jinja2 import Environment; Environment().parse(open('<template.j2>').read())" before mutation. Use the observation to choose the next check: if template parse fails, fix Jinja syntax/filter availability; if parse succeeds but rendering has undefined/wrong values, inspect variable contract and precedence; if rendered output is plausible but deployment validation fails, move to platform parser/semantic checks.
- A base template plus per-device variables produces full configs.
- Render one config per device.
- Validate before deploying.
ansible-playbook render.yml --check --diff— preview rendered configs- Templates per platform/role — structure
- Validation command (e.g. nginx -t, sshd -t) — syntax check
ReferenceWhy it matters1 section · depth on demand
Configuration templates transform typed inputs into target syntax
Automation Configuration Templates should be treated as a compiler boundary: input variables and defaults are resolved with precedence, conditions select fragments, escaping/quoting produces text or structured data, and the rendered artifact is consumed by a specific target/platform. Missing values, implicit type conversion and whitespace can be harmless on one syntax and destructive on another. Template revision, input revision and target platform therefore belong to the rendered artifact identity. Secrets should remain out of logs/diffs even when they participate in rendering.
ReferenceHow it works1 section · depth on demand
How it works
Templates → Configuration Templates should be reasoned about as a state pipeline, not as one command outcome. The responsible state is template source, variable contract, filters/macros, platform-specific branches and the rendered artifact produced for one target/context. Configuration templates combine static syntax with data-dependent expressions; correctness depends on both template parsing and a complete variable context that selects the intended platform branches and renders deterministic output; meanwhile custom filters, inventory/source data, secrets, whitespace policy and downstream platform parsers remain separate things to check. This distinction is especially important because a template can parse successfully while rendering wrong values or the wrong platform branch, and a rendered text artifact can still be invalid for the target device/service.
ReferenceImportant terms & boundaries1 section · depth on demand
Method
Configuration templates produce device or server configuration from data. A disciplined method keeps them correct as they evolve.
1. Define the data model first (YAML schema or NetBox fields).
2. Write templates per role/platform with includes for shared blocks.
3. Render with strict undefined-variable handling.
4. Validate the output: platform syntax checks, linters, Batfish.
5. Store golden rendered output for test data and diff on every change.
6. Deploy through the normal change pipeline.
| Common bug | Prevention |
|---|---|
| Missing variable renders empty | StrictUndefined |
| Whitespace breaks config | trim_blocks, lstrip_blocks |
| Secrets rendered into Git | Render at deploy time from vault |
| Platform syntax differences | Separate templates per OS |
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Templates > Configuration Templates. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://jinja.palletsprojects.com/en/stable/templates/ · confidence 94%