Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Add live logs to the ansible execution
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Dec 1, 2021
1 parent f0f0d31 commit 4c34f68
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions runner/ansiblerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package runner

import (
"bufio"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -177,9 +178,8 @@ func (a *AnsibleRunner) RunPlaybook() error {
cmd.Env = append(cmd.Env, newEnv)
}

output, err := cmd.CombinedOutput()

log.Debugf("Ansible output:\n%s:", output)
logCommand(cmd)
err := cmd.Run()

if err != nil {
log.Errorf("An error occurred while running ansible: %s", err)
Expand All @@ -190,3 +190,26 @@ func (a *AnsibleRunner) RunPlaybook() error {

return nil
}

func logCommand(cmd *exec.Cmd) {
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
return
}
go func() {
in := bufio.NewScanner(stdout)
for in.Scan() {
log.Infof(in.Text())
}
}()
go func() {
in := bufio.NewScanner(stderr)
for in.Scan() {
log.Debugf(in.Text())
}
}()
}

0 comments on commit 4c34f68

Please sign in to comment.