diff --git a/tools/play.sh b/tools/play.sh index 1b96e89..b451e50 100755 --- a/tools/play.sh +++ b/tools/play.sh @@ -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