Env Variable Operators

These operators are used to handle environment variables usage in expressions and documents. While environment variables can, of course, be passed in via your CLI with string interpolation, this often comes with complex quote escaping and can be tricky to write and read.

There are three operators:

  • env which takes a single environment variable name and parse the variable as a yaml node (be it a map, array, string, number of boolean)

  • strenv which also takes a single environment variable name, and always parses the variable as a string.

  • envsubst which you pipe strings into and it interpolates environment variables in strings using envsubst.

EnvSubst Options

You can optionally pass envsubst any of the following options:

  • nu: NoUnset, this will fail if there are any referenced variables that are not set

  • ne: NoEmpty, this will fail if there are any referenced variables that are empty

  • ff: FailFast, this will abort on the first failure (rather than collect all the errors)

E.g: envsubst(ne, ff) will fail on the first empty variable.

See Imposing Restrictions in the envsubst documentation for more information, and below for examples.

Tip

To replace environment variables across all values in a document, envsubst can be used with the recursive descent operator as follows:

yq '(.. | select(tag == "!!str")) |= envsubst' file.yaml

Read string environment variable

Running

will output

Read boolean environment variable

Running

will output

Read numeric environment variable

Running

will output

Read yaml environment variable

Running

will output

Read boolean environment variable as a string

Running

will output

Read numeric environment variable as a string

Running

will output

Dynamically update a path from an environment variable

The env variable can be any valid yq expression.

Given a sample.yml file of:

then

will output

Dynamic key lookup with environment variable

Given a sample.yml file of:

then

will output

Replace strings with envsubst

Running

will output

Replace strings with envsubst, missing variables

Running

will output

Replace strings with envsubst(nu), missing variables

(nu) not unset, will fail if there are unset (missing) variables

Running

will output

Replace strings with envsubst(ne), missing variables

(ne) not empty, only validates set variables

Running

will output

Replace strings with envsubst(ne), empty variable

(ne) not empty, will fail if a references variable is empty

Running

will output

Replace strings with envsubst, missing variables with defaults

Running

will output

Replace strings with envsubst(nu), missing variables with defaults

Having a default specified skips over the missing variable.

Running

will output

Replace strings with envsubst(ne), missing variables with defaults

Fails, because the variable is explicitly set to blank.

Running

will output

Replace string environment variable in document

Given a sample.yml file of:

then

will output

(Default) Return all envsubst errors

By default, all errors are returned at once.

Running

will output

Fail fast, return the first envsubst error (and abort)

Running

will output

Last updated

Was this helpful?