yq¶
Beware: There are two yq tools, one in golang, one in python.
See below.
yq (go)¶
- mikefarah/yq
- GitHub
- Docs
- Debian RFP
Installation¶
Get latest binary from GH:
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 \
-O /usr/local/bin/yq && chmod +x /usr/local/bin/yq
Brew:
or with snap (Be aware: https://github.com/mikefarah/yq/#snap-notes):
Arch:
Usage¶
Parse / read¶
Read single value:
yq '.internal.example.org' borgbackup_passphrase
yq '.internal.example.org' borgbackup_passphrase | ansible-vault decrypt
Read multiple values:
Match key in list:
gcloud beta billing accounts get-iam-policy 0E5... | \
yq eval '.bindings[] | select(.role == "roles/billing.admin")'
Iterate over map and show language and files extensions converted to csv:
curl --no-progress-meter \
https://raw.githubusercontent.com/github-linguist/linguist/refs/heads/main/lib/linguist/languages.yml\
| yq 'to_entries | .[] | .key + ": " + (.value.extensions | @csv)'
Flat keys¶
Output flat keys like foo.bar.baz using properties:
Include keys with empty values:
Strip comments:
Modify¶
Update inplace (write new pw to file):
yq write -i internal.example.org borgbackup_passphrase NEWPW
echo NEWPW | yq write -i internal.example.org borgbackup_passphrase -
Merge multiple files:
Sort keys:
Create from scratch¶
Creating yaml from scratch with multiple objects:
Merging multiple files¶
Merging YAML files with yq >= v4, concatenating arrays
Merge files, override arrays:
Merge files, merge arrays:
Issues¶
yq-python¶
Converts yaml to json and uses jq for parsing.
- Can process
yaml,tomlandxml. - Con only parse, not modify yaml
Install¶
kislyuk/yq Usage¶
Get single value:
Parse whole yaml file and output YAML: