Skip to content

Instantly share code, notes, and snippets.

@epcim
Last active March 29, 2019 16:46
Show Gist options
  • Save epcim/f1c5b748fa7c942de50677aef04f29f8 to your computer and use it in GitHub Desktop.
Save epcim/f1c5b748fa7c942de50677aef04f29f8 to your computer and use it in GitHub Desktop.
#salt #reclass parser - works as jq for yaml files, used to traverse all paths in #Salt reclass and find/grep tree paterns
#!/bin/bash
# tl;dr
# yaml-grep; to extract partial branches based on the 'jq' regex pattern.
# install:
# wget https://gist.githubusercontent.com/epcim/f1c5b748fa7c942de50677aef04f29f8/raw/yg.sh -O yg; chmod +x yg
# usage:
# ./yg '.compute?.network'
#
# classes/system/nova/compute/cluster.yml
# {"engine":"neutron","host":"${_param:network_vip_address}","user":"neutron","password":"${_param:keystone_neutron_password}","port":9696,"tenant":"service"
PATTERN=$@
PATTERN=${PATTERN:-.parameters}
export GOPATH=${GOPATH:-$HOME/go}
export PATH=$PATH:$GOPATH/bin
# with pure python yq (much slower)
# sudo pip install git+git://github.com/kyle-long/yq.git@master
# for i in $(find . -name "*.yml"); do echo $i; cat $i | yq -ec "..|${PATTERN}| select(.!=null)" || printf '\e[A\e[K'; done
# with go yaml2json
if [ command -v yaml2json &>/dev/null ]; then
if [ command -v go ]; then
go get github.com/bronze1man/yaml2json
pushd $GOPATH/src/github.com/bronze1man/yaml2json
go install
popd
else
echo "First install github.com/bronze1man/yaml2json"
fi
fi
for i in $(find . -name "*.yml"); do echo $i; cat $i | yaml2json | jq -e "..|${PATTERN}| select(.!=null)" || printf '\e[A\e[K'; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment