Skip to content

Commit 15d14c6

Browse files
Added Ansible Log Events CLI Flag (#5159)
* Added flag Signed-off-by: Venkat Ramaraju <[email protected]> * Changes Signed-off-by: Venkat Ramaraju <[email protected]> * more changes Signed-off-by: Venkat Ramaraju <[email protected]>
1 parent 05d58e2 commit 15d14c6

File tree

4 files changed

+80
-43
lines changed

4 files changed

+80
-43
lines changed

internal/ansible/events/log_events.go

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,50 +67,52 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,
6767
verbosity := GetVerbosity(u, e, ident)
6868

6969
// logger only the following for the 'Tasks' LogLevel
70-
t, ok := e.EventData["task"]
71-
if ok {
72-
setFactAction := e.EventData["task_action"] == eventapi.TaskActionSetFact
73-
debugAction := e.EventData["task_action"] == eventapi.TaskActionDebug
74-
75-
if verbosity > 0 {
76-
l.mux.Lock()
77-
fmt.Println(e.StdOut)
78-
l.mux.Unlock()
79-
return
80-
}
81-
if e.Event == eventapi.EventPlaybookOnTaskStart && !setFactAction && !debugAction {
82-
l.mux.Lock()
83-
logger.Info("[playbook task start]", "EventData.Name", e.EventData["name"])
84-
l.logAnsibleStdOut(e)
85-
l.mux.Unlock()
86-
return
87-
}
88-
if e.Event == eventapi.EventRunnerOnOk && debugAction {
89-
l.mux.Lock()
90-
logger.Info("[playbook debug]", "EventData.TaskArgs", e.EventData["task_args"])
91-
l.logAnsibleStdOut(e)
92-
l.mux.Unlock()
93-
return
94-
}
95-
if e.Event == eventapi.EventRunnerItemOnOk {
96-
l.mux.Lock()
97-
l.logAnsibleStdOut(e)
98-
l.mux.Unlock()
99-
return
100-
}
101-
if e.Event == eventapi.EventRunnerOnFailed {
102-
errKVs := []interface{}{
103-
"EventData.Task", t,
104-
"EventData.TaskArgs", e.EventData["task_args"],
70+
if l.LogLevel == Tasks {
71+
t, ok := e.EventData["task"]
72+
if ok {
73+
setFactAction := e.EventData["task_action"] == eventapi.TaskActionSetFact
74+
debugAction := e.EventData["task_action"] == eventapi.TaskActionDebug
75+
76+
if verbosity > 0 {
77+
l.mux.Lock()
78+
fmt.Println(e.StdOut)
79+
l.mux.Unlock()
80+
return
81+
}
82+
if e.Event == eventapi.EventPlaybookOnTaskStart && !setFactAction && !debugAction {
83+
l.mux.Lock()
84+
logger.Info("[playbook task start]", "EventData.Name", e.EventData["name"])
85+
l.logAnsibleStdOut(e)
86+
l.mux.Unlock()
87+
return
88+
}
89+
if e.Event == eventapi.EventRunnerOnOk && debugAction {
90+
l.mux.Lock()
91+
logger.Info("[playbook debug]", "EventData.TaskArgs", e.EventData["task_args"])
92+
l.logAnsibleStdOut(e)
93+
l.mux.Unlock()
94+
return
95+
}
96+
if e.Event == eventapi.EventRunnerItemOnOk {
97+
l.mux.Lock()
98+
l.logAnsibleStdOut(e)
99+
l.mux.Unlock()
100+
return
105101
}
106-
if taskPath, ok := e.EventData["task_path"]; ok {
107-
errKVs = append(errKVs, "EventData.FailedTaskPath", taskPath)
102+
if e.Event == eventapi.EventRunnerOnFailed {
103+
errKVs := []interface{}{
104+
"EventData.Task", t,
105+
"EventData.TaskArgs", e.EventData["task_args"],
106+
}
107+
if taskPath, ok := e.EventData["task_path"]; ok {
108+
errKVs = append(errKVs, "EventData.FailedTaskPath", taskPath)
109+
}
110+
l.mux.Lock()
111+
logger.Error(errors.New("[playbook task failed]"), "", errKVs...)
112+
l.logAnsibleStdOut(e)
113+
l.mux.Unlock()
114+
return
108115
}
109-
l.mux.Lock()
110-
logger.Error(errors.New("[playbook task failed]"), "", errKVs...)
111-
l.logAnsibleStdOut(e)
112-
l.mux.Unlock()
113-
return
114116
}
115117
}
116118

internal/ansible/flags/flag.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Flags struct {
3939
LeaderElectionNamespace string
4040
GracefulShutdownTimeout time.Duration
4141
AnsibleArgs string
42+
AnsibleLogEvents string
4243

4344
// Path to a controller-runtime componentconfig file.
4445
// If this is empty, use default values.
@@ -162,6 +163,12 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
162163
"The amount of time that will be spent waiting"+
163164
" for runners to gracefully exit.",
164165
)
166+
flagSet.StringVar(&f.AnsibleLogEvents,
167+
"ansible-log-events",
168+
"tasks",
169+
"Ansible log events. The log level for console logging."+
170+
" This flag can be set to either Nothing, Tasks, or Everything.",
171+
)
165172
}
166173

167174
// ToManagerOptions uses the flag set in f to configure options.

internal/cmd/ansible-operator/run/cmd.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
4040

4141
"github.com/operator-framework/operator-sdk/internal/ansible/controller"
42+
"github.com/operator-framework/operator-sdk/internal/ansible/events"
4243
"github.com/operator-framework/operator-sdk/internal/ansible/flags"
4344
"github.com/operator-framework/operator-sdk/internal/ansible/metrics"
4445
"github.com/operator-framework/operator-sdk/internal/ansible/proxy"
@@ -223,6 +224,7 @@ func run(cmd *cobra.Command, f *flags.Flags) {
223224
MaxConcurrentReconciles: w.MaxConcurrentReconciles,
224225
ReconcilePeriod: w.ReconcilePeriod,
225226
Selector: w.Selector,
227+
LoggingLevel: getAnsibleEventsToLog(f),
226228
})
227229
if ctr == nil {
228230
log.Error(fmt.Errorf("failed to add controller for GVK %v", w.GroupVersionKind.String()), "")
@@ -330,6 +332,20 @@ func getAnsibleDebugLog() bool {
330332
return val
331333
}
332334

335+
// getAnsibleDebugLog return the integer value of the log level set in the flag
336+
func getAnsibleEventsToLog(f *flags.Flags) events.LogLevel {
337+
if strings.ToLower(f.AnsibleLogEvents) == "everything" {
338+
return events.Everything
339+
} else if strings.ToLower(f.AnsibleLogEvents) == "nothing" {
340+
return events.Nothing
341+
} else {
342+
if strings.ToLower(f.AnsibleLogEvents) != "tasks" && f.AnsibleLogEvents != "" {
343+
log.Error(fmt.Errorf("--ansible-log-events flag value '%s' not recognized. Must be one of: Tasks, Everything, Nothing", f.AnsibleLogEvents), "unrecognized log level")
344+
}
345+
return events.Tasks // Tasks is the default
346+
}
347+
}
348+
333349
// setAnsibleEnvVars will set environment variables based on CLI flags
334350
func setAnsibleEnvVars(f *flags.Flags) error {
335351
if len(f.AnsibleRolesPath) > 0 {

website/content/en/docs/building-operators/ansible/reference/advanced_options.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,19 @@ ok: [localhost] => {
254254

255255
-------------------------------------------------------------------------------
256256
```
257-
[ansible-vault-doc]: https://docs.ansible.com/ansible/latest/user_guide/vault.html
258257
258+
## Using Ansible Log Events
259+
260+
Using the `--ansible-log-events` CLI flag, you can determine to what degree the Ansible task logs will be outputted. The flag can take any of the following values:
261+
262+
1. `Nothing` - No tasks or task-related logs will be outputted.
263+
2. `Tasks` - Only Ansible Tasks will be outputted.
264+
3. `Everything` - All info logs and all tasks will be outputted.
259265
266+
If you want more control over the logs that are outputted, consider using the [Zap Logger][Zap-Logger] and [verbosity annotations][verbosity-annotations] in tandem with the `--ansible-log-events` CLI flag.
267+
268+
269+
[ansible-vault-doc]: https://docs.ansible.com/ansible/latest/user_guide/vault.html
270+
[Zap-Logger]: https://github.com/operator-framework/operator-sdk/blob/master/website/content/en/docs/building-operators/golang/references/logging.md#default-zap-logger
271+
[verbosity-annotations]: https://sdk.operatorframework.io/docs/building-operators/ansible/reference/advanced_options/#ansible-verbosity
260272

0 commit comments

Comments
 (0)