go-ansible icon indicating copy to clipboard operation
go-ansible copied to clipboard

disable verbose stdout

Open MohamedKHALILRouissi opened this issue 1 year ago • 4 comments

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-->

} `

MohamedKHALILRouissi avatar Jun 06 '24 02:06 MohamedKHALILRouissi

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!

apenella avatar Jun 07 '24 05:06 apenella

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 )

MohamedKHALILRouissi avatar Jun 13 '24 11:06 MohamedKHALILRouissi

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.

apenella avatar Jun 14 '24 05:06 apenella

@MohamedKHALILRouissi let me know if any further assistance is required, otherwise, I will close the issue.

apenella avatar Aug 16 '24 16:08 apenella

@MohamedKHALILRouissi I close the issue. If you have any questions, please, feel free open a new issue.

apenella avatar Sep 06 '24 23:09 apenella