« Previous 1 2
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:// prod.example.com:5431"' 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 postgres://prod.example.com:5431
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.
Infos
- YAML: https://yaml.org
- JSON: https://www.json.org/json-en.html
- YAML parser yq: https://github.com/mikefarah/yq
- yq documentation: https://mikefarah.gitbook.io/yq/
- JSON parser jq: https://github.com/stedolan/jq/
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
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.