Drill 01: Basics

YAML reading, output formats, and property access. The foundation.

Run This Drill

bash ~/atelier/_bibliotheca/domus-captures/docs/modules/ROOT/examples/yq-drills/01-basics.sh

Drill Script

#!/bin/bash
# YQ DRILL 01: BASICS
# Paste this entire script into your terminal
# Topics: Reading YAML, output formats, property access

cat << 'EOF' > /tmp/yq-01.yaml
name: domus-captures
version: "~"
title: Work Chronicles
start_page: ROOT:index.adoc

asciidoc:
  attributes:
    icons: font
    domain: inside.domusdigitalis.dev
    ad-dc-ip: 10.50.1.50
    ise-pan-ip: 10.50.1.20
    pfsense-ip: 10.50.1.1
    vlan-data: 10
    vlan-mgmt: 50

nav:
  - modules/ROOT/nav.adoc

tags:
  - documentation
  - antora
  - infrastructure
EOF

echo "=================================================================="
echo "             YQ DRILL 01: BASICS                                  "
echo "=================================================================="
echo ""
echo "Test file: /tmp/yq-01.yaml"
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.1: IDENTITY — READ THE WHOLE FILE"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.' /tmp/yq-01.yaml"
yq '.' /tmp/yq-01.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.2: OUTPUT AS JSON"
echo "The bridge to jq — most important yq flag"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq -o=json '.' /tmp/yq-01.yaml"
yq -o=json '.' /tmp/yq-01.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.3: OUTPUT AS PROPS"
echo "Flat key=value — useful for shell sourcing"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq -o=props '.' /tmp/yq-01.yaml"
yq -o=props '.' /tmp/yq-01.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.4: PROPERTY ACCESS"
echo "Same as jq: .key for top-level, .key.subkey for nested"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.name' /tmp/yq-01.yaml"
yq '.name' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.title' /tmp/yq-01.yaml"
yq '.title' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.asciidoc.attributes.domain' /tmp/yq-01.yaml"
yq '.asciidoc.attributes.domain' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.asciidoc.attributes.ad-dc-ip' /tmp/yq-01.yaml"
yq '.asciidoc.attributes.ad-dc-ip' /tmp/yq-01.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.5: SEQUENCE ACCESS"
echo "YAML sequences = arrays. Same .[n] and .[] syntax"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.tags' /tmp/yq-01.yaml"
yq '.tags' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.tags[0]' /tmp/yq-01.yaml"
yq '.tags[0]' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.tags[]' /tmp/yq-01.yaml"
echo "(Each tag on its own line)"
yq '.tags[]' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.tags | length' /tmp/yq-01.yaml"
yq '.tags | length' /tmp/yq-01.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.6: KEYS AND LENGTH"
echo "Get structure information"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.asciidoc.attributes | keys' /tmp/yq-01.yaml"
yq '.asciidoc.attributes | keys' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.asciidoc.attributes | length' /tmp/yq-01.yaml"
yq '.asciidoc.attributes | length' /tmp/yq-01.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.7: TYPE CHECKING"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.name | type' /tmp/yq-01.yaml"
yq '.name | type' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.asciidoc | type' /tmp/yq-01.yaml"
yq '.asciidoc | type' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.tags | type' /tmp/yq-01.yaml"
yq '.tags | type' /tmp/yq-01.yaml
echo ""
echo "Command: yq '.asciidoc.attributes.vlan-data | type' /tmp/yq-01.yaml"
yq '.asciidoc.attributes.vlan-data | type' /tmp/yq-01.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 1.8: YQ TO JQ PIPELINE"
echo "Convert YAML to JSON, then filter with jq"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq -o=json '.asciidoc.attributes' /tmp/yq-01.yaml | jq 'to_entries[] | select(.value | tostring | test(\"10\\\\.50\"))'"
yq -o=json '.asciidoc.attributes' /tmp/yq-01.yaml | jq 'to_entries[] | select(.value | tostring | test("10\\.50"))'
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "YOUR TURN - TRY THESE:"
echo "------------------------------------------------------------------"
echo ""
echo "1. Get start_page: yq '.start_page' /tmp/yq-01.yaml"
echo ""
echo "2. Get all attribute values: yq '.asciidoc.attributes[]' /tmp/yq-01.yaml"
echo ""
echo "3. Output just attributes as JSON: yq -o=json '.asciidoc.attributes' /tmp/yq-01.yaml"
echo ""
echo "4. Count tags: yq '.tags | length' /tmp/yq-01.yaml"
echo ""
echo "------------------------------------------------------------------"
echo "KEY TAKEAWAYS:"
echo "1. yq '.' reads YAML like jq '.' reads JSON"
echo "2. -o=json is the bridge to jq — use it often"
echo "3. -o=props gives flat key=value output"
echo "4. .key, .key.subkey, .[n], .[] — same as jq"
echo "5. | keys, | length, | type — same as jq"
echo "------------------------------------------------------------------"