-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybook.go
59 lines (47 loc) · 1.03 KB
/
playbook.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
59
package ansible
import (
"fmt"
"strings"
)
type playbook struct {
inventoryPath string
playbookPath string
extraVars map[string]string
}
func (p *playbook) Run() error {
return Exec(p.Command())
}
func (p *playbook) baseCmd() string {
return fmt.Sprintf("ansible-playbook -i %s %s", p.inventoryPath, p.playbookPath)
}
func Playbook() *playbook {
a := &playbook{}
a.extraVars = make(map[string]string)
return a
}
func (p *playbook) Path(path string) *playbook {
p.playbookPath = path
return p
}
func (p *playbook) Inventory(path string) *playbook {
p.inventoryPath = path
return p
}
func (p *playbook) ExtraVars(vars map[string]string) *playbook {
for k, v := range vars {
p.extraVars[k] = v
}
return p
}
func (p *playbook) extraVarsToString() string {
var params strings.Builder
if p.extraVars != nil {
for k, v := range p.extraVars {
fmt.Fprintf(¶ms, "-e %s=%s ", k, v)
}
}
return params.String()
}
func (p *playbook) Command() string {
return fmt.Sprintf("%s %s", p.baseCmd(), p.extraVarsToString())
}