8

Using envsubst to Merge Environment Variables into Config Files

 3 years ago
source link: https://nickjanetakis.com/blog/using-envsubst-to-merge-environment-variables-into-config-files
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Updated on July 13th, 2021 in #linux

Using envsubst to Merge Environment Variables into Config Files

blog/cards/using-envsubst-to-merge-environment-variables-into-config-files.jpg

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.

Quick Jump: Demo Video

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

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

What was the last thing you used envsubst for? Let me know below.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK