-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathans_play
executable file
·65 lines (47 loc) · 1.94 KB
/
ans_play
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
[[ -z "${ANSIBLE_HOME}" ]] && { echo "ANSIBLE_HOME not defined!!" ; exit 1; }
[[ -d "${ANSIBLE_HOME}" ]] || { echo "ANSIBLE_HOME defined but not present!!" ; exit 1; }
pushd "${ANSIBLE_HOME}/playbooks" &> /dev/null || { echo "Could not cd to ANSIBLE_HOME/playbooks"; exit 1; }
yml_list="$(ls -1 *.yml)"
[[ -z "${1}" ]] && { echo "At least the name of playbook should be supplied (should be last term)"; exit 1; }
INVENTORY_JSON="${ANSIBLE_HOME}/inventory.json"
ansible-inventory --list --export > "${INVENTORY_JSON}"
ansible_inventory_hosts=$(cat "${INVENTORY_JSON}" | jq -nr 'inputs[] | .hosts | values[]' | sort -u)
ansible_inventory_groups=$(cat "${INVENTORY_JSON}" | jq -r '.|keys|values[]' | grep -v "_meta" | sort)
ARG_LIST=( "${@}" )
PLAY="${ARG_LIST[-1]}"
# search first in playbooks dir
IS_PLAY=$( find $(pwd -P) -name "${PLAY}" -o -name "${PLAY}.yml" )
[[ -n "${IS_PLAY}" ]] && PLAY="${IS_PLAY}"
# is playbook is custom check for it's existence
[[ -f "${PLAY}" ]] || { echo "Playbook ${PLAY} does not exists!"; exit 1; }
# this does not work with bash < 4.3
# unset ARG_LIST[-1]
unset 'ARG_LIST[${#ARG_LIST[@]}-1]'
set -- "${ARG_LIST[@]}"
shopt -s extglob
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-*)
POSITIONAL_ARGS+=("${1}") # save positional arg
shift # past argument
;;
*)
ARG1="${1}"
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
if [[ -n ${ARG1} ]]; then
IS_HOST=$(echo "${ansible_inventory_hosts}" | grep ${ARG1})
if [[ -n "${IS_HOST}" ]]; then
[[ "${ARG1}" != *.spacescience.ro && "${IS_HOST}" == *.spacescience.ro ]] && ARG1="${ARG1}.spacescience.ro"
TGT_SPEC="-e target=${ARG1}"
fi
[[ -z "${IS_HOST}" ]] && { IS_GROUP=$(echo "${ansible_inventory_groups}" | grep ${ARG1}); }
[[ -n "${IS_GROUP}" ]] && TGT_SPEC="-e target=${ARG1}"
fi
ansible-playbook ${TGT_SPEC} "${@}" "${PLAY}"
popd &> /dev/null