-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathansibleplaybook-json-stdout.go
58 lines (45 loc) · 1.3 KB
/
ansibleplaybook-json-stdout.go
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
package main
import (
"bytes"
"context"
"fmt"
"io"
"github.com/apenella/go-ansible/v2/pkg/execute"
"github.com/apenella/go-ansible/v2/pkg/execute/measure"
results "github.com/apenella/go-ansible/v2/pkg/execute/result/json"
"github.com/apenella/go-ansible/v2/pkg/execute/stdoutcallback"
"github.com/apenella/go-ansible/v2/pkg/playbook"
)
func main() {
var err error
var res *results.AnsiblePlaybookJSONResults
buff := new(bytes.Buffer)
ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Connection: "local",
Inventory: "127.0.0.1,",
}
playbooksList := []string{"site1.yml", "site2.yml", "site3.yml"}
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks(playbooksList...),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)
exec := measure.NewExecutorTimeMeasurement(
stdoutcallback.NewJSONStdoutCallbackExecute(
execute.NewDefaultExecute(
execute.WithCmd(playbookCmd),
execute.WithErrorEnrich(playbook.NewAnsiblePlaybookErrorEnrich()),
execute.WithWrite(io.Writer(buff)),
),
),
)
err = exec.Execute(context.TODO())
if err != nil {
fmt.Println(err.Error())
}
res, err = results.ParseJSONResultsStream(io.Reader(buff))
if err != nil {
panic(err)
}
fmt.Println(res.String())
fmt.Println("Duration: ", exec.Duration().String())
}