Drill 02: Filters

select, has, path, and type checking.

Run This Drill

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

Drill Script

#!/bin/bash
# YQ DRILL 02: FILTERS
# Paste this entire script into your terminal
# Topics: select, has, keys, path, to_entries

cat << 'EOF' > /tmp/yq-hosts.yaml
hosts:
  - name: kvm-01
    ip: 10.50.1.110
    role: hypervisor
    active: true
    vlans: [10, 50, 99]
  - name: kvm-02
    ip: 10.50.1.111
    role: hypervisor
    active: true
    vlans: [10, 50]
  - name: vault-01
    ip: 10.50.1.60
    role: secrets
    active: true
    vlans: [50]
  - name: vault-02
    ip: 10.50.1.61
    role: secrets
    active: false
    vlans: [50]
  - name: ise-01
    ip: 10.50.1.20
    role: nac
    active: true
    vlans: [10, 50, 99]
EOF

echo "=================================================================="
echo "             YQ DRILL 02: FILTERS                                 "
echo "=================================================================="
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 2.1: SELECT BY FIELD VALUE"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.hosts[] | select(.role == \"hypervisor\") | .name' /tmp/yq-hosts.yaml"
yq '.hosts[] | select(.role == "hypervisor") | .name' /tmp/yq-hosts.yaml
echo ""
echo "Command: yq '.hosts[] | select(.active == false) | .name' /tmp/yq-hosts.yaml"
yq '.hosts[] | select(.active == false) | .name' /tmp/yq-hosts.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 2.2: SELECT WITH TEST (REGEX)"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.hosts[] | select(.name | test(\"vault\")) | .ip' /tmp/yq-hosts.yaml"
yq '.hosts[] | select(.name | test("vault")) | .ip' /tmp/yq-hosts.yaml
echo ""
echo "Command: yq '.hosts[] | select(.ip | test(\"10\\.50\\.1\\.1\")) | .name' /tmp/yq-hosts.yaml"
yq '.hosts[] | select(.ip | test("10\\.50\\.1\\.1")) | .name' /tmp/yq-hosts.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 2.3: HAS — CHECK KEY EXISTS"
echo "------------------------------------------------------------------"
echo ""
cat << 'EOF' > /tmp/yq-services.yaml
services:
  - name: vault
    port: 8200
    tls: true
  - name: consul
    port: 8500
  - name: prometheus
    port: 9090
    tls: true
  - name: grafana
    port: 3000
EOF
echo "Command: yq '.services[] | select(has(\"tls\")) | .name' /tmp/yq-services.yaml"
yq '.services[] | select(has("tls")) | .name' /tmp/yq-services.yaml
echo ""
echo "Command: yq '.services[] | select(has(\"tls\") | not) | .name' /tmp/yq-services.yaml"
yq '.services[] | select(has("tls") | not) | .name' /tmp/yq-services.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 2.4: KEYS AND TO_ENTRIES"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '.hosts[0] | keys' /tmp/yq-hosts.yaml"
yq '.hosts[0] | keys' /tmp/yq-hosts.yaml
echo ""
echo "Command: yq -o=json '.hosts[0] | to_entries' /tmp/yq-hosts.yaml"
yq -o=json '.hosts[0] | to_entries' /tmp/yq-hosts.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 2.5: COLLECT RESULTS"
echo "Wrap with [] to get a sequence instead of individual lines"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '[.hosts[] | select(.active) | .name]' /tmp/yq-hosts.yaml"
yq '[.hosts[] | select(.active) | .name]' /tmp/yq-hosts.yaml
echo ""
echo "Command: yq '[.hosts[] | .ip]' /tmp/yq-hosts.yaml"
yq '[.hosts[] | .ip]' /tmp/yq-hosts.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "DRILL 2.6: UNIQUE AND SORT"
echo "------------------------------------------------------------------"
echo ""
echo "Command: yq '[.hosts[].role] | unique' /tmp/yq-hosts.yaml"
yq '[.hosts[].role] | unique' /tmp/yq-hosts.yaml
echo ""
echo "Command: yq '[.hosts[] | select(.active) | .name] | sort' /tmp/yq-hosts.yaml"
yq '[.hosts[] | select(.active) | .name] | sort' /tmp/yq-hosts.yaml
echo ""

# ---------------------------------------------------------------------------
echo "------------------------------------------------------------------"
echo "YOUR TURN - TRY THESE:"
echo "------------------------------------------------------------------"
echo ""
echo "1. Get IPs of active hosts:"
echo "   yq '.hosts[] | select(.active) | .ip' /tmp/yq-hosts.yaml"
echo ""
echo "2. Find hosts with VLAN 99:"
echo "   yq '.hosts[] | select(.vlans[] == 99) | .name' /tmp/yq-hosts.yaml"
echo ""
echo "3. Count active vs inactive:"
echo "   yq '[.hosts[] | select(.active)] | length' /tmp/yq-hosts.yaml"
echo ""
echo "------------------------------------------------------------------"
echo "KEY TAKEAWAYS:"
echo "1. select(cond) filters — same as jq"
echo "2. test(\"regex\") for pattern matching"
echo "3. has(\"key\") checks key existence"
echo "4. Wrap with [] to collect into sequence"
echo "5. unique, sort work on sequences"
echo "------------------------------------------------------------------"