HOW TO VALIDATE YOUR YAML FILES FROM COMMAND LINE

Diabol CONTINUOUS INTEGRATION Leave a Comment

2013-12-05 MARCUS PHILIP 

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}')" # This line does the actual validation.
  RESULT+=$?
done
# ...
exit $RESULT

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

Leave a Reply

Your email address will not be published. Required fields are marked *