Dec 05 2013

How to validate your yaml files from command line

Category: Continuous Integration,Puppet @ 15:15

I like using Hiera with Puppet. In my  puppet pipeline I just added YAML syntax validation for the Hiera files in the compile step. Here’s how:

# ...
GIT_DIFF_CMD="git diff --name-only --diff-filter=ACMR $OLD_REVISION $REVISION"
declare -i RESULT=0
set +e # Don't exit on error. Collect the errors instead.
YAML_PATH_LIST=`$GIT_DIFF_CMD | grep -F 'hieradata/' | grep -F '.yaml'`
echo 'YAML files to check syntax:'; echo "$YAML_PATH_LIST"; echo "";
for YAML_PATH in $YAML_PATH_LIST; do
  ruby -e "require 'yaml'; YAML.load_file('${YAML_PATH}')"
  RESULT+=$?
done
# ...
exit $RESULT

The line in bold does the actual validation.

If you read my previous post you can see that we have managed to migrated to git. Hurray!

Tags: , ,

One Response to “How to validate your yaml files from command line”

  1. Marcus Philip says:

    Actually we need to do this:

    if [ $OLD_REVISION != $REVISION ]; then
    GIT_DIFF_CMD=”git diff –name-only –diff-filter=ACMR $OLD_REVISION $REVISION”
    else
    GIT_DIFF_CMD=”git diff-tree –no-commit-id –name-only –diff-filter=ACMR -r $REVISION”
    fi

    We get nothing with the first command if current_version == new_version