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

View file

@ -1,27 +1,51 @@
#!/usr/bin/env bash #!/usr/bin/env bash
HOSTS="$1" HOSTS="$1"
ROLE_LIST=( "virt-common" "docker" "apps" "postgresql" "proxy" )
case $HOSTS in # Dynamically remove roles that are incompatible with a host
*:*) OLDIFS="$IFS"
ROLES="['virt-common']" ;; IFS=:
for target in $HOSTS; do
case $target in
*app*) *app*)
ROLES="['virt-common', 'docker', 'apps']" ;; ROLE_LIST=( "${ROLE_LIST[@]/postgresql}" )
ROLE_LIST=( "${ROLE_LIST[@]/proxy}" )
;;
*db*) *db*)
ROLES="['virt-common', 'postgresql']" ;; 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*) *prx*|proxy*)
ROLES="['virt-common', 'docker', 'proxy']" ;; ROLE_LIST=( "${ROLE_LIST[@]/apps}" )
ROLE_LIST=( "${ROLE_LIST[@]/postgresql}" )
;;
*) *)
ROLES="['virt-common']" ;; ROLE_LIST=( "virt-common" ) ;;
esac 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 HOSTS
export ROLES export ROLES
# Execution of temporary envsubst'ed playbook
PLAYBOOK="playbook-$(tr -dc A-Za-z < /dev/urandom | head -c 10).yml" PLAYBOOK="playbook-$(tr -dc A-Za-z < /dev/urandom | head -c 10).yml"
envsubst < site.yml > "$PLAYBOOK" envsubst < site.yml > "$PLAYBOOK"
ansible-playbook "$PLAYBOOK" ansible-playbook "$PLAYBOOK"
STATUS=$? STATUS=$?
# Clean up temporary playbook
rm -f "$PLAYBOOK" rm -f "$PLAYBOOK"
exit $STATUS exit $STATUS