From a62f57f4987884bc2f3143edec13444aa7550d28 Mon Sep 17 00:00:00 2001 From: jdongmo Date: Sat, 20 Nov 2021 16:00:34 -0500 Subject: [PATCH] update run script --- run.sh | 116 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/run.sh b/run.sh index 6311a35..df25d81 100755 --- a/run.sh +++ b/run.sh @@ -1,58 +1,58 @@ -#!/usr/bin/env bash - -# ENV Vars: -# VAGRANT_MODE - [0,1] -# - to be used with bovine-inventory's vagrant mode -# ANSIBLE_RUN_MODE - ["playbook","ad-hoc"] -# - specify which mode to run ansible in -# ANSIBLE_PLAYBOOK_FILE - defaults to "infra.yml" -# - specify playbook to pass to ansible-playbook -# - NB: only used when run mode is "playbook" -# ANSIBLE_BASE_ARA - ["0","1"] -# - a bash STRING (not numeral) to enable ARA -# VAULT_PASSWORD_FILE - - -export ANSIBLE_RUN_MODE="${ANSIBLE_RUN_MODE:-playbook}" -export ANSIBLE_PLAYBOOK_FILE="${ANSIBLE_PLAYBOOK_FILE:-infra.yml}" -export VAULT_PASSWORD_FILE="${VAULT_PASSWORD_FILE:-${HOME}/.ssh/creds/vault_password.txt}" -export VAGRANT_MODE="${VAGRANT_MODE:-0}" - -run_ansible() { - INOPTS=( "$@" ) - VAULTOPTS="" - # Plaintext vault decryption key, not checked into SCM - if [ -f "${VAULT_PASSWORD_FILE}" ]; then - VAULTOPTS="--vault-password-file=${VAULT_PASSWORD_FILE}" - if [ ${ANSIBLE_RUN_MODE} == 'playbook' ]; then - time ansible-playbook --diff ${VAULTOPTS} "${ANSIBLE_PLAYBOOK_FILE}" "${INOPTS[@]}" - return $? - elif [ ${ANSIBLE_RUN_MODE} == 'ad-hoc' ]; then - time ansible --diff ${VAULTOPTS} "${INOPTS[@]}" - return $? - fi - else - if [ "${ANSIBLE_RUN_MODE}" == 'playbook' ]; then - echo "Vault password file unreachable. Skip steps require vault." - VAULTOPTS="--skip-tags=requires_vault" - #echo "ansible-playbook --diff $VAULTOPTS ${INOPTS[@]} ${ANSIBLE_PLAYBOOK_FILE}" && \ - time ansible-playbook --diff ${VAULTOPTS} "${ANSIBLE_PLAYBOOK_FILE}" "${INOPTS[@]}" - return $? - elif [ "${ANSIBLE_RUN_MODE}" == 'ad-hoc' ]; then - #echo "ansible --diff $VAULTOPTS ${INOPTS[@]}" && \ - time ansible --diff ${VAULTOPTS} "${INOPTS[@]}" - return $? - else - echo "Invalid run mode: ${ANSIBLE_RUN_MODE}" - exit 15 - fi - fi -} - -if [ "${VAGRANT_MODE}" -eq 1 ]; then - export ANSIBLE_SSH_ARGS="-o UserKnownHostsFile=/dev/null" - export ANSIBLE_HOST_KEY_CHECKING=false -fi - -run_ansible "$@" -retcode=$? -exit $retcode +#!/usr/bin/env bash + +# ENV Vars: +# VAGRANT_MODE - [0,1] +# - to be used with bovine-inventory's vagrant mode +# ANSIBLE_RUN_MODE - ["playbook","ad-hoc"] +# - specify which mode to run ansible in +# ANSIBLE_PLAYBOOK_FILE - defaults to "infra.yml" +# - specify playbook to pass to ansible-playbook +# - NB: only used when run mode is "playbook" +# ANSIBLE_BASE_ARA - ["0","1"] +# - a bash STRING (not numeral) to enable ARA +# VAULT_PASSWORD_FILE - + +export ANSIBLE_RUN_MODE="${ANSIBLE_RUN_MODE:-playbook}" +export ANSIBLE_PLAYBOOK_FILE="${ANSIBLE_PLAYBOOK_FILE:-infra.yml}" +export VAULT_PASSWORD_FILE="${VAULT_PASSWORD_FILE:-${HOME}/.ssh/creds/vault_password.txt}" +export VAGRANT_MODE="${VAGRANT_MODE:-0}" + +run_ansible() { + INOPTS=( "$@" ) + VAULTOPTS="" + # Plaintext vault decryption key, not checked into SCM + if [ -f "${VAULT_PASSWORD_FILE}" ]; then + VAULTOPTS="--vault-password-file=${VAULT_PASSWORD_FILE}" + if [ ${ANSIBLE_RUN_MODE} == 'playbook' ]; then + time ansible-playbook --diff ${VAULTOPTS} "${ANSIBLE_PLAYBOOK_FILE}" "${INOPTS[@]}" + return $? + elif [ ${ANSIBLE_RUN_MODE} == 'ad-hoc' ]; then + time ansible --diff ${VAULTOPTS} "${INOPTS[@]}" + return $? + fi + else + if [ "${ANSIBLE_RUN_MODE}" == 'playbook' ]; then + echo "Vault password file unreachable. Skip steps require vault." + VAULTOPTS="--skip-tags=requires_vault" + #echo "ansible-playbook --diff $VAULTOPTS ${INOPTS[@]} ${ANSIBLE_PLAYBOOK_FILE}" && \ + time ansible-playbook --diff ${VAULTOPTS} "${ANSIBLE_PLAYBOOK_FILE}" "${INOPTS[@]}" + return $? + elif [ "${ANSIBLE_RUN_MODE}" == 'ad-hoc' ]; then + #echo "ansible --diff $VAULTOPTS ${INOPTS[@]}" && \ + time ansible --diff ${VAULTOPTS} "${INOPTS[@]}" + return $? + else + echo "Invalid run mode: ${ANSIBLE_RUN_MODE}" + exit 15 + fi + fi +} + +if [ "${VAGRANT_MODE}" -eq 1 ]; then + export ANSIBLE_SSH_ARGS="-o UserKnownHostsFile=/dev/null" + export ANSIBLE_HOST_KEY_CHECKING=false +fi + +run_ansible "$@" +retcode=$? +exit $retcode