Configuring complex environments

At the Controls

Validating Values

The validation of certain values with the length function can be quite useful:

yq eval ".spec.containers[].name | length" pod.yaml

A YAML template can also be easily modified by yq to create configurations for different environments. For example, if you want to insert the hostname of your production database into the URL variable of the first container in the pod.yaml template, you can use:

yq eval '.spec.containers[0].env[0].value = "postgres://"' pod.yaml > prod-pod.yaml

To make sure the change is visible not just on the screen, the modified YAML document has been written to a new file named prod-pod.yaml, which now contains the modification, as shown with the command:

yq eval ".spec.containers[0].env[0].value" prod-pod.yaml

With Kubernetes, this function proves to be extremely useful, because you can use it to change existing configurations immediately. For example, you can simply forward the output of the Kubernetes kubectl tool with

yq (kubectl ... | yq eval ...)

and use kubectl apply to send the result directly back to Kubernetes.

The Author

Thorsten Scherf is the global Product Lead for Identity Management and Platform Security in Red Hat's Product Experience group. He is a regular speaker at various international conferences and writes a lot about open source software.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs

Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.