Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

yq

Read and change YAML files from the command line — Kubernetes manifests, Helm values, CI files — like jq does for JSON.

Niveau
DevOps-tool
Groep
Tekst en zoeken
Root nodig
Nee
Pakket
yq

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

yq (the Go version by Mike Farah, v4) uses the same filter language as jq, but for YAML (and JSON, XML, TOML). It keeps comments and the order of keys, so it is safe for files in Git.

Typical use: set an image tag in a manifest in a pipeline, read a value from values.yaml, merge two YAML files, or turn YAML into JSON.

Notitie

there is also an older Python yq; the commands here are for the Go yq v4 (yq --version shows "mikefarah").

Zo schrijf je het

yq [eval|eval-all] [OPTIONS] EXPRESSION [FILE ...]

Opties 16

Meest gebruikt

Main options

-i, --inplace
Change the file in place.
-o, --output-format FMT
Output as yaml, json, props, csv, tsv, xml.
-p, --input-format FMT
Input format (yaml, json, xml, toml, props).
-r, --unwrapScalar
Print plain values without quotes (default on).
-e, --exit-status
Exit 1 if there is no match.
-n, --null-input
Start from nothing (make new YAML).
-I N
Indent with N spaces.
ea, eval-all
Read all files together (for merges).
-s EXPR
Split the output into files named by EXPR.

Expressions

.a.b / .a[0]
A value / the first item.
.x = "v"
Set a value.
del(.x)
Remove a key.
select(COND)
Keep matching documents or items.
strenv(VAR) / env(VAR)
Use an environment variable (string / typed).
*+ / *
Deep merge of two documents.
document_index / di
Pick documents in a multi-document file.

Voorbeelden 12

$ als gewone gebruiker# als root (of met sudo)

  1. 1Read one value

    yq '.image.tag' values.yaml
    v1.3.2
  2. 2Set the image tag in place (in a pipeline)

    TAG=v1.4.0 yq -i '.image.tag = strenv(TAG)' charts/shop/values.yaml
  3. 3Change the image of a Deployment

    yq -i '(.spec.template.spec.containers[] | select(.name == "app")).image = "ghcr.io/acme/shop:1.4.0"' deploy.yaml
  4. 4YAML to JSON and back

    yq -o=json deploy.yaml > deploy.json
    yq -p=json -o=yaml deploy.json
  5. 5Merge a base and an override file

    yq ea '. as $item ireduce ({}; . * $item)' values.yaml values-prod.yaml > merged.yaml
  6. 6Only the Services in a multi-document file

    yq 'select(.kind == "Service")' all.yaml
  7. 7Count the documents

    yq ea '[.] | length' all.yaml
  8. 8Remove a key

    yq -i 'del(.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration")' svc.yaml
  9. 9Add an item to a list

    yq -i '.env += [{"name": "LOG_LEVEL", "value": "info"}]' container.yaml
  10. 10Read the jobs of a GitHub Actions workflow

    yq '.jobs | keys' .github/workflows/ci.yml
  11. 11Split a big file into one file per resource

    yq -s '.kind + "-" + .metadata.name' all.yaml
  12. 12Check that a value exists (CI gate)

    yq -e '.resources.limits.memory' values.yaml >/dev/null || echo "memory limit missing"

Let op

  • -i changes the file without a backup; commit first or work on a copy.
  • Use strenv(VAR) for versions like 1.10 — env(VAR) would turn them into the number 1.1.
  • Two different tools are called yq; check yq --version.

Tips om te onthouden

  • Kustomize or Helm is better for big changes; yq is great for one value in CI.

Exitcodes

0OK.
1Error, or no match with -e.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man yq