How do you set up pre-commit hooks for an Ansible repo?
BeginnerProductionTesting, CI/CD and Quality
Answer
Pre-commit runs the cheap checks before code ever reaches CI, so reviews focus on design instead of indentation.
# .pre-commit-config.yaml
repos:
- repo: https://github.com/adrienverge/yamllint
rev: v1.35.1
hooks:
- id: yamllint
- repo: https://github.com/ansible/ansible-lint
rev: v25.1.3
hooks:
- id: ansible-lint
- repo: https://github.com/gitleaks/gitleaks
rev: v8.21.2
hooks:
- id: gitleaks
pip install pre-commit
pre-commit install # hook into git commit
pre-commit run --all-files # first full run
pre-commit autoupdate # bump pinned revs deliberately
CI runs pre-commit run --all-files too, because hooks on laptops can be skipped with --no-verify.
More on Testing, CI/CD and Quality
What does a testing pyramid look like for Ansible code?BeginnerHow do you roll out `ansible-lint` profiles across a team without blocking everyone?IntermediateWhen do you use `skip_list`, `warn_list` and `# noqa`?IntermediateHow is a Molecule scenario laid out, and what does `molecule test` actually run?IntermediateDocker, Podman or real VMs as the Molecule driver: how do you choose?AdvancedHow do you test roles that manage systemd services inside Molecule containers?Advanced