-
Notifications
You must be signed in to change notification settings - Fork 2
/
ansible-fmtout
executable file
·97 lines (83 loc) · 2.76 KB
/
ansible-fmtout
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
#!/bin/bash
if [[ "$*" =~ --debug ]]; then
dbg() { echo "$@"; }
else
dbg() { : ; }
fi
while read -r -p "Please paste json-ish output from failed ansible with-items task here now..." CONTENT; do
if [[ "$CONTENT" =~ ^[[:space:]]*$ ]]; then
echo ""
dbg "== skipping empty line =="
continue
fi
#|\S.*m)
#|\S.*m)
dbg "~~~~~~~~~~~~~~~~~~~~"
dbg "$CONTENT"
dbg "~~~~~~~~~~~~~~~~~~~~"
esc_char=""
PLAINCONTENT=$(sed -r "s/${esc_char}\[[0-9;]+m//g" <<<"${CONTENT}")
dbg "== Obtaining changed-state and host =="
state=$(sed -nr "s/^([a-z]+): .* => .*/\\1/p" <<<"${PLAINCONTENT}")
thost=$(sed -nr "s/^[a-z]+: \[(.*)\] => .*/\\1/p" <<<"${PLAINCONTENT}")
dbg "== Trying to handles as item's input and output =="
inp=$(sed -nr "s/.*\(item=(.*)\) => \{.*/\\1/p" <<<"${PLAINCONTENT}")
out=$(sed -nr "s/.*\(item=.*\) => (\{.*)/\\1/p" <<<"${PLAINCONTENT}")
if [[ -z "$inp" && -z "$out" ]]; then
dbg "== Trying to handle as item's input only =="
inp=$(sed -nr "s/.*\(item=(.*)\)/\\1/p" <<<"${PLAINCONTENT}")
fi
if [[ -z "$inp" ]]; then
dbg "== Trying to handle input as regular task line =="
out=$(sed -nr "s/.*\[.*\] => (\{.*)/\\1/p" <<<"${PLAINCONTENT}")
fi
if [[ -z "$out" ]]; then
dbg "== Trying to handle input as raw json output =="
out="${PLAINCONTENT}"
fi
FINP=""
if [[ -n "$inp" ]]; then
dbg "~~~~~~~~~~~~ input is"
dbg "${inp}"
if [[ "${inp}" =~ ^\{ ]]; then
FINP=$(echo "import pprint; a=${inp}; pprint.pprint(a)" | python | sed -r 's/\"/"/g;s/\\n/\n/g')
else
FINP="${inp}"
fi
fi
dbg "~~~~~~~~~~~~ output is"
dbg "${out}"
FOUT=$(python -m json.tool <<<"${out}" |& sed -r 's/\\"/"/g;s/\\n/\n/g'; exit ${PIPESTATUS[0]})
fout_rc="$?"
dbg "fout_rc: ${fout_rc}"
[[ "${FOUT}" = "No JSON object could be decoded" ]] && fout_rc=1
# Print out results
if [[ ( -n "$FINP" || -n "$FOUT" ) && ( -n "$state" && -n "$thost" ) ]]; then
# if we have any formated output
# and also we have both taget host and state of task
# lets print 'nice' header
echo "== $state @$thost =="
fi
dbg -e "\n\n"
dbg "== INPUT =="
if [[ -n "$FINP" ]]; then
echo -ne "item object: \033[0;32m";
echo "$FINP"
echo -ne "\033[00m";
elif [[ -n "$inp" ]]; then
echo -ne "item: \033[0;32m";
echo "${inp}"
echo -ne "\033[00m";
fi
dbg -e "\n\n"
dbg "== OUTPUT =="
if [[ "$fout_rc" == "0" ]]; then
echo -ne "task state: \033[0;33m";
echo "$FOUT"
echo -ne "\033[00m";
else
dbg "== $FOUT =="
echo "${CONTENT}"
fi
dbg -e "\n\n"
done