-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrapVMAuthent.sh
executable file
·144 lines (123 loc) · 3.27 KB
/
bootstrapVMAuthent.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
142
#!/usr/bin/env bash
dir=$(cd -P -- "$(dirname -- "$BASH_SOURCE[0]")" && pwd -P)
#
# load utilities
#
_utilities="${dir}/utils.sh"
if [ ! -r "${_utilities}" ]; then
echo "Failed to read file ${_utilities}"
exit 1
fi
. "${_utilities}"
#
# Install Ansible (need python and wget)
#
install_using_yum() {
sudo yum install -y ansible
}
install_using_apt() {
sudo apt install -y sshpass python3 python3-pip python3-setuptools
pip3 install ansible
}
install_sshpass_using_apt() {
sudo apt-get install -y sshpass
}
install_ansible() {
_info "Install Ansible"
if command -v yum >/dev/null 2>&1 ; then
install_using_yum
elif command -v apt-get >/dev/null 2>&1 ; then
install_using_apt
else
>&2 echo "only apt-get and yum install are coded"
return 1
fi
}
install_sshpass() {
_info "Install sshPass"
if command -v apt-get >/dev/null 2>&1 ; then
install_sshpass_using_apt
else
>&2 echo "only apt-get install is coded"
return 1
fi
}
usage() {
echo "Usage: $0 [-a <initial_address>] [-p <initial_port>] [-u <initial_user, root ?>] [-k <initial_ssh_key_path>] [-t <server target>]" 1>&2;
echo "Prerequisite file: ~/.personnalVault must containts the ansible_vault key" 1>&2;
exit 1;
}
_info "Init Environment and tools for the project"
#
# install Ansible
#
command -v ansible-playbook >/dev/null 2>&1 || install_ansible
command -v sshpass >/dev/null 2>&1 || install_sshpass
checkForError "Install Ansible failed"
#
# Default values
#
server_initial_address=''
server_initial_port=22
server_initial_root='root'
server_initial_key=""
server_target=""
#
# Check arguments
#
while getopts ":a:u:k:p:t:" option; do
case "${option}" in
a)
server_initial_address=${OPTARG}
server_target="initial_server"
;;
u)
server_initial_root=${OPTARG}
;;
k)
server_initial_key=${OPTARG}
;;
p)
server_initial_port=${OPTARG}
;;
t)
server_target=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${server_target}" ] && [ -z "${server_initial_address}" ] || ! [ -f ~/.personnalVault ]; then
usage
fi
temp_file_inventory=$(mktemp)
trap 'rm -f "${temp_file_inventory}"' EXIT
cat <<EOF > ${temp_file_inventory}
[initial]
initial_server
[initial:vars]
ansible_ssh_host=${server_initial_address}
ansible_ssh_port=${server_initial_port}
EOF
# run ansible playbook
_info "Launch ansible playbook"
if [ ! -z ${server_initial_address} ]; then
ANSIBLE_FORCE_COLOR=true \
ANSIBLE_HOST_KEY_CHECKING=false \
ANSIBLE_SSH_ARGS="${_ssh_options_light}" \
ANSIBLE_CONFIG="${dir}/ansible.cfg" \
ansible-playbook -i "${temp_file_inventory}" -i "${dir}/inventory" -l "${server_target}" --user "${server_initial_root}" \
--vault-id=user@~/.personnalVault \
"${dir}/bootstrapPlaybook.yml" --ask-pass
else
ANSIBLE_FORCE_COLOR=true \
ANSIBLE_HOST_KEY_CHECKING=false \
ANSIBLE_SSH_ARGS="${_ssh_options}" \
ANSIBLE_CONFIG="${dir}/ansible.cfg" \
ansible-playbook -i "${temp_file_inventory}" -i "${dir}/inventory" -l "${server_target}" --user "${server_initial_root}" \
--vault-id=user@~/.personnalVault \
"${dir}/bootstrapPlaybook.yml"
fi
checkForError "Setup failed"