Pre-Commit Hook Script
Example of a pre-commit hook script in the ansible-inventory reference implementation, demonstrating automated linting for Ansible projects.
Categories:
The ansible-inventory
reference implementation
c2platform/c2/ansible-inventory contains an example of a pre-commit hook script. The script
runs YAML and Ansible linting tools to ensure code quality before commits. It
activates a virtual environment, runs yamllint and ansible-lint, and fails the
commit if errors are found.
doc/howto-development/pre-commit
#!/bin/bash
set -euo pipefail
C2_LINTERS_VIRTUALENV=${C2_LINTERS_VIRTUALENV:-c2d}
export ANSIBLE_COLLECTIONS_PATH=./
# shellcheck disable=SC1090
source "${HOME}/.virtualenv/${C2_LINTERS_VIRTUALENV}/bin/activate"
yamllint -c .yamllint . > yamllint.log 2>&1 || true
if [[ -s yamllint.log ]]; then
echo "yamllint failed. See yamllint.log for details."
exit 1
fi
if ! ansible-lint -c .ansible-lint --nocolor > ansible-lint.log 2>&1; then
echo "ansible-lint failed. See ansible-lint.log for details"
exit 1
fi
Additional information
- Clone script: Automate setup of the development environment with multiple Git repositories.
- Ansible Inventory Project: A structured collection of files used for managing hosts and configurations. It typically includes inventory files, playbooks, host configurations, group variables, and Ansible vault files.
- Ansible Collection Project: An Ansible Collection project is a comprehensive unit that combines modules, plugins, roles, and documentation to enhance the automation language and manage infrastructures. It serves as a reusable and distributable package of Ansible content.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.