- DevOpsCube Newsletter
- Posts
- YQ: Handy CLI to Parse YAML
YQ: Handy CLI to Parse YAML
If you are DevOps engineer, YAML parsing is omething uyou will come across in difference situation while wiring automation scripts or in code.
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
It 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.
You can 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.
Practical Examples
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
Lets say you want to extract a specific value from Helm values.yaml
yq eval '.app.image.tag' values.yaml
YAML Formating
Another feature i really like about yq is --prettyPrint
. It ensures the YAML configuration files are correctly formatted before deploying.
Using exit codes, you can use this as a YAML validator in CI/CD pipelines.
$ yq eval --prettyPrint secret.yaml
Error: bad file 'test.yaml': yaml: line 11: mapping values are not allowed in this context
$ echo $?
1
Reply