Files
homelab-infra/ansible/ci/ansible-target.sh
T
karpenok 842a633916
ansible-core-net / deploy (push) Successful in 27s
ansible-core-secrets / deploy (push) Successful in 12s
ansible-rus-central / deploy (push) Successful in 36s
chore(ansible,ci): align workflow paths, shared letsencrypt_email, and netbird task tags
2026-11-11 11:35:45 +07:00

94 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
# Печатает аргументы ansible-playbook для одной машины.
# Полный прогон только для общих файлов: playbooks/site.yml, --limit, <машина>
# Файл роли: playbooks/<машина>.yml, --tags, каталог роли
# Тег берётся из каталога ansible/roles/<машина>/<продукт>/ и должен совпадать с тегом в плейбуке машины.
# Файл в roles/<машина>/ вне такого каталога завершает скрипт с кодом 2.
# Коммит без файлов этой машины ничего не печатает и завершается с кодом 0.
set -eu
machine=${1:?machine}
event=${2:-}
before=${3:-}
after=${4:-}
case "$machine" in
core-net | core-secrets | rus-central) ;;
*)
echo "Неизвестная машина: $machine" >&2
exit 2
;;
esac
full=0
changed=""
case "$event" in
push) ;;
*) full=1 ;;
esac
case "$before" in
"" | 0000000000000000000000000000000000000000) full=1 ;;
esac
if [ "$full" -eq 0 ]; then
changed=$(git diff --name-only "$before" "$after" 2>/dev/null) || full=1
fi
tags=""
full_from_files=0
unknown=0
playbook="ansible/playbooks/${machine}.yml"
add_tag() {
case ",$tags," in
*",$1,"*) ;;
*) tags="${tags:+$tags,}$1" ;;
esac
}
reject_unknown() {
echo "Неизвестный файл продукта на ${machine}: $1" >&2
unknown=1
}
if [ "$full" -eq 0 ] && [ -n "$changed" ]; then
old_ifs=$IFS
IFS='
'
set -f
for file in $changed; do
[ -n "$file" ] || continue
case "$file" in
ansible/ansible.cfg | ansible/ci/* | ansible/roles/host/* | ansible/playbooks/host.yml | ansible/playbooks/site.yml | ansible/playbooks/"$machine".yml | ansible/inventories/prod/hosts.yml | ansible/inventories/prod/group_vars/* | ansible/inventories/prod/host_vars/"$machine".yml)
full_from_files=1
;;
ansible/roles/"$machine"/*/*)
product=${file#ansible/roles/"$machine"/}
product=${product%%/*}
if grep -q "role: ${machine}/${product}" "$playbook" && grep -q "tags: \[${product}\]" "$playbook"; then
add_tag "$product"
else
reject_unknown "$file"
fi
;;
ansible/roles/"$machine"/*)
reject_unknown "$file"
;;
esac
done
set +f
IFS=$old_ifs
fi
if [ "$unknown" -eq 1 ]; then
exit 2
fi
if [ "$full" -eq 1 ] || [ "$full_from_files" -eq 1 ]; then
printf '%s\n' "playbooks/site.yml" "--limit" "$machine"
elif [ -n "$tags" ]; then
printf '%s\n' "playbooks/${machine}.yml" "--tags" "$tags"
fi