This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
pi-ansible/provision.sh

92 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env sh
set -e
usage() {
printf '%s\n' "Usage:"
printf '$ %s\n' "$0 [--help]"
printf '$ %s\n' "$0 [--dry] os"
printf '$ %s\n' "$0 [--dry] base"
printf '$ %s\n' "$0 [--dry] firewall"
printf '$ %s\n' "$0 [--dry] ssh"
printf '$ %s\n' "$0 [--dry] docker"
printf '$ %s\n' "$0 [--dry] docker_config"
printf '$ %s\n' "$0 [--dry] users [--init]"
printf '$ %s\n' "$0 [--dry] reboot"
printf '$ %s\n' "$0 [--dry] services [--down|--restart|--recreate] [SINGLE_SERVICE]"
}
install_modules() {
if [ -z "$(ansible-galaxy collection list community.general 2>/dev/null)" ]; then
ansible-galaxy collection install community.general
fi
}
BASE_CMD="ansible-playbook playbook.yml --ask-vault-pass --ask-become-pass"
cd "$(dirname "$0")" || exit 255
if [ "$1" = "--dry" ]; then
EXEC="echo"
shift
else
EXEC="eval"
fi
if [ "$#" -gt 0 ]; then
TAG="$1"
shift
fi
case $TAG in
"")
install_modules; $EXEC "$BASE_CMD" ;;
os|base|firewall|ssh|docker|docker_config)
install_modules; $EXEC "$BASE_CMD --tags '$TAG'" ;;
users)
install_modules
if [ "$1" = "--init" ]; then
$EXEC "$BASE_CMD --user root --tags '$TAG'"
else
$EXEC "$BASE_CMD --tags '$TAG'"
fi
;;
reboot)
install_modules; $EXEC "$BASE_CMD --tags '$TAG' --extra-vars 'do_reboot=true'" ;;
services)
install_modules
if [ "$1" = "--down" ]; then
ACTION="down"
shift
elif [ "$1" = "--restart" ]; then
ACTION="restart"
shift
elif [ "$1" = "--recreate" ]; then
ACTION="recreate"
shift
fi
case $1 in
--*)
;;
*)
SINGLE_SERVICE="$1" ;;
esac
if [ -z "$ACTION" ] && [ -n "$SINGLE_SERVICE" ]; then
VARS="single_service=$SINGLE_SERVICE"
elif [ -n "$ACTION" ] && [ -z "$SINGLE_SERVICE" ]; then
VARS="$ACTION=true"
elif [ -n "$ACTION" ] && [ -n "$SINGLE_SERVICE" ]; then
VARS='{"'$ACTION'": true, "single_service": "'$SINGLE_SERVICE'"}'
fi
$EXEC "$BASE_CMD --tags '$TAG' $(test -z "$VARS" || echo "--extra-vars '$VARS'")"
;;
--help)
usage ;;
*)
usage >&2; exit 1 ;;
esac