apenella / go-ansible

Go-ansible is a Go package that enables the execution of ansible-playbook or ansible commands directly from Golang applications. It supports a wide range of options for each command, enabling smooth integration of Ansible functionality into your projects.
MIT License
807 stars 137 forks source link

disable verbose stdout #151

Open MohamedKHALILRouissi opened 4 weeks ago

MohamedKHALILRouissi commented 4 weeks ago

am not sure exactly how to disable to stdout any help please and thanks

` func cloud_server_configuration(server_ip string, os_type string, user string) {

ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
    Become:        true,
    User:          user,
    Connection:    "ssh",
    Inventory:     server_ip + ",",
    PrivateKey:    "/app/id_rsa",
    SSHCommonArgs: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
    Verbose:       false,
    VerboseV:      false,
    VerboseVV:     false,
    VerboseVVV:    false,
    VerboseVVVV:   false,
    Timeout:       60,
}

err := playbook.NewAnsiblePlaybookExecute("/app/playbooks/" + os_type + ".cloud_playbook.yaml").
    WithPlaybookOptions(ansiblePlaybookOptions).
    Execute(context.TODO())

if err != nil {
    fmt.Print("failed provisioning")
    // itona backend call to update server attribute <--TODO-->
    fmt.Println(err.Error())
}
fmt.Print("success provisioning")
// itona backend call to update server attribute <--TODO-->

} `

apenella commented 4 weeks ago

Hi @MohamedKHALILRouissi! Thank you for opening this issue!

You're currently utilizing the AnsiblePlaybookExecute, which is a ready-to-go executor and doesn't offer much room for customization.

To disable stdout, I suggest using the DefaultExecute and utilizing the WithWrite method to set the writer as io.Discard.

playbookCmd := playbook.NewAnsiblePlaybookCmd(
  playbook.WithPlaybooks("site.yml"),
  playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

exec := execute.NewDefaultExecute(
  execute.WithCmd(playbookCmd),
  execute.WithErrorEnrich(playbook.NewAnsiblePlaybookErrorEnrich()),
  execute.WithWrite(io.Discard),
)

exec.Execute(context.TODO())

I hope it can help you!

MohamedKHALILRouissi commented 3 weeks ago

is possible to catch each step output , let say that have i have single yaml playbook with 4 step , failed at step 3 create log for the failed creation ? ( without the stdout )

apenella commented 3 weeks ago

hi @MohamedKHALILRouissi! That won't be possible by configuring the writer, at least not straightforwardly, because the DefaultExecute's writer captures the entire command execution.

Did you explore the JSON stdout callback? It might help you customize how you control the output. However, one drawback of the JSON stdout callback is that you won't get the output until the execution is completed.

Here you have an example of how to enable the JSON stdout callback with go-ansible.