-
Notifications
You must be signed in to change notification settings - Fork 11
/
ansible-vault-rgrep
executable file
·41 lines (36 loc) · 1.16 KB
/
ansible-vault-rgrep
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
#!/bin/bash
help() {
echo 'usage: ansible-vault-rgrep [-H] directory [GREP_PARAMS]'
echo ' ansible-vault-rgrep --help'
echo
echo ' Recursively grep ansible vault files.'
echo
echo ' -H prefix each match with the filename'
echo
echo ' You should either set `DEFAULT_VAULT_PASSWORD_FILE`'
echo ' or set `vault_password_file` in your ansible'
echo ' config file, otherwise ansible-vault-rgrep will'
echo ' be hardly useful.'
echo
exit 1
}
[ "$1" == "--help" -o "$1" == "" ] && help
[ "$1" == "-H" ] && PREFIX_WITH_FILE_NAME=true && shift
dir="$1"; shift
find "$dir" -type f \
| while read file; do
# length of "$ANSIBLE_VAULT;" = 15
header=$( dd if="$file" bs=15 count=1 status=none )
if [ "$header" == '$ANSIBLE_VAULT;' ]; then
ansible-vault view "$file" \
| grep "$@" \
| if [ "$PREFIX_WITH_FILE_NAME" ]; then
# escape filename for sed
# https://stackoverflow.com/a/2705678/4640820
file_escaped=$( echo "$file" | sed -e 's/[]\/$*.^[]/\\&/g' )
sed "s/^/$file_escaped:/"
else
cat
fi
fi
done