ENV Variable Expansion and YAML References
Compare environment variable expansion ($VAR, ${VAR}) in .env files with YAML anchors and aliases. Learn how referencing patterns differ between the two formats.
Detailed Explanation
Both ENV and YAML support forms of value referencing, but they work very differently. ENV uses shell-style variable expansion, while YAML uses anchors and aliases. Understanding both is key to proper conversion.
ENV file with variable expansion:
# Base configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DB_PASSWORD=secret123
DB_NAME=myapp
# Composed URL using variable expansion
DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
# Derived values
APP_TITLE=MyApp
FULL_TITLE=${APP_TITLE} - Production
LOG_FILE=/var/log/${APP_TITLE}/app.log
The expanded values would be:
DATABASE_URL=postgres://admin:secret123@localhost:5432/myapp
FULL_TITLE=MyApp - Production
LOG_FILE=/var/log/MyApp/app.log
YAML equivalent using anchors (different paradigm):
db:
host: &db_host localhost
port: &db_port 5432
user: &db_user admin
password: &db_password secret123
name: &db_name myapp
# YAML can't compose strings from anchors like ENV can.
# This is the closest equivalent:
database_url: "postgres://admin:secret123@localhost:5432/myapp"
app:
title: MyApp
full_title: "MyApp - Production"
log_file: /var/log/MyApp/app.log
Key differences:
ENV expansion is string interpolation.
${DB_HOST}is replaced with the literal value ofDB_HOSTinside another string. This allows building composite values like URLs.YAML anchors are value references.
*anchorinserts the entire anchored value, but cannot be used within a string. YAML has no native string interpolation.Expansion timing. ENV variables are expanded by the shell or the dotenv library at parse time. YAML anchors are resolved by the YAML parser.
Converting ENV expansion to YAML: Since YAML lacks string interpolation, you have two options:
- Pre-expand all values -- Resolve all
${...}references and write the final values to YAML. - Use application-level templating -- Tools like Helm (Go templates), Ansible (Jinja2), or envsubst can provide variable expansion on top of YAML.
Converting YAML anchors to ENV: YAML anchors are fully expanded during YAML-to-ENV conversion since ENV has no concept of references. The merged/referenced values are written directly.
Variable expansion syntax variants:
$VAR # Simple expansion (some parsers)
${VAR} # Braced expansion (most common)
${VAR:-default} # Default value if VAR is unset
${VAR:?error} # Error if VAR is unset
Not all dotenv libraries support all expansion forms. Test with your specific runtime.
Use Case
Migrating from a .env file that heavily uses variable expansion to construct database URLs and API endpoints to a YAML configuration file for a Helm chart, requiring all values to be pre-expanded.