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.

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

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Jenkins Configuration as Code
    The move from Groovy scripts to Jenkins Configuration as Code simplifies the initialization of Jenkins and Jenkins plugins.
  • Verifying your configuration
    Automated acceptance testing is a powerful tool for catching problems related to misconfiguration. We'll show you how to implement your own acceptance testing environment with a free tool called goss.
  • Explore automation-as-code with Ansible
    The Ansible automation tool makes it really easy to implement IT scenarios as code. We use structured YAML code to roll out Ansible in the form of AWX.
  • Validating Docker containers
    A new test tool by Google lets you peek inside Docker containers, so you can make sure they hold exactly what you expect.
  • Policy rulesets in cloud-native environments
    What a user is allowed to do in a program is usually defined by a role model, which often poses numerous challenges, especially in the cloud or for infrastructure as code. The free Open Policy Agent offers a flexible way to manage user rights.
comments powered by Disqus