-
Notifications
You must be signed in to change notification settings - Fork 4
/
ansible_log_parser.py
96 lines (89 loc) · 3.39 KB
/
ansible_log_parser.py
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
#!/usr/bin/env python
import re
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument(
'-l',
'--logfile',
required=True,
help='The log file that you want to parse'
)
parser.add_argument(
'-c',
'--criteria',
default='all',
help='Whether you want to see "all" output parsed (default) or only the "success", "failure", or "unreachable" output'
)
parser.add_argument('-o', '--output', required=False, help='Specify an output file to store the output')
args = parser.parse_args()
if args.criteria not in ['all','success','failure','unreachable']:
print("The --criteria or -c option must be one of: all, success, failure, or unreachable")
sys.exit(1)
def print_summary():
hit_recap = False
successes = []
failures = []
unreachables = []
with open(args.logfile, 'r', encoding="utf-8") as log:
for line in log:
if 'PLAY RECAP' in line and not hit_recap:
hit_recap = True
elif hit_recap:
#Tower 3.0.3 log compatible
if '[0m' in line:
next_line = line[6:]
hostname, tasks = next_line.split('[0m', 1)
#Tower 3.2.3 log compatible
else:
hostname, tasks = line.split(' : ',1)
if 'unreachable=1' in tasks:
unreachables.append(hostname)
elif re.search('failed=[1-9+]', tasks):
failures.append(hostname)
else:
successes.append(hostname)
else:
#This should theortically never run
pass
#+= syntax chosen for readability - could use join() with a list instead
output_file_string = ''
if 'all' in args.criteria:
output_file_string += "Successes: " + str(len(successes)) + "\n"
for success in successes:
output_file_string += success + "\n"
output_file_string += "\nFailures: " + str(len(failures)) + "\n"
for failure in failures:
output_file_string += failure + "\n"
output_file_string += "\nUnreachables: " + str(len(unreachables)) + "\n"
for unreachable in unreachables:
output_file_string += unreachable + "\n"
finalize(output_file_string)
all_hostnames = [successes, failures, unreachables]
return all_hostnames
elif 'success' in args.criteria:
output_file_string += "Successes: " + str(len(successes)) + "\n"
for success in successes:
output_file_string += success + "\n"
finalize(output_file_string)
return successes
elif 'failure' in args.criteria:
output_file_string += "Failures: " + str(len(failures)) + "\n"
for failure in failures:
output_file_string += failure + "\n"
finalize(output_file_string)
return failures
elif 'unreachable' in args.criteria:
output_file_string += "Unreachables: " + str(len(unreachables)) + "\n"
for unreachable in unreachables:
output_file_string += unreachable + "\n"
finalize(output_file_string)
return unreachables
else:
pass
def finalize(output_file_string):
print(output_file_string)
if args.output:
with open(args.output, 'w', encoding="utf-8") as output_file:
output_file.write(output_file_string)
result = print_summary()