forked from sgsullivan/run-ansible-playbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec-playbook
38 lines (28 loc) · 956 Bytes
/
exec-playbook
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
#!/bin/bash
set -xeuo pipefail
[[ -z ${IP:-} ]] && {
echo "Environment variable [IP] is not defined. Cannot continue."
exit 1
}
[[ -z ${PLAYBOOK:-} ]] && {
echo "Environment variable [PLAYBOOK] is not defined. Cannot continue."
exit 1
}
[[ -e ${PLAYBOOK} ]] || {
echo "Ansible playbook defined in environment variable [PLAYBOOK] has a value of [${PLAYBOOK}]; however it doesnt exist. Cannot continue."
exit 1
}
pkey_file=/root/.ssh/id_rsa
[[ -n ${PRIVATE_KEY_FILE:-} ]] && {
pkey_file=${PRIVATE_KEY_FILE}
}
[[ -e ${pkey_file} ]] || {
echo "No private ssh key has been found. By default, [/root/.ssh/id_rsa] is checked, you can override with the [PRIVATE_KEY_FILE] environment variable."
exit 1
}
echo $IP > hosts
gw=$(ip r|grep -Po 'default via \S+'|awk '{print $3}')
arping ${gw} -c1
ping ${gw} -c1
ansible all -i hosts -m ping
ansible-playbook -vvv --ssh-extra-args="-i ${pkey_file}" -F /root/.ssh/config -u root -i ${IP}, ${PLAYBOOK}