yq is a powerful command-line YAML processor that allows you to query, update, and manipulate YAML files with a syntax similar to jq for JSON. This cheatsheet provides essential commands for efficient YAML data handling.
Render the entire YAML file to standard output:
yq -r '.'
Access the value associated with the microservices key:
yq -r '.microservices'
Append new key-value pairs to the YAML file in place:
yq -i '.external_secrets = {"enabled": false}' test.yaml
Delete a specific key and its contents from the YAML file:
yq -i 'del(.microservice.secrets)' test.yaml
Inject data from another YAML file into a specific section:
yq -i ".microservice.env = load(\"staging.yaml\") | .microservice.env" test.yaml.yaml
Add a new nested data structure to the YAML file:
yq -i '.microservice.aws = {"accountId": "000000000000","region": "eu-west-1","eks": {"clusterId": "xxxxxxxxxxxxxxx"}}' test.yaml
Sort keys alphabetically under a specific section (e.g., microservice):
yq -i '.microservice |= (to_entries | sort_by(.key) | from_entries)' test.yaml