yq/pkg/yqlib/doc/operators/headers/env-variable-operators.md

32 lines
1.5 KiB
Markdown
Raw Normal View History

2021-11-03 04:00:58 +00:00
# Env Variable Operators
2021-01-10 00:09:59 +00:00
2022-01-26 22:20:53 +00:00
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:
2022-03-20 02:03:40 +00:00
2022-01-26 22:20:53 +00:00
- `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](https://github.com/a8m/envsubst).
## EnvSubst Options
You can optionally pass envsubst any of the following options:
2022-03-20 02:03:40 +00:00
- 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](https://github.com/a8m/envsubst#imposing-restrictions) in the `envsubst` documentation for more information, and below for examples.
2022-01-26 22:20:53 +00:00
## Tip
To replace environment variables across all values in a document, `envsubst` can be used with the recursive descent operator
as follows:
```bash
2022-01-27 06:21:10 +00:00
yq '(.. | select(tag == "!!str")) |= envsubst' file.yaml
2022-01-26 22:20:53 +00:00
```