Templates and secrets
Quick answer
A configuration template separates reusable structure from environment-specific values. Secrets need a separate protected delivery mechanism.
A rendered configuration can be more sensitive than its template. Test with placeholder values, restrict access to the rendered file, and never include real secret values in diffs or test logs. A missing required variable should stop rendering rather than silently produce an empty credential.
Prepare the practice environment
Run python3 lab-fixtures/automation/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Only private files are used; optional examples require an authorized snapshot VM. Do not modify system services. Review the lab guide.
Prerequisites
Review: Imperative and declarative configuration.
How it works
Use a versioned template with explicit placeholders and validate the supplied values before rendering. GNU envsubst substitutes environment variables but does not escape JSON, YAML or shell syntax; use format-aware serialization for arbitrary values. Missing variables may become empty strings. The examples use only known non-secret literals and an explicit substitution list. Never commit real tokens, put them in command arguments, or print them in CI logs. Environment variables are not a secret vault: process inspection, diagnostics and inherited environments may expose them. Prefer an approved secret manager or restricted file, limit access and rotate exposed credentials. A template can refer to a secret file without storing its contents.
Try the examples
BM_PORT=8080 BM_ENV=dev envsubst '$BM_PORT $BM_ENV' < app.templateOnly the listed variables are replaced; the unlisted placeholder remains literal. This is a partial render, not a deployable config.
Expected stdout:
port=8080environment=devsecret_file=${BM_SECRET_FILE}The helper validates required fields before rendering; no output configuration is created.
Expected stdout:
missing value rejectedThe supplied dummy file contains no real credentials. Mode 0600 restricts ordinary discretionary access to its owner; it does not protect against privileged access.
Expected stdout:
600Common mistakes
Do not use eval to render templates. Do not print secret content or assume a hidden CI variable cannot leak.
Practice
Render the sample for a different non-secret port, explain the remaining placeholder and describe a protected secret delivery approach.
Related references
Study: env. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Templates and secrets — Knowledge Check
Apply configuration management concepts to synthetic fixtures and VM-only scenarios.
What is safe to commit?
Does envsubst escape JSON strings?
What if a required value is missing?
What does mode 0600 mean for an ordinary file?