-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_vault.sh
executable file
·71 lines (59 loc) · 1.93 KB
/
edit_vault.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
#! /usr/bin/env bash
set -o posix
set -o nounset
set -o pipefail
set -o errexit
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "${PROJECT_ROOT}/variables.env"
source "${PROJECT_ROOT}/scripts/logging.sh"
#
# Setup the Python virtualenv for Ansible
#
source "${PROJECT_ROOT}/scripts/virtualenv.sh"
setup_virtualenv
#
# Some pre-processing things
#
#
# Setup vault command parameters
#
CMD_PARAMS=""
if test -e "${ANSIBLE_PLAYBOOK_PATH}/.vault_pass"
then
CMD_PARAMS="${CMD_PARAMS} --vault-password-file "${ANSIBLE_PLAYBOOK_PATH}/.vault_pass""
fi
#
# Get the list of vault files from the config dir
# and provide the list to choose from to the user.
#
ANSIBLE_VAULT_FILE_LIST="/tmp/ansible-vault-file-list.txt"
echo "CREATE A NEW VAULT" > "${ANSIBLE_VAULT_FILE_LIST}"
grep --recursive --files-with-matches 'ANSIBLE_VAULT' "${ANSIBLE_CONFIG_PATH}" >> "${ANSIBLE_VAULT_FILE_LIST}"
echo "Please select which vault you want to edit:"
nl "${ANSIBLE_VAULT_FILE_LIST}"
count="$(wc --lines "${ANSIBLE_VAULT_FILE_LIST}" | cut --field 1 --delimiter ' ')"
selection=""
while true; do
read -p 'Select a vault file: ' selection
# If the user selection is an integer between one and $count...
if [ "$selection" -eq "$selection" ] && [ "$selection" -gt 0 ] && [ "$selection" -le "$count" ]; then
break
fi
done
if [ "$selection" -eq 1 ]; then
read -p "Where to store the new vault file? If relative path given, will be stored in ${ANSIBLE_CONFIG_PATH} " vault
if [[ $vault != /* ]]; then
vault="${ANSIBLE_CONFIG_PATH}/${vault}"
fi
info "Will create a new vault file at '${vault}'"
CMD_PARAMS="create $CMD_PARAMS"
else
vault="$(sed -n "${selection}p" ${ANSIBLE_VAULT_FILE_LIST})"
CMD_PARAMS="edit $CMD_PARAMS"
fi
info "Selected the vault '$vault'"
rm "${ANSIBLE_VAULT_FILE_LIST}"
#
# Let's go
#
ansible-vault $CMD_PARAMS "$vault"