Skip to content

Commit

Permalink
adapt examples to use executor as main component to execute ansible c…
Browse files Browse the repository at this point in the history
…ommands
  • Loading branch information
apenella committed Nov 17, 2023
1 parent 439be27 commit c884467
Show file tree
Hide file tree
Showing 19 changed files with 192 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"

"github.com/apenella/go-ansible/pkg/adhoc"
"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/execute/stdoutcallback"
"github.com/apenella/go-ansible/pkg/options"
)

Expand All @@ -24,12 +26,17 @@ func main() {
Pattern: "all",
Options: ansibleAdhocOptions,
ConnectionOptions: ansibleConnectionOptions,
StdoutCallback: "oneline",
}

fmt.Println("Command: ", adhoc.String())

err := adhoc.Run(context.TODO())
onelineExecute := stdoutcallback.NewOnelineStdoutCallbackExecute(
execute.NewDefaultExecute(
execute.WithCmd(adhoc),
),
)

err := onelineExecute.Execute(context.TODO())
if err != nil {
panic(err)
}
Expand Down
8 changes: 6 additions & 2 deletions examples/ansibleadhoc-simple/ansibleadhoc-simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/apenella/go-ansible/pkg/adhoc"
"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/options"
)

Expand Down Expand Up @@ -33,12 +34,15 @@ func main() {
Pattern: "all",
Options: ansibleAdhocOptions,
ConnectionOptions: ansibleConnectionOptions,
//StdoutCallback: "oneline",
}

log.Println("Command: ", adhoc)

err := adhoc.Run(context.TODO())
exec := execute.NewDefaultExecute(
execute.WithCmd(adhoc),
)

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
Expand Down
20 changes: 13 additions & 7 deletions examples/ansibleplaybook-become/ansibleplaybook-become.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ func main() {
ConnectionOptions: ansiblePlaybookConnectionOptions,
PrivilegeEscalationOptions: ansiblePlaybookPrivilegeEscalationOptions,
Options: ansiblePlaybookOptions,
Exec: execute.NewDefaultExecute(
execute.WithEnvVar("ANSIBLE_FORCE_COLOR", "true"),
execute.WithTransformers(
transformer.Prepend("Go-ansible example with become"),
),
),
}

err := playbook.Run(context.TODO())
exec := execute.NewDefaultExecute(
execute.WithCmd(playbook),
execute.WithTransformers(
transformer.Prepend("Go-ansible example with become"),
),
execute.WithEnvVars(
map[string]string{
"ANSIBLE_FORCE_COLOR": "true",
},
),
)

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
Expand Down
25 changes: 14 additions & 11 deletions examples/ansibleplaybook-cobra-cmd/ansibleplaybook-cobra-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/execute/configuration"
"github.com/apenella/go-ansible/pkg/execute/result/transformer"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
Expand All @@ -35,12 +36,12 @@ func init() {
}

var rootCmd = &cobra.Command{
Use: "cobra-cmd-ansibleplaybook",
Short: "cobra-cmd-ansibleplaybook",
Long: `cobra-cmd-ansibleplaybook is an example which show how to use go-ansible library from cobra cli
Use: "ansibleplaybook-cobra-cmd",
Short: "ansibleplaybook-cobra-cmd",
Long: `ansibleplaybook-cobra-cmd is an example which show how to use go-ansible library from cobra cli
Run the example:
go run cobra-cmd-ansibleplaybook.go -L -i 127.0.0.1, -p site.yml -e example="hello go-ansible!"
go run ansibleplaybook-cobra-cmd.go -L -i 127.0.0.1, -p site.yml -e example="hello go-ansible!"
`,
RunE: commandHandler,
}
Expand Down Expand Up @@ -77,18 +78,20 @@ func commandHandler(cmd *cobra.Command, args []string) error {
Playbooks: playbookFiles,
ConnectionOptions: ansiblePlaybookConnectionOptions,
Options: ansiblePlaybookOptions,
Exec: execute.NewDefaultExecute(
}

exec := configuration.NewExecutorWithAnsibleConfigurationSettings(
execute.NewDefaultExecute(
execute.WithCmd(playbook),
execute.WithTransformers(
transformer.Prepend("cobra-cmd-ansibleplaybook example"),
transformer.Prepend("Go-ansible example with become"),
),
),
}

options.AnsibleForceColor()
).WithAnsibleForceColor()

err = playbook.Run(context.TODO())
err = exec.Execute(context.TODO())
if err != nil {
return err
panic(err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/signal"

"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/execute/configuration"
"github.com/apenella/go-ansible/pkg/execute/result/transformer"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
Expand Down Expand Up @@ -38,15 +39,18 @@ func main() {
Playbooks: []string{"site.yml"},
ConnectionOptions: ansiblePlaybookConnectionOptions,
Options: ansiblePlaybookOptions,
Exec: execute.NewDefaultExecute(
execute.WithEnvVar("ANSIBLE_FORCE_COLOR", "true"),
}

exec := configuration.NewExecutorWithAnsibleConfigurationSettings(
execute.NewDefaultExecute(
execute.WithCmd(playbook),
execute.WithTransformers(
outputColored(),
transformer.Prepend("Go-ansible example"),
transformer.LogFormat(transformer.DefaultLogFormatLayout, transformer.Now),
),
),
}
).WithAnsibleForceColor()

signal.Notify(signalChan, os.Interrupt)
defer func() {
Expand All @@ -62,10 +66,9 @@ func main() {
}
}()

err := playbook.Run(ctx)
err := exec.Execute(context.TODO())
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
panic(err)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"

"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
)
Expand All @@ -28,7 +29,11 @@ func main() {
Options: ansiblePlaybookOptions,
}

err := playbook.Run(context.TODO())
exec := execute.NewDefaultExecute(
execute.WithCmd(playbook),
)

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
Expand Down
24 changes: 13 additions & 11 deletions examples/ansibleplaybook-json-stdout/ansibleplaybook-json-stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/execute/measure"
results "github.com/apenella/go-ansible/pkg/execute/result/json"
"github.com/apenella/go-ansible/pkg/execute/stdoutcallback"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
"github.com/apenella/go-ansible/pkg/stdoutcallback/results"
)

func main() {
Expand All @@ -28,22 +29,23 @@ func main() {
Inventory: "127.0.0.1,",
}

executorTimeMeasurement := measure.NewExecutorTimeMeasurement(
execute.NewDefaultExecute(
execute.WithWrite(io.Writer(buff)),
),
)

playbooksList := []string{"site1.yml", "site2.yml", "site3.yml"}
playbook := &playbook.AnsiblePlaybookCmd{
Playbooks: playbooksList,
Exec: executorTimeMeasurement,
ConnectionOptions: ansiblePlaybookConnectionOptions,
Options: ansiblePlaybookOptions,
StdoutCallback: "json",
}

err = playbook.Run(context.TODO())
exec := measure.NewExecutorTimeMeasurement(
stdoutcallback.NewJSONStdoutCallbackExecute(
execute.NewDefaultExecute(
execute.WithCmd(playbook),
execute.WithWrite(io.Writer(buff)),
),
),
)

err = exec.Execute(context.TODO())
if err != nil {
fmt.Println(err.Error())
}
Expand All @@ -54,6 +56,6 @@ func main() {
}

fmt.Println(res.String())
fmt.Println("Duration: ", executorTimeMeasurement.Duration())
fmt.Println("Duration: ", exec.Duration().String())

}
33 changes: 11 additions & 22 deletions examples/ansibleplaybook-myexecutor/ansibleplaybook-myexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,20 @@ import (

type MyExecutor struct {
Prefix string
Cmd execute.Commander
}

func (e *MyExecutor) Options(options ...execute.ExecuteOptions) {
// apply all options to the executor
for _, opt := range options {
opt(e)
func NewMyExecutor(cmd execute.Commander) *MyExecutor {
return &MyExecutor{
Cmd: cmd,
}
}

func WithPrefix(prefix string) execute.ExecuteOptions {
return func(e execute.Executor) {
e.(*MyExecutor).Prefix = prefix
}
func (e *MyExecutor) WithPrefix(prefix string) {
e.Prefix = prefix
}

func (e *MyExecutor) Execute(ctx context.Context, command []string, options ...execute.ExecuteOptions) error {

// apply all options to the executor
for _, opt := range options {
opt(e)
}

func (e *MyExecutor) Execute(ctx context.Context) error {
fmt.Println(fmt.Sprintf("%s %s\n", e.Prefix, "I am MyExecutor and I am doing nothing"))

return nil
Expand All @@ -48,19 +40,16 @@ func main() {
Inventory: "127.0.0.1,",
}

exe := &MyExecutor{}
exe.Options(
WithPrefix("[Go ansible example]"),
)

playbook := &playbook.AnsiblePlaybookCmd{
Playbooks: []string{"site.yml"},
ConnectionOptions: ansiblePlaybookConnectionOptions,
Options: ansiblePlaybookOptions,
Exec: exe,
}

err := playbook.Run(context.TODO())
exec := NewMyExecutor(playbook)
exec.WithPrefix("[Go ansible example]")

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/signal"

"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/execute/configuration"
"github.com/apenella/go-ansible/pkg/execute/result/transformer"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
Expand All @@ -30,12 +31,16 @@ func main() {
Playbooks: []string{"site.yml"},
ConnectionOptions: ansiblePlaybookConnectionOptions,
Options: ansiblePlaybookOptions,
Exec: execute.NewDefaultExecute(
}

exec := configuration.NewExecutorWithAnsibleConfigurationSettings(
execute.NewDefaultExecute(
execute.WithCmd(playbook),
execute.WithTransformers(
transformer.Prepend("Go-ansible example"),
transformer.Prepend("[ansibleplaybook-signals-and-cancellation]"),
),
),
}
).WithAnsibleForceColor()

signal.Notify(signalChan, os.Interrupt)
defer func() {
Expand All @@ -51,7 +56,7 @@ func main() {
}
}()

err := playbook.Run(ctx)
err := exec.Execute(ctx)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"

"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
)
Expand Down Expand Up @@ -46,7 +47,11 @@ func main() {
Options: ansiblePlaybookOptions,
}

err = playbook.Run(context.TODO())
exec := execute.NewDefaultExecute(
execute.WithCmd(playbook),
)

err = exec.Execute(context.TODO())
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"

"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
)
Expand All @@ -23,7 +24,11 @@ func main() {
Options: ansiblePlaybookOptions,
}

err := playbook.Run(context.TODO())
exec := execute.NewDefaultExecute(
execute.WithCmd(playbook),
)

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit c884467

Please sign in to comment.