👋 Hi! I’m Bibin Wilson. In each edition, I share practical tips, guides, and the latest trends in DevOps and MLOps to make your day-to-day DevOps tasks more efficient. If someone forwarded this email to you, you can subscribe here to never miss out!

If you are a DevOps Engineer, YAML parsing is something you will come across in different situations while writing 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

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

Avatar

or to participate

Keep Reading