#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail STRICT="${STRICT:-0}" ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" ANSIBLE_DIR="$ROOT_DIR/infra-run/ansible" ok_count=0 warn_count=0 fail_count=0 ok() { printf 'OK: %s\n' "$*" ok_count=$((ok_count + 1)) } warning() { printf 'WARNING: %s\n' "$*" warn_count=$((warn_count + 1)) } critical() { printf 'CRITICAL: %s\n' "$*" fail_count=$((fail_count + 1)) } if [[ ! -d "$ANSIBLE_DIR" ]]; then warning "No infra-run/ansible directory found" printf '\nAnsible summary: %d OK, %d WARNING, %d CRITICAL\n' "$ok_count" "$warn_count" "$fail_count" exit 0 fi mapfile -t yaml_files < <(find "$ANSIBLE_DIR" -type f \( -name '*.yml' -o -name '*.yaml' \) -print | sort) if ((${#yaml_files[@]} == 0)); then warning "No Ansible YAML files found" else ok "Found ${#yaml_files[@]} Ansible YAML files" fi if command -v ansible-playbook >/dev/null 2>&1; then while IFS= read -r playbook; do [[ -n "$playbook" ]] || continue playbook_rel="${playbook#"$ANSIBLE_DIR"/}" if (cd "$ANSIBLE_DIR" && ansible-playbook --syntax-check -i inventory/hosts.yml "$playbook_rel"); then ok "ansible syntax $playbook_rel" else critical "ansible syntax failed $playbook_rel" fi done < <(find "$ANSIBLE_DIR/playbooks" -type f \( -name '*.yml' -o -name '*.yaml' \) -print | sort) else if [[ "$STRICT" == "1" ]]; then critical "ansible-playbook not installed" else warning "ansible-playbook not installed; skipped syntax checks" fi fi if command -v ansible-lint >/dev/null 2>&1; then if (cd "$ANSIBLE_DIR" && ansible-lint playbooks roles); then ok "ansible-lint" else critical "ansible-lint reported issues" fi else if [[ "$STRICT" == "1" ]]; then critical "ansible-lint not installed" else warning "ansible-lint not installed; skipped optional lint" fi fi if command -v yamllint >/dev/null 2>&1; then if yamllint "$ANSIBLE_DIR"; then ok "yamllint infra-run/ansible" else critical "yamllint reported issues in infra-run/ansible" fi else if [[ "$STRICT" == "1" ]]; then critical "yamllint not installed" else warning "yamllint not installed; skipped optional YAML lint" fi fi printf '\nAnsible summary: %d OK, %d WARNING, %d CRITICAL\n' "$ok_count" "$warn_count" "$fail_count" if ((fail_count > 0)); then exit 1 fi exit 0