#!/usr/bin/env bash HOSTS="$1" ROLE_LIST=( "virt-common" "docker" "apps" "postgresql" "proxy" ) # 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