Using envsubst to Merge Environment Variables into Config Files
This command is available on Linux and macOC by default and it lets you template out files with ENV vars to create dynamic config files.
Let’s say you have an nginx or Kubernetes config file which doesn’t support
templating out of the box and you want to dynamically create config files based
on 1 or more environment variables. This is what envsubst
lets you do.
This can help you avoid creating 2 separate config files that are nearly
identical. Instead you can define what you want to be different as environment
variables and send it to envsubst
to be processed. We’ll go over a few
examples below:
# Demo Video
Commands
Trying out envsubst
on the command line:
$ export PERSON=nick
$ echo 'hello ${PERSON}' | envsubst
hello nick
# We're using single quotes above instead of double quotes to avoid the variable
# being interpreted by your shell. Single quotes will ensure it's picked up
# and processed by envsubst.
Piping the output of envsubst
as input to another command:
$ source .env
$ envsubst < aws-cluster-tpl.yaml | eksctl create cluster --config-file - --dry-run
Outputting a new config file with the results of running it through envsubst
:
$ source .env
$ envsubst < aws-cluster-tpl.yml > aws-cluster-example.yaml
Timestamps
- 0:12 – envsubst lets you generate templated files with ENV variables
- 0:44 – Trying out envsubst on the command line with a simple echo
- 1:19 – Going over the use case of templating out an eksctl config file
- 2:29 – Taking a look at a templated eksctl config file with ENV vars
- 3:34 – Running the templated config through envsubst
- 4:53 – Exporting the processed config to eksctl as input
- 6:29 – Writing out a new processed config file based on using envsubst
- 7:00 – Being able to define default values using a fork of envsubst
Reference Links
- https://github.com/a8m/envsubst (optional replacement with default variable support)
What was the last thing you used envsubst
for? Let me know below.