Skip to content

YAML

Multiline

Parse yaml

General issues

All cli parsers were, as of today (2019-10) not able to handle custom data types like ansible-vault uses them. Here are the issues:

  • yq-python: Won't fix b/c of the nature of yq-python, which converts data to json and uses jq for parsing.
  • shyaml
  • yq-go is using go-yaml, and the bug is still open in go-yaml.
  • yaml-cli (Anyhow, yaml-cli stalled development, last commit 2016)

Example:

export YAML="/home/varac/projects/yaml/examples/ansible-vault-example.yml"
yq -y . < $YAML
yq -Y . < $YAML

dasel -f $YAML .

Tools

mikefarah/yq (go)

See yq.md

yq-python

https://github.com/kislyuk/yq

Converts yaml to json and uses jq for parsing.

  • Can process yaml and xml.
  • Con only parse, not modify yaml

Install:

pipx install yq

Usage:

yq .services.kubelet.extra_args < /var/lib/OpenAppStack/rke/cluster.yml

Issues:

dasel

https://github.com/TomWright/dasel

shyaml

https://github.com/0k/shyaml

Can only parse yaml.

Install

pipsi install shyaml

or

wget https://raw.githubusercontent.com/0k/shyaml/master/shyaml \
  -O /usr/local/bin/shyaml && chmod a+x /usr/local/bin/shyaml

query

show all values:

cd ~/leap/git/bitmask
cat hiera/local1.yaml| shyaml get-values

get particular value:

cat hiera/local1.yaml| shyaml get-value domain
cat hiera/local1.yaml| shyaml get-value sources.apt
cat hiera/local1.yaml| shyaml sources.platform.apt.basic

Deprecated

yaml-cli (Warning: Last commit 2016)

https://github.com/pandastrike/yaml-cli

Validate / lint yaml

yamllint

Install:

apt install yamllint

Usage:

yamllint hiera/local1.yaml

v8r

A command-line JSON, YAML and TOML validator that's on your wavelength v8r is a command-line validator that uses Schema Store to detect a suitable schema for your input files based on the filename.

Other linters

or

cat hiera/local1.yaml| shyaml get-values

cat .gitlab-ci.yml| ysh

Format/fix yaml

prettier l.yml
prettier --write l.yml

Sort yaml

https://github.com/ddebin/yaml-sort

npm install -g yaml-sort

Yaml formaters

Merging

https://stackoverflow.com/questions/66694238/merging-two-yaml-documents-while-concatenating-arrays

Convert json to yaml

Convert json to yaml

yq eval -j resume.json > resume.yaml