This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
run-playbook.sh
executable file
·141 lines (117 loc) · 3.97 KB
/
run-playbook.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
bin=ansible-playbook
function usage() {
cat <<EOF
Usage: $(basename "$0") playbook[.yml] inventory [--flag ...] [var=value ...]
EOF
}
function help() {
usage
cat <<EOF
playbook playbook file in deploy/ansible/, the .yml extension is optional.
inventory inventory host file in deploy/ansible/inventory/.
--flag any valid $bin flags, Eg. --help for help, -vvvv for
connection debugging, --user=REMOTE_USER to specify the remote
user, --ask-become-pass to prompt for a remote password, etc.
var=value any number of ansible extra vars in the format var=value.
Notes:
* Flags and vars must be specified after both playbook and inventory.
* All arguments specified after playbook and inventory not beginning with -
or -- will be treated as extra vars.
* If a non-vagrant inventory host is specified, unless the ubuntu user is
specified, the --ask-become-pass flag will be automatically added to the
command.
Examples:
$(basename "$0") provision vagrant
$(basename "$0") deploy production --user=ubuntu commit=master force=true
EOF
}
function error() {
[[ "$1" ]] && echo "ERROR: $@"
errors=1
}
function exit_on_errors() {
[[ "$errors" ]] && usage && exit 1
}
# Was one of the specified args passed to this script?
script_args=("$@")
function has_arg() {
local arg script_arg
for script_arg in "${script_args[@]}"; do
for arg in "$@"; do
[[ "$script_arg" == "$arg" || "$script_arg" =~ ^$arg= ]] && return 0
done
done
return 1
}
# Does the user need help?
[[ ! "$1" ]] && help && exit
if has_arg -h --help; then
help
echo -e "\n=== Output from $bin --help ===\n"
$bin --help
exit
fi
# Initial error checking.
errors=
[[ "$(type -P $bin)" ]] || error "$bin must be installed and in the PATH."
[[ "$2" ]] || error "both inventory and playbook must be specified."
exit_on_errors
# Parse playbook and inventory from command-line arguments.
playbook="${1#./}"; shift
inventory="${1#./}"; shift
# script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
script_dir="$(dirname "${BASH_SOURCE[0]}")"
script_dir="${script_dir#./}"
ansible_dir="$script_dir/ansible"
inventory_dir="$ansible_dir/inventory"
# Try to find the playbook file.
playbook_files=(
"$playbook" # Path absolute or relative to the project root
"$ansible_dir/$playbook" # Playbook file name, no path
"$ansible_dir/$playbook.yml" # Playbook file name, no path, no extension
)
playbook_file=; for f in "${playbook_files[@]}"; do
[[ -f "$f" ]] && playbook_file="$f" && break
done
# Try to find the inventory file.
inventory_files=(
"$inventory" # Path absolute or relative to the project root
"$inventory_dir/$inventory" # Inventory file name, no path
)
inventory_file=; for f in "${inventory_files[@]}"; do
[[ -f "$f" ]] && inventory_file="$f" && break
done
# Handle errors.
errors=
[[ -f "$playbook_file" ]] || error "playbook file $playbook not found."
[[ -f "$inventory_file" ]] || error "inventory file $inventory not found."
exit_on_errors
# Build the command.
command=(
$bin
"$playbook_file"
"--inventory=$inventory_file"
)
# Ask for sudo password for non-vagrant inventory hosts, unless --user=ubuntu
# was specified (the default "ubuntu" user in AWS instances has passwordless
# sudo, but any users created via ansible should require passwords for sudo).
if [[ "${inventory##*/}" != "vagrant" ]]; then
if ! has_arg -K --ask-sudo-pass --ask-become-pass && ! has_arg --user=ubuntu; then
command=("${command[@]}" --ask-become-pass)
fi
fi
# Pass args starting with - (hypen) through to ansible-playbook as-is,
# otherwise precede them with --extra-vars.
for arg in "$@"; do
[[ "$arg" =~ ^- ]] || command=("${command[@]}" "--extra-vars")
command=("${command[@]}" "$arg")
done
# Output some (helpful) text before running the command.
cat <<EOF
Running the following command:
${command[@]}
If prompted for a password, enter the remote account password.
EOF
# Run the command!
"${command[@]}"