#!/usr/bin/env bash # The ansible-lint pre-commit hook: ansible-lint --offline against the pinned collections. # # CI sets ANSIBLE_COLLECTIONS_PATH (the ci image bakes requirements.yml into /opt/collections) and # this script then leaves the environment alone. A local run does not set it, and ansible-lint would # resolve modules from the control host's ~/.ansible, whose collection versions need not be the # ones CI tests against (#118, #80). So when the variable is unset, this script uses the collections # scripts/dev-setup.sh installed into .ansible/collections of this clone, and fails with the fix # when they are missing. That covers `git commit` and a bare `pre-commit run` alike. # # The Collection's own role names (mortis.kamino., which the molecule scenarios' converge # plays use) only resolve from an / directory under an ansible_collections root, # and a checkout is not one. So the checkout is linked into a temporary root, put first on the # collections path, and ansible-lint reads the checkout itself through it (#118 step 5). The link # is removed on exit. set -euo pipefail root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$root" if [ -z "${ANSIBLE_COLLECTIONS_PATH:-}" ]; then if [ ! -d "$root/.ansible/collections/ansible_collections" ]; then echo "error: the pinned collections are not installed in $root/.ansible/collections; run scripts/dev-setup.sh" >&2 exit 1 fi export ANSIBLE_HOME="$root/.ansible" export ANSIBLE_COLLECTIONS_PATH="$root/.ansible/collections" fi # The Collection's own name, from galaxy.yml, so a rename needs no edit here. namespace="$(sed -n 's/^namespace: *//p' galaxy.yml)" name="$(sed -n 's/^name: *//p' galaxy.yml)" if [ -z "$namespace" ] || [ -z "$name" ]; then echo "error: no namespace and name in $root/galaxy.yml" >&2 exit 1 fi link_root="$(mktemp -d)" trap 'rm -rf "$link_root"' EXIT mkdir -p "$link_root/ansible_collections/$namespace" ln -s "$root" "$link_root/ansible_collections/$namespace/$name" export ANSIBLE_COLLECTIONS_PATH="$link_root:$ANSIBLE_COLLECTIONS_PATH" # Not exec: the trap above has to run when ansible-lint ends. ansible-lint --offline --nocolor