Skip to content

yq (go)

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:

yq '.internal.example.org' borgbackup_passphrase
yq '.internal.example.org' borgbackup_passphrase | ansible-vault decrypt

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 -

Add to list::

yq eval '.a.b += ["cow"]' sample.yml

Delete key:

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