forked from k3s-io/k3s-ansible
-
Notifications
You must be signed in to change notification settings - Fork 1
/
k3sible
executable file
·104 lines (92 loc) · 2.79 KB
/
k3sible
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#
# This is a wrapper around ansible / ansible-playbook that uses the
# [Ansible Suitecase](https://github.com/epfl-si/ansible.suitecase).
#
# Usage ("ansible" mode):
# ./ansible/k3sible -m raw all -a 'date'
#
# Usage ("ansible-playbook" mode):
# ./ansible/k3sible
#
# If you are unfamiliar with Ansible, read up on it at
# - https://www.ansible.com/overview/how-ansible-works
# - https://github.com/jdauphant/awesome-ansible
#
cd "$(dirname "$0")"; cd "$(/bin/pwd)"
git_current_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
case "$git_current_branch" in
"") : ;;
*) playbook_flags="$playbook_flags -e git_current_branch=$git_current_branch" ;;
esac
warn () {
if [ -n "$1" ]; then
echo "$@" >&2
else
cat >&2
fi
}
fatal () {
warn "$@"
exit 1
}
platform_check () {
if ! test -f ansible-deps-cache/.versions 2>/dev/null; then
curl https://raw.githubusercontent.com/epfl-si/ansible.suitcase/master/install.sh | \
SUITCASE_DIR=$PWD/ansible-deps-cache \
SUITCASE_PIP_EXTRA="bcrypt passlib" \
SUITCASE_ANSIBLE_REQUIREMENTS=requirements.yml \
SUITCASE_ANSIBLE_VERSION=3.4 \
SUITCASE_NO_EYAML=1 \
SUITCASE_PYTHON_VERSIONS="3.8.5 3.8.2 3.8.9 3.8.10" \
bash -x
fi
export PATH="$PWD/ansible-deps-cache/bin:$PATH"
export ANSIBLE_ROLES_PATH="$PWD/ansible-deps-cache/roles"
export ANSIBLE_COLLECTIONS_PATHS="$PWD/ansible-deps-cache"
}
inventory_mode="dev"
inventories () {
case "$inventory_mode" in
dev) echo "-i inventory/switch/hosts.ini" ;;
reset) echo "-i inventory/switch/hosts.ini" ;;
*) echo "Invalid inventory_mode" ;;
esac
}
###########################################################################
mode=ansible-playbook
declare -a ansible_args
while [ "$#" -gt 0 ]; do
case "$1" in
-m) mode=ansible
ansible_args+=("-m")
shift ;;
--reset) inventory_mode="reset"
mode=reset
shift ;;
*)
ansible_args+=("$1")
shift ;;
esac
done
# https://github.com/ansible/ansible/issues/32499, https://bugs.python.org/issue35219
case "$(uname -s)" in
Darwin) export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES ;;
esac
set -e
platform_check
case "$mode" in
ansible-playbook)
ansible-playbook $playbook_flags $(inventories) "${ansible_args[@]}" \
-e "k3sible_cwd=$OLDPWD" \
site.yml
;;
reset)
ansible-playbook $playbook_flags $(inventories) "${ansible_args[@]}" \
-e "k3sible_cwd=$OLDPWD" \
reset.yml
;;
ansible)
ansible $(inventories) $ansible_flags "${ansible_args[@]}"
;;
esac