Files
software-infrastructure/.gitea/scripts/check-playbooks.sh
claudeandClaude Opus 5.5 b15bb337e9 G3: syntax-check and lint this repo's playbooks in CI
.gitea/scripts/check-playbooks.sh runs ansible-playbook --syntax-check on
every top-level playbook and ansible-lint (production profile, with
yamllint's rules) on the repo's own files, in a temporary copy with empty
secrets stubs, so it never touches a real secrets file. Roles are excluded;
they're linted in their own repos.

Baselines so the check passes today and fails on new problems:
- .gitea/playbooks-skip.txt: nine playbooks that fail the syntax check now
  (missing roles, an unknown module, an unfinished playbook), with reasons.
- .ansible-lint-ignore: 110 existing file/rule findings.

The workflow (.gitea/workflows/playbooks.yaml) is manual-only until S8 passes
and the ci-tools image is published; its header has the push/pull_request
trigger. Docs: docs/ci-cd/playbook-checks.md.

Refs DevOps/software-infrastructure#51

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:25:00 -05:00

93 lines
3.0 KiB
Bash

#!/bin/sh
# Check this repo's playbooks. DevOps/software-infrastructure#51 (G3).
# CI runs exactly this (.gitea/workflows/playbooks.yaml) in the ci-tools
# image; run it locally from the repo root the same way:
#
# sh .gitea/scripts/check-playbooks.sh # syntax, then lint
# sh .gitea/scripts/check-playbooks.sh syntax # ansible-playbook --syntax-check
# sh .gitea/scripts/check-playbooks.sh lint # ansible-lint (with yamllint rules)
# sh .gitea/scripts/check-playbooks.sh syntax gitea.yaml www.yaml
#
# Role submodules must be checked out first. They are all public:
# git -c url."https://gitea.eom.dev/".insteadOf="git@gitea.eom.dev:" \
# submodule update --init --jobs 4
#
# Playbooks load ../secrets.yaml (and a few vars/secrets.yaml). These checks
# don't evaluate variables, so an empty stub is enough. The repo is copied to
# a temporary directory and the stubs are written there, so this never reads,
# creates or overwrites a real secrets file.
#
# Playbooks in .gitea/playbooks-skip.txt are left out of both checks. Lint
# findings that already existed are in .ansible-lint-ignore. Regenerate that
# baseline only to drop fixed entries, never to hide a new finding.
set -eu
mode="${1:-all}"
[ "$#" -gt 0 ] && shift
repo="$(cd "$(dirname "$0")/../.." && pwd)"
skip_file="$repo/.gitea/playbooks-skip.txt"
export NO_COLOR=1 ANSIBLE_NOCOLOR=1 ANSIBLE_FORCE_COLOR=0
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
mkdir "$work/repo"
(cd "$repo" && tar -c --exclude=./.git -f - .) | tar -x -C "$work/repo"
printf '%s\n' '# Empty stub for CI checks only.' '{}' > "$work/secrets.yaml"
for d in "$work/repo" "$work/repo/vars"; do
mkdir -p "$d"
[ -e "$d/secrets.yaml" ] || cp "$work/secrets.yaml" "$d/secrets.yaml"
done
cd "$work/repo"
skips="$(sed -e 's/#.*//' -e '/^[[:space:]]*$/d' "$skip_file" 2>/dev/null | awk '{print $1}')"
is_skipped() {
printf '%s\n' "$skips" | grep -q -x -F -- "$1"
}
run_syntax() {
if [ "$#" -gt 0 ]; then
playbooks="$*"
else
# Every top-level playbook: a YAML file with a play-level hosts: key.
playbooks="$(grep -l -E '^[[:space:]-]*hosts:' -- *.yaml *.yml 2>/dev/null | sort)"
fi
failed=""
checked=0
for pb in $playbooks; do
if is_skipped "$pb"; then
echo "skip $pb (see .gitea/playbooks-skip.txt)"
continue
fi
checked=$((checked + 1))
if out="$(ansible-playbook --syntax-check -i localhost, "$pb" 2>&1)"; then
echo "ok $pb"
else
echo "FAIL $pb"
printf '%s\n' "$out" | sed 's/^/ /'
failed="$failed $pb"
fi
done
echo "syntax: checked $checked playbook(s)"
if [ -n "$failed" ]; then
echo "syntax check failed:$failed" >&2
return 1
fi
}
run_lint() {
set --
for pb in $skips; do set -- "$@" --exclude "$pb"; done
echo "ansible-lint (baseline: .ansible-lint-ignore)"
ansible-lint --nocolor -f pep8 "$@"
}
case "$mode" in
syntax) run_syntax "$@" ;;
lint) run_lint ;;
all) status=0; run_syntax || status=1; run_lint || status=1; exit "$status" ;;
*) echo "usage: $0 [syntax|lint|all] [playbook...]" >&2; exit 2 ;;
esac