Drill 04: Multi-Document
Multi-doc YAML, document selection, anchors, and aliases.
Run This Drill
bash ~/atelier/_bibliotheca/domus-captures/docs/modules/ROOT/examples/yq-drills/04-multi-doc.sh
Drill Script
#!/bin/bash
# YQ DRILL 04: MULTI-DOCUMENT YAML
# Paste this entire script into your terminal
# Topics: Document index, anchors, aliases, split
cat << 'EOF' > /tmp/yq-multi.yaml
apiVersion: v1
kind: Namespace
metadata:
name: wazuh
---
apiVersion: v1
kind: Service
metadata:
name: wazuh-indexer
namespace: wazuh
spec:
ports:
- port: 9200
targetPort: 9200
selector:
app: wazuh-indexer
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: wazuh-indexer
namespace: wazuh
spec:
replicas: 1
selector:
matchLabels:
app: wazuh-indexer
EOF
echo "=================================================================="
echo " YQ DRILL 04: MULTI-DOCUMENT "
echo "=================================================================="
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 4.1: SELECT BY DOCUMENT INDEX"
echo "di = document index (0-based)"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq 'select(di == 0)' /tmp/yq-multi.yaml"
yq 'select(di == 0)' /tmp/yq-multi.yaml
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 4.2: SELECT BY FIELD"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq 'select(.kind == \"Service\")' /tmp/yq-multi.yaml"
yq 'select(.kind == "Service")' /tmp/yq-multi.yaml
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 4.3: GET FIELD FROM ALL DOCUMENTS"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.kind' /tmp/yq-multi.yaml"
yq '.kind' /tmp/yq-multi.yaml
echo ""
echo "Command: yq '.metadata.name' /tmp/yq-multi.yaml"
yq '.metadata.name' /tmp/yq-multi.yaml
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 4.4: EDIT ONE DOCUMENT ONLY"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq 'select(di == 2).spec.replicas = 3' /tmp/yq-multi.yaml"
echo "(Only StatefulSet changed)"
yq 'select(di == 2).spec.replicas = 3' /tmp/yq-multi.yaml
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 4.5: ADD LABEL TO ALL DOCUMENTS"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.metadata.labels.managed-by = \"yq-drill\"' /tmp/yq-multi.yaml"
yq '.metadata.labels.managed-by = "yq-drill"' /tmp/yq-multi.yaml
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 4.6: ANCHORS AND ALIASES"
echo "------------------------------------------------------------------"
echo ""
cat << 'EOF' > /tmp/yq-anchor.yaml
defaults: &defaults
timeout: 30
retries: 3
tls: true
services:
vault:
<<: *defaults
port: 8200
consul:
<<: *defaults
port: 8500
timeout: 60
EOF
echo "Command: yq '.services.consul.timeout' /tmp/yq-anchor.yaml"
echo "(Override wins: 60, not 30)"
yq '.services.consul.timeout' /tmp/yq-anchor.yaml
echo ""
echo "Command: yq '.services.consul.retries' /tmp/yq-anchor.yaml"
echo "(Inherited from anchor: 3)"
yq '.services.consul.retries' /tmp/yq-anchor.yaml
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 4.7: EXPLODE ANCHORS"
echo "Resolve all aliases to actual values"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq 'explode(.)' /tmp/yq-anchor.yaml"
yq 'explode(.)' /tmp/yq-anchor.yaml
echo ""
# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "YOUR TURN - TRY THESE:"
echo "------------------------------------------------------------------"
echo ""
echo "1. Get all Service ports:"
echo " yq 'select(.kind == \"Service\") | .spec.ports[].port' /tmp/yq-multi.yaml"
echo ""
echo "2. Count documents:"
echo " yq -N 'di' /tmp/yq-multi.yaml | tail -1"
echo ""
echo "3. Check what vault inherits:"
echo " yq '.services.vault' /tmp/yq-anchor.yaml"
echo ""
echo "------------------------------------------------------------------"
echo "KEY TAKEAWAYS:"
echo "1. di = document index for multi-doc YAML"
echo "2. select(di == N) targets one document"
echo "3. Without select, operations apply to ALL documents"
echo "4. explode(.) resolves anchors/aliases"
echo "5. Override values in alias merge win"
echo "------------------------------------------------------------------"