generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
devshop-ansible-playbook.sh
executable file
·103 lines (88 loc) · 3.46 KB
/
devshop-ansible-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
#!/usr/bin/env bash
set -e
if [[ -z "$COMPOSER_RUNTIME_BIN_DIR" ]]; then
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
else
BIN_DIR="$COMPOSER_RUNTIME_BIN_DIR"
fi
PLATFORM_FOLDER_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
PROJECT_FOLDER_PATH=$(dirname $BIN_DIR)
ANSIBLE_PLAYBOOK=${ANSIBLE_PLAYBOOK:-"${PLATFORM_FOLDER_PATH}/playbook.yml"}
ANSIBLE_TAGS=${ANSIBLE_TAGS:-""}
ANSIBLE_SKIP_TAGS=${ANSIBLE_SKIP_TAGS:-""}
ANSIBLE_EXTRA_VARS=${ANSIBLE_EXTRA_VARS:-""}
ANSIBLE_PLAYBOOK_COMMAND_OPTIONS=${ANSIBLE_PLAYBOOK_COMMAND_OPTIONS:-"--inventory=/etc/ansible/hosts"}
ANSIBLE_VERBOSITY=${ANSIBLE_VERBOSITY:-"0"}
# If the including repo has an inventory.yml file at the root, load it.
if [[ -f "$PROJECT_FOLDER_PATH/inventory.yml" ]]; then
ANSIBLE_PLAYBOOK_COMMAND_OPTIONS="$ANSIBLE_PLAYBOOK_COMMAND_OPTIONS --inventory=$PROJECT_FOLDER_PATH/inventory.yml"
fi
# Build options string if ENV vars exist.
if [[ -n "${ANSIBLE_TAGS}" ]]; then
ANSIBLE_PLAYBOOK_COMMAND_OPTIONS="--tags $ANSIBLE_TAGS $ANSIBLE_PLAYBOOK_COMMAND_OPTIONS"
fi
if [[ -n "${ANSIBLE_SKIP_TAGS}" ]]; then
ANSIBLE_PLAYBOOK_COMMAND_OPTIONS="--skip-tags $ANSIBLE_SKIP_TAGS $ANSIBLE_PLAYBOOK_COMMAND_OPTIONS"
fi
if [[ -n "${ANSIBLE_EXTRA_VARS}" ]]; then
ANSIBLE_PLAYBOOK_COMMAND_OPTIONS="--extra-vars $ANSIBLE_EXTRA_VARS $ANSIBLE_PLAYBOOK_COMMAND_OPTIONS"
fi
ANSIBLE_PLAYBOOK_COMMAND_OPTIONS="$ANSIBLE_PLAYBOOK_COMMAND_OPTIONS --inventory=$PLATFORM_FOLDER_PATH/services"
ON_FAIL=${ON_FAIL:-"systemctl status --no-pager"}
# TODO: Goat Scripts
LINE="------------------------"
echo $LINE
echo "Welcome to the Platform."
echo "Current Dir: ${pwd}"
# Detect dependencies
if [[ ! `command -v ansible` ]]; then
echo "This script requires ansible. Install it and try again."
exit 1
fi
if [ -n "$DEBUG" ]; then
echo $LINE
echo "devshop-ansible-playbook.sh Environment Vars:"
echo
echo "PLATFORM_FOLDER_PATH: $PLATFORM_FOLDER_PATH"
echo "PROJECT_FOLDER_PATH: $PROJECT_FOLDER_PATH"
echo "PWD: $PWD"
echo "ANSIBLE_PLAYBOOK: $ANSIBLE_PLAYBOOK"
echo "ANSIBLE_TAGS: $ANSIBLE_TAGS"
echo "ANSIBLE_SKIP_TAGS: $ANSIBLE_SKIP_TAGS"
echo "ANSIBLE_EXTRA_VARS: $ANSIBLE_EXTRA_VARS"
echo "ANSIBLE_VERBOSITY: $ANSIBLE_VERBOSITY"
echo "ANSIBLE_PLAYBOOK_COMMAND_OPTIONS: $ANSIBLE_PLAYBOOK_COMMAND_OPTIONS"
echo "Additional Arguments: $*"
fi
ANSIBLE_PLAYBOOK_INVENTORY_COMMAND="ansible-playbook --list-hosts $ANSIBLE_PLAYBOOK \
$ANSIBLE_PLAYBOOK_COMMAND_OPTIONS \
$*"
ANSIBLE_PLAYBOOK_FULL_COMMAND="ansible-playbook $ANSIBLE_PLAYBOOK \
$ANSIBLE_PLAYBOOK_COMMAND_OPTIONS \
$*"
echo $LINE
# @TODO: detect missing roles using ansible instead. This is tricky due to paths & ansible config. We don't want this to run unless called.
## Detect missing roles and install.
#if [ ! -d ${PROJECT_FOLDER_PATH}/roles/contrib ]; then
# echo "No ansible roles found at ${PROJECT_FOLDER_PATH}/roles/contrib."
# echo "Running ./scripts/ansible-galaxy-install.sh..."
# cd ${PROJECT_FOLDER_PATH}
# ./scripts/ansible-galaxy-install.sh
# cd -
#fi
cd $PROJECT_FOLDER_PATH
echo "Changed directory to $PROJECT_FOLDER_PATH"
echo "Running Ansible Playbook --list-hosts Command: "
echo "> $ANSIBLE_PLAYBOOK_INVENTORY_COMMAND"
$ANSIBLE_PLAYBOOK_INVENTORY_COMMAND
echo $LINE
echo "Running Ansible Playbook Command: "
echo "> $ANSIBLE_PLAYBOOK_FULL_COMMAND"
# Do not exit on error so we can run additional commands.
set +e
time $ANSIBLE_PLAYBOOK_FULL_COMMAND || \
(
EXIT=$?
echo "Playbook Failed! (exit $EXIT)"
exit $EXIT
)