Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Improve log format of Ansible-based Operators #2321

Merged
merged 2 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

### Added

- Improve Ansible logs in the Operator container for Ansible-based Operators. ([#2321](https://github.com/operator-framework/operator-sdk/pull/2321))

### Changed

Expand Down
14 changes: 14 additions & 0 deletions pkg/ansible/events/log_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package events

import (
"errors"
"fmt"

"github.com/operator-framework/operator-sdk/pkg/ansible/runner/eventapi"

Expand Down Expand Up @@ -67,10 +68,12 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,

if e.Event == eventapi.EventPlaybookOnTaskStart && !setFactAction && !debugAction {
logger.Info("[playbook task]", "EventData.Name", e.EventData["name"])
l.logAnsibleStdOut(e)
return
}
if e.Event == eventapi.EventRunnerOnOk && debugAction {
logger.Info("[playbook debug]", "EventData.TaskArgs", e.EventData["task_args"])
l.logAnsibleStdOut(e)
return
}
if e.Event == eventapi.EventRunnerOnFailed {
Expand All @@ -82,6 +85,7 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,
errKVs = append(errKVs, "EventData.FailedTaskPath", taskPath)
}
logger.Error(errors.New("[playbook task failed]"), "", errKVs...)
l.logAnsibleStdOut(e)
return
}
}
Expand All @@ -92,6 +96,16 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,
}
}

// logAnsibleStdOut will print in the logs the Ansible Task Output formatted
func (l loggingEventHandler) logAnsibleStdOut(e eventapi.JobEvent) {
fmt.Printf("\n--------------------------- Ansible Task StdOut -------------------------------\n")
if e.Event != eventapi.EventPlaybookOnTaskStart {
fmt.Printf(fmt.Sprintf("\n TASK [%v] ******************************** \n", e.EventData["task"]))
}
fmt.Println(e.StdOut)
fmt.Printf("\n-------------------------------------------------------------------------------\n")
}

// NewLoggingEventHandler - Creates a Logging Event Handler to log events.
func NewLoggingEventHandler(l LogLevel) EventHandler {
return loggingEventHandler{
Expand Down