Add pre-commit hook to check playbook syntax

This commit is contained in:
Aleksey Zalesov 2018-02-24 16:01:22 +03:00
parent bbdfb8c6da
commit db944c8ab4
1 changed files with 30 additions and 0 deletions

30
hooks/pre-commit.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/sh
# This is the pre-commit hook that runs each time with `git commit`
# and checks syntax of the playbooks
# To install run
# ln -s ../../hooks/pre-commit.sh .git/hooks/pre-commit
echo "Running pre-commit hook"
STASH_NAME="pre-commit-$(date +%s)"
git stash save -q --keep-index $STASH_NAME
echo "Checking the playbook syntax"
ansible-playbook -i hosts site.yml --syntax-check
RESULT=$?
if [[ $RESULT == 0 ]]; then
echo "Running Ansible linter"
ansible-lint *.yml
RESULT=$?
fi
STASHES=$(git stash list)
if [[ $STASHES =~ .*$STASH_NAME ]]; then
git stash pop -q
fi
[ $RESULT -ne 0 ] && exit 1
exit 0