-
Notifications
You must be signed in to change notification settings - Fork 0
/
showansible
executable file
·135 lines (116 loc) · 3.46 KB
/
showansible
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
#!/bin/bash
# Shows various Ansible stuff
# Load GPG agent's soocket if it isn't there
[ -z "$SSH_AUTH_SOCK" ] && export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
# Defaults
PROJECTDIR=/opt/ansible/projects
INVENTORYDIR=inventory
FILESDIR=files
ANSIBLE_VAULT_PASSWORD_FILE=.ansible-vault
# Override any defaults that are set by the user
test -f $HOME/.runansible.conf && . $HOME/.runansible.conf
usage="Usage: $0 [-p project] [-i] [-f file] variable|file|host|[group]
-A host Show all variable values for host (requires ansible-variables)
-f file Which vars file (which vars file to use, e.g. database_servers.yml)
Only required for files that do not reside inside
<project>/inventory/group_vars
-p project Name of the project to retrieve data from
-i [group] Inventory mode, show groups instead of variables,
if no group is supplied, 'all' is used
-h This text
When the input variable is a filename (NOTE: do not put in full paths!) the
script will render the contents of the file. If multiple files with the same name
exist, the script will prompt which one to use.
NOTE: This is different then using -f, as that is meant to select which vars file to read!
"
FILE=''
MODE=var
while getopts "A:f:ihlp:" opt; do
case ${opt} in
A)
MODE=allvars
INPUT="$OPTARG"
;;
f)
FILE="$OPTARG"
;;
i)
MODE=inventory
;;
p)
PROJECT="$OPTARG"
;;
h)
echo -e "$usage"
exit 0
;;
esac
done
shift $[$OPTIND -1]
[[ $INPUT == '' ]] && INPUT=$1
[[ $INPUT == '' ]] && [[ $MODE == 'inventory' ]] && INPUT='all'
shift 1
[ -z "$PROJECT" ] || [ -z "$INPUT" ] && echo "$usage" && exit 1
cd $PROJECTDIR/$PROJECT
test -f $PROJECTDIR/$PROJECT/$ANSIBLE_VAULT_PASSWORD_FILE && export ANSIBLE_VAULT_PASSWORD_FILE
if test -f .bitwarden.gpg
then
# Export bitwarden envvars
$(cat .bitwarden.gpg | gpg -qd)
bw login --apikey
export BW_SESSION=$(bw unlock --passwordenv BW_PASSWORD --raw)
fi
[[ "$FILE" != '' ]] && FILE="-e @$(find $INVENTORYDIR -name $FILE | tail -1)"
mapfile -d $'\0' POSSIBLE_INPUTS < <(find -L $PROJECTDIR/$PROJECT/$FILESDIR -name $INPUT -print0)
if [ ${#POSSIBLE_INPUTS[@]} -ne 0 ]
then
MODE=file
if [ ${#POSSIBLE_INPUTS[@]} -ge 2 ]
then
echo -e "Multiple matching files found, please select a file:\n"
for i in $(seq ${#POSSIBLE_INPUTS[@]})
do
echo "$i) ${POSSIBLE_INPUTS[i-1]}"
done
echo ""
read -p "Select file: " INPUT_NUM
INPUT=${POSSIBLE_INPUTS[INPUT_NUM-1]}
else
INPUT=${POSSIBLE_INPUTS[0]}
fi
fi
case "$MODE" in
allvars)
if [ "$(which ansible-variables)" == '' ]
then
echo "ERROR: ansible-variables not installed\n$usage"
exit 1
fi
ansible-variables $INPUT
;;
file)
ansible -i /dev/null localhost -m debug -a msg="{{ lookup('file', \'$INPUT\') }}" | cut -d\> -f2 | jq -r .msg
;;
var)
if [ "$FILE" == '' ]
then
TMP_INVENTORY=/tmp/.$$.inventory
echo localhost > $TMP_INVENTORY
for GROUP in $(ls $PROJECTDIR/$PROJECT/inventory/group_vars | cut -d. -f1)
do
echo "[${GROUP}]" >> $TMP_INVENTORY
echo "localhost" >> $TMP_INVENTORY
done
else
TMP_INVENTORY=/dev/null
fi
ansible -i $TMP_INVENTORY localhost -m debug -a var=$INPUT $FILE
if [ "$FILE" == '' ]
then
rm $TMP_INVENTORY
fi
;;
inventory)
ansible-inventory --graph $INPUT
;;
esac