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:
brew install yq
or with snap (Be aware: https://github.com/mikefarah/yq/#snap-notes):
sudo snap install yq
Arch:
sudo pacman -S go-yq
Usage
Parse / read
Read single value:
yq '.internal.example.org' borgbackup_passphrase
yq '.internal.example.org' borgbackup_passphrase | ansible-vault decrypt
Read multiple values:
yq .
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)'
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 -
yq eval '.a.b += ["cow"]' sample.yml
sops -d basic-auth.yaml | yq eval 'del(.metadata.creationTimestamp)' -
Merge multiple files:
yq ea '. as $item ireduce ({}; . * $item )' file1.yml file2.yml
Sort keys:
yq ea 'sort_keys(..)' helm-values-template.yml
Create from scratch
Creating yaml from scratch with multiple objects:
$ yq --null-input '(.secret.a = "a") | (.secret.b = "b")'
secret:
a: a
b: b
Issues
yq-python
Converts yaml to json and uses jq
for parsing.
- Can process
yaml
,toml
andxml
. - Con only parse, not modify yaml
Install
apt install yq
kislyuk/yq Usage
Get single value:
yq .foo.bar input.yml
Parse whole yaml file and output YAML:
yq -y .services.kubelet.extra_args < /var/lib/OpenAppStack/rke/cluster.yml