mirror of
https://github.com/dredx/prole.git
synced 2026-09-23 11:03:59 +00:00
103 lines
3.2 KiB
Bash
Executable File
103 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
export ANSIBLE_CONFIG="${ROOT_DIR}/ansible.cfg"
|
|
|
|
# Defaults
|
|
MODE="fg" # fg|bg
|
|
PLAYBOOK="infrastructure/playbooks/site.yml"
|
|
VAULT_PASS_FILE="" # optional
|
|
LIMIT="" # optional
|
|
TAGS="" # optional
|
|
EXTRA_ARGS=() # passthrough
|
|
LOG_DIR="${ROOT_DIR}/.ansible/logs"
|
|
SYSLOG_HOST="" # e.g. loghost.prole.org
|
|
SYSLOG_PORT="514"
|
|
SYSLOG_TAG="ansible"
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
Usage: ./ansible.sh [options] [-- <extra ansible-playbook args>]
|
|
|
|
Options:
|
|
-p, --playbook PATH Playbook path (default: ${PLAYBOOK})
|
|
-l, --limit HOSTS Limit hosts
|
|
-t, --tags TAGS Tags
|
|
-v, --vault-pass-file FILE Vault password file
|
|
-m, --mode fg|bg Run in foreground or background (default: fg)
|
|
--syslog-host HOST Send start/end markers via UDP syslog to HOST
|
|
--syslog-port PORT Syslog UDP port (default: 514)
|
|
--syslog-tag TAG Syslog tag (default: ansible)
|
|
-h, --help Show help
|
|
|
|
Examples:
|
|
./ansible.sh -l pi.prole.org -t iscsi -v .vault_pass
|
|
./ansible.sh -m bg -p infrastructure/playbooks/site.yml -v .vault_pass
|
|
./ansible.sh --syslog-host loghost.prole.org -m bg -v .vault_pass -- -vv
|
|
EOF
|
|
}
|
|
|
|
send_syslog() {
|
|
local msg="$1"
|
|
if [[ -n "${SYSLOG_HOST}" ]]; then
|
|
# -d = UDP, -n host, -P port
|
|
logger -d -n "${SYSLOG_HOST}" -P "${SYSLOG_PORT}" -t "${SYSLOG_TAG}" -- "${msg}" || true
|
|
fi
|
|
}
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-p|--playbook) PLAYBOOK="$2"; shift 2 ;;
|
|
-l|--limit) LIMIT="$2"; shift 2 ;;
|
|
-t|--tags) TAGS="$2"; shift 2 ;;
|
|
-v|--vault-pass-file) VAULT_PASS_FILE="$2"; shift 2 ;;
|
|
-m|--mode) MODE="$2"; shift 2 ;;
|
|
--syslog-host) SYSLOG_HOST="$2"; shift 2 ;;
|
|
--syslog-port) SYSLOG_PORT="$2"; shift 2 ;;
|
|
--syslog-tag) SYSLOG_TAG="$2"; shift 2 ;;
|
|
--) shift; EXTRA_ARGS+=("$@"); break ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
*) EXTRA_ARGS+=("$1"); shift ;;
|
|
esac
|
|
done
|
|
|
|
mkdir -p "${LOG_DIR}"
|
|
|
|
ts="$(date +%Y%m%d-%H%M%S)"
|
|
logfile="${LOG_DIR}/ansible-${ts}.log"
|
|
|
|
cmd=(ansible-playbook "${PLAYBOOK}")
|
|
[[ -n "${LIMIT}" ]] && cmd+=("--limit" "${LIMIT}")
|
|
[[ -n "${TAGS}" ]] && cmd+=("--tags" "${TAGS}")
|
|
[[ -n "${VAULT_PASS_FILE}" ]] && cmd+=("--vault-password-file" "${VAULT_PASS_FILE}")
|
|
cmd+=("${EXTRA_ARGS[@]}")
|
|
|
|
send_syslog "START playbook=${PLAYBOOK} limit=${LIMIT:-<none>} tags=${TAGS:-<none>} log=${logfile}"
|
|
|
|
echo "ANSIBLE_CONFIG=${ANSIBLE_CONFIG}"
|
|
echo "LOGFILE=${logfile}"
|
|
echo "CMD: ${cmd[*]}"
|
|
|
|
if [[ "${MODE}" == "fg" ]]; then
|
|
# Stream to terminal and file
|
|
set +e
|
|
"${cmd[@]}" 2>&1 | tee "${logfile}"
|
|
rc=${PIPESTATUS[0]}
|
|
set -e
|
|
else
|
|
# Background: nohup to logfile
|
|
nohup "${cmd[@]}" >"${logfile}" 2>&1 &
|
|
rc=0
|
|
echo "Started in background (pid $!)"
|
|
fi
|
|
|
|
if [[ "${MODE}" == "fg" ]]; then
|
|
if [[ $rc -eq 0 ]]; then
|
|
send_syslog "END OK playbook=${PLAYBOOK} limit=${LIMIT:-<none>} tags=${TAGS:-<none>} log=${logfile}"
|
|
else
|
|
send_syslog "END FAIL rc=${rc} playbook=${PLAYBOOK} limit=${LIMIT:-<none>} tags=${TAGS:-<none>} log=${logfile}"
|
|
fi
|
|
exit $rc
|
|
fi
|