siddou.tk

Puppet tips

Make sure you respect puppet style_guide

Install puppet-lint:

sudo gem install puppet-lint

Usage:

puppet-lint modules/chrome/manifests/init.pp

It Will show bad syntax.

ERROR: trailing whitespace found on line 3
ERROR: two-space soft tabs not used on line 4
ERROR: two-space soft tabs not used on line 13
WARNING: indentation of => is not properly aligned on line 14
WARNING: indentation of => is not properly aligned on line 15
WARNING: indentation of => is not properly aligned on line 16
WARNING: unquoted resource title on line 4
Don’t commit errors on your puppet git repo

Create a Git Pre-Commit Hook

nano .git/hooks/pre-commit

Get updated code in the link above

#!/bin/sh

syntax_errors=0
error_msg=$(mktemp /tmp/error_msg.XXXXXX)

if git rev-parse --quiet --verify HEAD > /dev/null
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Get list of new/modified manifest and template files to check (in git index)
for indexfile in `git diff-index --diff-filter=AM --name-only --cached $against | egrep '\.(pp|erb)'`
do
    # Don't check empty files
    if [ `git cat-file -s :0:$indexfile` -gt 0 ]
    then
        case $indexfile in
            *.pp )
                # Check puppet manifest syntax
                #git cat-file blob :0:$indexfile | puppet --color=false --parseonly --ignoreimport > $error_msg ;;
                # Updated for 2.7.x
                puppet parser validate $indexfile > $error_msg ;;
            *.erb )
                # Check ERB template syntax
                # -P : ignore lines which start with "%"
                git cat-file blob :0:$indexfile | erb -P -x -T - | ruby -c 2> $error_msg > /dev/null ;;
        esac
        if [ "$?" -ne 0 ]
        then
            echo -n "$indexfile: "
            cat $error_msg
            syntax_errors=`expr $syntax_errors + 1`
        fi
    fi
done

rm -f $error_msg

if [ "$syntax_errors" -ne 0 ]
then
    echo "Error: $syntax_errors syntax errors found, aborting commit."
    exit 1
fi

basically it will run this command before the commit:

puppet parser validate owncloud6/manifests/init.pp

Git commit will result as an error if your manifests have a syntax error.

manifests/nodes.pp: err: Could not parse for environment production: Syntax error at '}' at /root/puppet/manifests/nodes.pp:567
err: Try 'puppet help parser validate' for usage
Error: 1 syntax errors found, aborting commit.

Tagged on: , , , ,

Leave a Reply

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