Skip to content

Instantly share code, notes, and snippets.

@princebot
Created October 28, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save princebot/2bd84b3b344168db22ce1259241f4a88 to your computer and use it in GitHub Desktop.
Save princebot/2bd84b3b344168db22ce1259241f4a88 to your computer and use it in GitHub Desktop.
(bash) A Git pre-commit hook that rejects commits to Ansible projects that contain unencrypted Ansible Vault files.
#!/usr/bin/env bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Name: pre-commit
# Author: prince@princebot.com
# Synopsis: Reject commits containing unencrypted Ansible Vault files.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set -e
vault_files=($(IFS=$'\n' find . -type f -name 'vault'))
for f in "${vault_files[@]}"; do
if ! head -n 1 "$f" | grep -E '^\$ANSIBLE_VAULT' >/dev/null; then
abspath=$(cd "$(dirname "$f")" && pwd -P)/$(basename "$f")
>&2 printf -- "\n$(tput bold)$(tput setaf 1)\t"
>&2 printf -- "error: commit rejected\n"
>&2 printf -- "$(tput sgr0)$(tput setaf 1)\t"
>&2 printf -- "${abspath} is not encrypted with ansible-vault\n\n"
>&2 printf -- "$(tput sgr0)"
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment