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

Hoofdstuk 3Naam in de cursus: Shell automation

Automate the boring work in the shell

Much DevOps work is glue: call an API, read one value, change a YAML file, run something on 20 servers. Small shell tools do this faster than any big program. These 8 are in almost every pipeline script.

  • 8 Lessen
  • 17 Commando's

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

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

Vaardigheden in dit hoofdstuk

  • Shell-automatisering — 8

3.1

bash — the glue of every pipeline

bash scripts join the other tools: if, loops, functions, exit codes.

  • Used for: CI steps, entrypoints of containers, small ops scripts.
cat deploy.sh
#!/usr/bin/env bash
set -euo pipefail
kubectl apply -k "deploy/overlays/${ENV:?ENV is not set}"
kubectl rollout status deploy/shop --timeout=120s
bash -n deploy.sh && shellcheck deploy.sh

Good practice:

  • Start every script with set -euo pipefail.
  • Quote variables ("$file"); check scripts with shellcheck.

3.2

curl — talk to every API

curl sends HTTP requests: health checks, REST APIs, webhooks, downloads.

  • Used for: smoke tests after a deploy, API calls in scripts, debugging.
curl -fsS https://staging.example.com/health
curl -s -o /dev/null -w '%{http_code} %{time_total}\n' https://staging.example.com/
curl -fsS -H "Authorization: Bearer $TOKEN" https://api.example.com/v1/items
curl -fsSL https://get.example.com/install.sh -o install.sh

Good practice:

  • -f makes curl fail on HTTP errors — needed in scripts.
  • Read install scripts before you run them; never curl | sudo bash blindly.

Commando's in deze les

3.3

jq — read and change JSON

jq takes one value out of JSON — from APIs, kubectl -o json or cloud CLIs.

kubectl get pods -A -o json | jq -r '.items[].spec.containers[].image' | sort -u
terraform output -json | jq -r '.lb_ip.value'
jq -n --arg v "$TAG" '{version: $v}'

Good practice:

  • -r for raw strings in shell variables; -e for checks.

3.4

yq — read and change YAML

yq is jq for YAML: set an image tag, read a Helm value, merge files.

yq '.image.tag' values.yaml
TAG=v1.4.0 yq -i '.image.tag = strenv(TAG)' values.yaml
yq -o=json deploy.yaml

Good practice:

  • Use strenv() for versions so 1.10 stays a string.

Commando's in deze les

3.5

xargs — run a command for many items

xargs turns lines into arguments, in batches or in parallel.

cat images.txt | xargs -P 4 -n 1 docker pull
find . -name '*.log' -mtime +14 -print0 | xargs -0 -r rm -f
cat hosts.txt | xargs -I {} ssh {} uptime

Good practice:

3.6

envsubst — tiny templates

envsubst fills $VARIABLES in a file with environment values.

export TAG=v1.4.0
envsubst < deploy.tpl.yaml | kubectl apply -f -
envsubst '$SERVER_NAME' < nginx.conf.tpl > shop.conf

Good practice:

  • Name the variables to replace, so other $ words stay.

3.7

watch — a live view

watch runs a command every few seconds and shows what changed.

watch -n 1 kubectl get pods
watch -d 'df -h /var'
watch -g 'kubectl get job migrate -o jsonpath={.status.succeeded}'

Commando's in deze les

3.8

tmux — sessions that survive

tmux keeps a terminal session alive on the server when SSH drops, and splits the screen.

tmux new -A -s main
tmux ls
tmux attach -t main

Good practice:

  • Start every long remote job (upgrade, migration, big copy) inside tmux.

Onthoud

A 10-line script with set -euo pipefail, curl -f, jq -e and clear exit codes is often better than a big tool.

Alle commando's in dit hoofdstuk