YQ: Handy CLI to Parse YAML
yq is a lightweight command-line utility to parse YAML. But wait, there’s more!
Not just YAML, yq also works with JSON, XML, LUA, Shell output, and properties files.
Also written in Go
Project: github.com/mikefarah/yq
Handles almost all file types you’ll encounter in CI/CD pipelines. Perfect for those times when you need to tweak files on the fly.
Here is one use case.
Pair yq with kustomize to patch values directly into your YAML files.
For instance, read an image name from an environment variable and add it to the YAML.
Usage Example
Let’s take an example of pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
To get the apiVersion
yq eval '.apiVersion' pod.yaml
To change the image name
yq eval -i '.spec.containers[0].image = "nginx:1.18"' pod.yaml