Files
Kamino/scripts/ansible-lint.sh
T
Defame1297andClaude Sonnet 5.5 3289c0d430
Build / build (pull_request) Failing after 11s
Lint / lint (pull_request) Successful in 20s
Molecule / molecule (pull_request) Successful in 1s
ci(lint): resolve the Collection's own roles and drop mock_roles
Step 5 of #118. The scenarios' converge plays call the roles by FQCN (mortis.kamino.<role>), which only resolves from an ansible_collections/<namespace>/<name> directory, and a checkout is not one, so .ansible-lint listed the five roles in mock_roles and ansible-lint never checked those plays against the real roles.

scripts/ansible-lint.sh, which the ansible-lint hook runs in CI and locally, now links the checkout as ansible_collections/<namespace>/<name> in a temporary root (namespace and name read from galaxy.yml), puts that root first on ANSIBLE_COLLECTIONS_PATH and removes it on exit, so ansible-lint reads the checkout itself. Removing mock_roles reproduced five syntax-check[specific] role-not-found errors; with the link the run passes with no warnings, locally and with CI's preset path. A bare ansible-lint, outside the hook, does not get the link; .ansible-lint says so.

No image input changes: .pre-commit-config.yaml, requirements and images/** are untouched, so no version bump or second pull request. Closes the last open item of #118.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
2026-11-14 15:38:34 +00:00

46 lines
2.1 KiB
Bash
Executable File

#!/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.<role>, which the molecule scenarios' converge
# plays use) only resolve from an <namespace>/<name> 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