Skip to content

Commit 5c53f65

Browse files
John KimShawn Hurley
authored andcommitted
make ansible task log outputs more readable (#545)
1 parent 2e23ca2 commit 5c53f65

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

pkg/ansible/events/log_events.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ const (
3232

3333
// Nothing - this will log nothing.
3434
Nothing
35+
36+
// Ansible Events
37+
EventPlaybookOnTaskStart = "playbook_on_task_start"
38+
EventRunnerOnOk = "runner_on_ok"
39+
EventRunnerOnFailed = "runner_on_failed"
40+
41+
// Ansible Task Actions
42+
TaskActionSetFact = "set_fact"
43+
TaskActionDebug = "debug"
3544
)
3645

3746
// EventHandler - knows how to handle job events.
@@ -51,17 +60,32 @@ func (l loggingEventHandler) Handle(u *unstructured.Unstructured, e eventapi.Job
5160
"gvk": u.GroupVersionKind().String(),
5261
"event_type": e.Event,
5362
})
63+
if l.LogLevel == Nothing {
64+
return
65+
}
66+
// log only the following for the 'Tasks' LogLevel
5467
t, ok := e.EventData["task"]
5568
if ok {
56-
log = log.WithField("task", t)
69+
setFactAction := e.EventData["task_action"] == TaskActionSetFact
70+
debugAction := e.EventData["task_action"] == TaskActionDebug
71+
72+
if e.Event == EventPlaybookOnTaskStart && !setFactAction && !debugAction {
73+
log.Infof("[playbook task]: %s", e.EventData["name"])
74+
return
75+
}
76+
if e.Event == EventRunnerOnOk && debugAction {
77+
log.Infof("[playbook debug]: %v", e.EventData["task_args"])
78+
return
79+
}
80+
if e.Event == EventRunnerOnFailed {
81+
log.Errorf("[failed]: [playbook task] '%s' failed with task_args - %v",
82+
t, e.EventData["task_args"])
83+
return
84+
}
5785
}
58-
switch l.LogLevel {
59-
case Everything:
86+
// log everything else for the 'Everything' LogLevel
87+
if l.LogLevel == Everything {
6088
log.Infof("event: %#v", e.EventData)
61-
case Tasks:
62-
if ok {
63-
log.Infof("event: %#v", e.EventData)
64-
}
6589
}
6690
}
6791

0 commit comments

Comments
 (0)