Dynamically exclude roles based on host type

This commit is contained in:
Sam A. 2024-01-01 17:53:40 +01:00
parent 99942dadfb
commit f4249ea484
Signed by: samsapti
GPG Key ID: CBBBE7371E81C4EA
1 changed files with 36 additions and 12 deletions

View File

@ -1,27 +1,51 @@
#!/usr/bin/env bash
HOSTS="$1"
ROLE_LIST=( "virt-common" "docker" "apps" "postgresql" "proxy" )
case $HOSTS in
*:*)
ROLES="['virt-common']" ;;
*app*)
ROLES="['virt-common', 'docker', 'apps']" ;;
*db*)
ROLES="['virt-common', 'postgresql']" ;;
*prx*|proxy*)
ROLES="['virt-common', 'docker', 'proxy']" ;;
*)
ROLES="['virt-common']" ;;
esac
# Dynamically remove roles that are incompatible with a host
OLDIFS="$IFS"
IFS=:
for target in $HOSTS; do
case $target in
*app*)
ROLE_LIST=( "${ROLE_LIST[@]/postgresql}" )
ROLE_LIST=( "${ROLE_LIST[@]/proxy}" )
;;
*db*)
ROLE_LIST=( "${ROLE_LIST[@]/docker}" )
ROLE_LIST=( "${ROLE_LIST[@]/apps}" )
ROLE_LIST=( "${ROLE_LIST[@]/proxy}" )
;;
*mda*|media*)
ROLE_LIST=( "${ROLE_LIST[@]/apps}" )
ROLE_LIST=( "${ROLE_LIST[@]/postgresql}" )
ROLE_LIST=( "${ROLE_LIST[@]/proxy}" )
;;
*prx*|proxy*)
ROLE_LIST=( "${ROLE_LIST[@]/apps}" )
ROLE_LIST=( "${ROLE_LIST[@]/postgresql}" )
;;
*)
ROLE_LIST=( "virt-common" ) ;;
esac
done
IFS="$OLDIFS"
# Transform role list into JSON array
ROLES="$(jq -c -M -n '$ARGS.positional | map(select(length > 0))' --args -- "${ROLE_LIST[@]}")"
echo "Running roles $ROLES against $HOSTS"
# Export vars for envsubst
export HOSTS
export ROLES
# Execution of temporary envsubst'ed playbook
PLAYBOOK="playbook-$(tr -dc A-Za-z < /dev/urandom | head -c 10).yml"
envsubst < site.yml > "$PLAYBOOK"
ansible-playbook "$PLAYBOOK"
STATUS=$?
# Clean up temporary playbook
rm -f "$PLAYBOOK"
exit $STATUS