YQ the tool

yq is a lightweight, portable command-line YAML, JSON, and XML processor. It is essential for DevOps and SRE engineers working with Kubernetes, Terraform, Ansible, and CI/CD pipelines across AWS, Azure, GCP, Linux, NixOS, and WSL environments.

Why Use yq in DevOps & SRE?

  • Automate YAML/JSON edits: Update Kubernetes manifests, Terraform variables, and CI/CD configs programmatically.

  • Bulk Operations: Apply changes across multiple files for GitOps, policy enforcement, or compliance.

  • CI/CD Integration: Use yq in GitHub Actions, Azure Pipelines, or GitLab CI/CD for validation, patching, and templating.

  • Cloud-Native: Works seamlessly with cloud IaC and configuration workflows.

Real-Life Examples

1. Update Image Tag in All Kubernetes Deployments

grep -rl 'image:' ./k8s | xargs -I{} yq -i '.spec.template.spec.containers[0].image = "nginx:1.25.0"' {}

2. Extract All Resource Limits for Audit

find ./manifests -name '*.yaml' | xargs -I{} yq '.spec.template.spec.containers[].resources.limits' {}

3. Patch a Value in a CI/CD Pipeline (GitHub Actions)

- name: Patch image tag in deployment
  run: yq -i '.spec.template.spec.containers[0].image = "myrepo/app:${{ github.sha }}"' k8s/deployment.yaml

4. Merge Multiple YAML Files for GitOps

5. Use Environment Variables for Dynamic Values

Best Practices (2025)

  • Always validate YAML after edits: kubectl apply --dry-run=client -f file.yaml

  • Use yq in CI/CD for repeatable, automated changes

  • Document yq commands in README or pipeline logs

  • Prefer explicit paths to avoid accidental overwrites

  • Use yq with version control for traceability

Common Pitfalls

  • Overwriting files without backup (-i is destructive)

  • Not validating YAML after bulk edits

  • Using ambiguous paths (be specific to avoid wrong fields)

  • Forgetting to quote strings with special characters

Install

wget

Use wget to download, gzipped pre-compiled binaries:

For instance, VERSION=v4.2.0 and BINARY=yq_linux_amd64

Compressed via tar.gz

Last updated