forked from bpleines/ansible-log-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansibleLogParser.py
85 lines (78 loc) · 3.27 KB
/
ansibleLogParser.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
#!/usr/bin/env python
import re
import argparse
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")
exit(1)
def printSummary():
hitRecap = False
successes = []
failures = []
unreachables = []
with open(args.logfile, 'r') as log:
for line in log:
if 'PLAY RECAP' in line and not hitRecap:
hitRecap = True
elif hitRecap:
#Tower 3.0.3 log compatible
if '[0m' in line:
nextLine = line[6:]
hostname, tasks = nextLine.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
outputFileString = ''
if 'all' in args.criteria:
outputFileString += "Successes: " + str(len(successes)) + "\n"
for success in successes:
outputFileString += success + "\n"
outputFileString += "\nFailures: " + str(len(failures)) + "\n"
for failure in failures:
outputFileString += failure + "\n"
outputFileString += "\nUnreachables: " + str(len(unreachables)) + "\n"
for unreachable in unreachables:
outputFileString += unreachable + "\n"
finalize(outputFileString)
allHostnames = [successes, failures, unreachables]
return allHostnames
elif 'success' in args.criteria:
outputFileString += "Successes: " + str(len(successes)) + "\n"
for success in successes:
outputFileString += success + "\n"
finalize(outputFileString)
return successes
elif 'failure' in args.criteria:
outputFileString += "Failures: " + str(len(failures)) + "\n"
for failure in failures:
outputFileString += failure + "\n"
finalize(outputFileString)
return failures
elif 'unreachable' in args.criteria:
outputFileString += "Unreachables: " + str(len(unreachables)) + "\n"
for unreachable in unreachables:
outputFileString += failure + "\n"
finalize(outputFileString)
return unreachables
else:
pass
def finalize(outputFileString):
print(outputFileString)
if args.output:
with open(args.output, 'w') as outputFile:
outputFile.write(outputFileString)
result = printSummary()