How to use yq to read a simple value from a top-level key in a config.yaml file
The aim of this page📝 is to describe a way to read values from a structured config.yaml
file without the need to use pattern matching, and instead to treat it as structured data. I usually do this with Python and oyaml
module — this is also possible for different usecases. yq is a variant of the famous jq
module for json.
1 min readDec 27, 2022
1. to install yq, use wget
wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
2. use -e flag with dotted key notation and filepath to fetch the value from a yaml file
- for more, see yq gitbook < official docs
- use
yq
command to call the module - use
-e
flag to extract the value from a ke - use dotted notation to specify a key that you want the value of with
.<key>
syntax (of course, chainable) - follow the key specification with the filename
yq -e .<key> <filename>
>>> <value>
- for example, I have a
config.yml
containing the following key value pair
<!-- config.yml -->
...
GITHUB_TOKEN: foobar_5114o3oR5114o3oR5114o3oR5114o3oR5114o3oR
- to get my token (I need to authenticate with this with Vault), I then call
yq -e '.GITHUB_TOKEN' config.yml
foobar_5114o3oR5114o3oR5114o3oR5114o3oR5114o3oR