ansible-infra/scripts/generate_ansible_config.sh
2023-11-08 00:00:00 +00:00

117 lines
3.5 KiB
Bash
Executable file

#!/usr/bin/env bash
set -o pipefail -o noclobber
function usage() {
cat <<EOF
DESCRIPTION
This command generates an ansible configuration file to use with this repository; it is tailored for the user calling ansible commands,
not to be used as a boilerplate for Molecule and \`ansible-test\` environments.
USAGE
\$> ./$(basename "$0") [option [option...]] [file_path]
ARGUMENTS
file_path file path where the file will be saved; defaults to '\$HOME/.ansible.cfg';
if neither this argument nor the environment variable \$ANSIBLE_CFG_PATH is provided, the
content will be printed on stdout.
OPTIONS
-h, --help show this output
-o, --overwrite allow overwriting of a pre-existing file
ENVIRONMENT
The following environment variables are supported for overrides:
ANSIBLE_CFG_PATH: path where the ansible config file should be stored (current: "${ANSIBLE_CFG_PATH:-stdout}" or argument)
TMPL_PROJECT_ROOT: path where this project is located; defaults to where '../' leads (current: "$TMPL_PROJECT_ROOT")
TMPL_INVENTORY_FILE: path where the main inventory file is located (current: "$TMPL_INVENTORY_FILE")
TMPL_CONNECTION_TIMEOUT: default connection timeout when connecting to hosts (current: $TMPL_CONNECTION_TIMEOUT)
TMPL_PLAYBOOK_DIR: path where the playbooks are located (current: "$TMPL_PLAYBOOK_DIR")
EOF
}
ANSIBLE_CFG_PATH=${ANSIBLE_CFG_PATH:=}
OVERWRITE_CFG=0
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Template variables
TMPL_PROJECT_ROOT=$(realpath "${TMPL_PROJECT_ROOT:-"${SCRIPT_DIR}/.."}")
TMPL_INVENTORY_FILE=$(realpath "${TMPL_INVENTORY_FILE:-"${TMPL_PROJECT_ROOT}/inventory/inventory.yml"}")
TMPL_CONNECTION_TIMEOUT=${TMPL_CONNECTION_TIMEOUT:-30}
TMPL_PLAYBOOK_DIR=$(realpath "${TMPL_PLAYBOOK_DIR:-"${TMPL_PROJECT_ROOT}/playbooks"}")
OPTS=ho
LONGOPTS=help,overwrite
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo -e "Getopt is not available, please install linux-utils or a similar package and ensure your bash is up-to-date."
exit 1
fi
! PARSED=$(getopt --options=${OPTS} --longoptions=${LONGOPTS} --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
exit 2
fi
eval set -- "${PARSED}"
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
-o|--overwrite)
OVERWRITE_CFG=1
shift
;;
--)
shift
break
;;
*)
echo "Unsupported option: ${1}"
exit 2
;;
esac
done
if [ -n "${1}" ]; then
ANSIBLE_CFG_PATH=$1
fi
if [ -e "${ANSIBLE_CFG_PATH}" ] && [ $OVERWRITE_CFG -eq 0 ]; then
printf "Configuration file already exists, not overwriting (file: '%s')\n" "${ANSIBLE_CFG_PATH}"
exit 1
fi
CFG_TEMPLATE=$(cat <<EOF
[defaults]
cow_selection = random
error_on_undefined_vars = True
home = ${TMPL_PROJECT_ROOT}
inventory = ${TMPL_INVENTORY_FILE}
local_tmp = /tmp
nocolor = 0
nocows = 0
playbook_dir = ${TMPL_PLAYBOOK_DIR}
remote_tmp = /tmp
[inventory]
enable_plugins = yaml
[privilege_escalation]
become_method = su
become_flags = "-l"
[persistent_connection]
connect_timeout = ${TMPL_CONNECTION_TIMEOUT}
[galaxy]
display_progress = True
EOF
)
if [ -z "${ANSIBLE_CFG_PATH}" ]; then
echo -e "$CFG_TEMPLATE"
else
echo -e "$CFG_TEMPLATE" > "${ANSIBLE_CFG_PATH}"
fi