Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create galaxy commands #143

Merged
merged 30 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ff74d48
docs: upgrade-guide. move overview to the top of the document
apenella Mar 1, 2024
eba64fd
quote scp-extra-args, sftp-extra-args, ssh-common-args and ssh-extra-…
apenella Mar 13, 2024
5cca698
add NewAnsiblePlaybookCmd
apenella Mar 13, 2024
f7294b3
create example ansibleplaybook-ssh
apenella Mar 13, 2024
7197039
do not use go-shellquote package
apenella Mar 14, 2024
1b982a9
add test to NewAnsiblePlaybookCmd
apenella Mar 14, 2024
67cacb9
Create NewAnsibleAdhocCmd method
apenella Mar 14, 2024
86566f8
Create NewAnsibleInventoryCmd method
apenella Mar 14, 2024
5a5588f
fix typos
apenella Mar 15, 2024
ea7dc7e
use constructor method to create command structs in the examples
apenella Mar 15, 2024
61e116b
Include in readme file and upgrade guide references to command struct…
apenella Mar 15, 2024
7d46bf8
In ansibleplaybook-ssh's Docker compose file set init to true
apenella Mar 20, 2024
5490b27
Include an example with extravars
apenella Mar 20, 2024
b4e9456
in ansibleplaybook-extravars example use a remote host
apenella Mar 21, 2024
3c7c882
include considerations section in to the Readme
apenella Mar 22, 2024
a8ce3f7
improve ansibleplaybook-extravars example
apenella Mar 22, 2024
d927a62
Create package to install Ansible roles through ansible-galaxy
apenella Apr 2, 2024
40388c0
fix AnsibleGalaxyRoleInstallCmd tests
apenella Apr 3, 2024
2eb3380
include documentation for the ansible galaxy role install feature
apenella Apr 3, 2024
2518cfc
rename ApiKeyFlag to APIKeyFlag in ansibleGalaxyRoleInstall
apenella Apr 3, 2024
f95d06c
rename errContext to include detailed references
apenella Apr 3, 2024
fc63be6
use requirements file in the workflowexecute-ansibleplaybook-with-gal…
apenella Apr 3, 2024
73814cb
Create package to install Ansible collections through ansible-galaxy
apenella Apr 3, 2024
448b5fd
define constants in the galaxy role pacakge
apenella Apr 3, 2024
ca95a93
define constants in the galaxy
apenella Apr 3, 2024
b53da74
Add documentation about ansible galaxy collection package
apenella Apr 3, 2024
e2e881f
Add ansible galaxy collection package into release notes
apenella Apr 3, 2024
d0a35fe
remove commented code
apenella Apr 3, 2024
e05fa62
docs: corrections
apenella Apr 3, 2024
3cc4355
docs: prepare release v2.0.0-rc.3
apenella Apr 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 49 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ A _command generator_ or a _commander_ is responsible for generating the command

### Results Handler

A _results handler_ or an _results outputer_ is responsible for managing the output of the command execution. The library includes two output mechanisms: the [DefaultResults](#defaultexecute-struct) and the [JSONStdoutCallbackResults](#jsonstdoutcallbackresults-struct) structs.
A _results handler_ or a _results outputer_ is responsible for managing the output of the command execution. The library includes two output mechanisms: the [DefaultResults](#defaultexecute-struct) and the [JSONStdoutCallbackResults](#jsonstdoutcallbackresults-struct) structs.

## Getting Started

Expand All @@ -122,7 +122,7 @@ This section will guide you through the step-by-step process of using the _go-an

Before proceeding, ensure you have installed the latest version of the _go-ansible_ library. If not, please refer to the [Installation section](#install) for instructions.

To create an application that launches the `ansible-playbook` command you need to create an [AnsiblePlaybookCmd](#ansibleplaybookcmd-struct) struct. This struct generates the _Ansible_ command to be run. Then, you need to execute the command usingng an [executor](#executor). In that guided example, you will use the [DefaultExecute](#defaultexecute-struct) executor, an _executor_ provided by the _go-ansible_ library.
To create an application that launches the `ansible-playbook` command you need to create an [AnsiblePlaybookCmd](#ansibleplaybookcmd-struct) struct. This struct generates the _Ansible_ command to be run. Then, you need to execute the command using an [executor](#executor)](#executor). In that guided example, you will use the [DefaultExecute](#defaultexecute-struct) executor, an _executor_ provided by the _go-ansible_ library.

### Create the _AnsiblePlaybookCmd_ struct

Expand All @@ -141,10 +141,11 @@ ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Finally, create the [AnsiblePlaybookCmd](#ansibleplaybookcmd-struct) struct that generates the command to execute the playbook `site.yml` using the `ansible-playbook` command:

```go
playbookCmd := &playbook.AnsiblePlaybookCmd{
Playbook: "site.yml",
PlaybookOptions: ansiblePlaybookOptions,
}
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml", "site2.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

```

Once the `AnsiblePlaybookCmd` is defined, provide the command to an [executor](#executor) to run the command.
Expand Down Expand Up @@ -206,6 +207,12 @@ The `github.com/apenella/go-ansible/v2/pkg/adhoc` package facilitates the genera

The `AnsibleAdhocCmd` struct enables the generation of _Ansible ad-hoc_ commands. It implements the [Commander](#commander-interface) interface, so its method `Command` returns an array of strings that represents the command to be executed. An executor can use it to create the command to be executed.

The package provides the `NewAnsibleAdhocCmd` function to create a new instance of the `AnsibleAdhocCmd` struct. The function accepts a list of options to customize the ad-hoc command. The following functions are available:

- `WithAdhocOptions(options *AnsibleAdhocOptions) AdhocOptionsFunc`: Set the ad-hoc options for the command.
- `WithBinary(binary string) AdhocOptionsFunc`: Set the binary for the ad-hoc command.
- `WithPattern(pattern string) AdhocOptionsFunc`: Set the pattern for the ad-hoc command.

The following code snippet demonstrates how to use the `AnsibleAdhocCmd` struct to generate an ad-hoc command:

```go
Expand All @@ -218,10 +225,10 @@ ansibleAdhocOptions := &adhoc.AnsibleAdhocOptions{
}
}

adhocCmd := &adhoc.AnsibleAdhocCmd{
Pattern: "all",
Options: ansibleAdhocOptions,
}
adhocCmd := adhoc.NewAnsibleAdhocCmd(
adhoc.WithPattern("all"),
adhoc.WithAdhocOptions(ansibleAdhocOptions),
)

// Generate the command to be executed
cmd, err := adhocCmd.Command()
Expand Down Expand Up @@ -320,10 +327,10 @@ The snippet below shows how to customize the `DefaultExecute` executor using the

```go
// PlaybookCmd is the Commander responsible for generating the command to execute
playbookCmd := &playbook.AnsiblePlaybookCmd{
Playbook: "site.yml",
PlaygookOptions: ansiblePlaybookOptions,
}
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

// MyExecutabler is an hypothetical implementation of the Executabler interface
executabler := &myExecutabler{}
Expand Down Expand Up @@ -368,10 +375,10 @@ The next code snippet demonstrates how to execute the `ansible-playbook` command

```go
// Define the command to execute
playbookCmd := &ansibler.AnsiblePlaybookCmd{
Playbook: []string{"site.yml"},
PlaybookOptions: ansiblePlaybookOptions,
}
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

// Define an instance for the new executor and set the options
exec := &MyExecutor{
Expand Down Expand Up @@ -427,10 +434,10 @@ ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Inventory: "127.0.0.1,",
}

playbookCmd := &playbook.AnsiblePlaybookCmd{
Playbooks: []string{"site.yml"},
PlaybookOptions: ansiblePlaybookOptions,
}
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

exec := configuration.NewAnsibleWithConfigurationSettingsExecute(
execute.NewDefaultExecute(
Expand Down Expand Up @@ -461,10 +468,10 @@ ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Inventory: "127.0.0.1,",
}

playbookCmd := &playbook.AnsiblePlaybookCmd{
Playbooks: []string{"site.yml"},
PlaybookOptions: ansiblePlaybookOptions,
}
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

executorTimeMeasurement := measure.NewExecutorTimeMeasurement(
execute.NewDefaultExecute(
Expand Down Expand Up @@ -695,6 +702,12 @@ The `github.com/apenella/go-ansible/v2/pkg/inventory` package provides the funct

The `AnsibleInventoryCmd` struct enables the generation of `ansible-inventory` commands. It implements the [Commander](#commander-interface) interface, so its method `Command` returns an array of strings that represents the command to be executed. An executor can use it to create the command to be executed.

The package provides the `NewAnsibleInventoryCmd` function to create a new instance of the `AnsibleInventoryCmd` struct. The function accepts a list of options to customize the `ansible-inventory` command. The following functions are available:

- `WithBinary(binary string) InventoryOptionsFunc`: Set the binary for the `ansible-inventory` command.
- `WithInventoryOptions(options *AnsibleInventoryOptions) InventoryOptionsFunc`: Set the inventory options for the command.
- `WithPattern(pattern string) InventoryOptionsFunc`: Set the pattern for the `ansible-inventory` command.

> Note
> Unlike other _Ansible_ commands, the `ansible-inventory` command does not provide privilege escalation or connection options, aligning with the functionality of the command itself.

Expand Down Expand Up @@ -735,6 +748,12 @@ The `github.com/apenella/go-ansible/v2/pkg/playbook` package facilitates the gen

The `AnsiblePlaybookCmd` struct enables the generation of _ansible-playbook_ commands. It implements the [Commander](#commander-interface) interface, so its method `Command` returns an array of strings that represents the command to be executed. An executor can use it to create the command to be executed.

The package provides the `NewAnsiblePlaybookCmd` function to create a new instance of the `AnsiblePlaybookCmd` struct. The function accepts a list of options to customize the _ansible-playbook_ command. The following functions are available:

- `WithBinary(binary string) PlaybookOptionsFunc`: Set the binary for the _ansible-playbook_ command.
- `WithPlaybookOptions(options *AnsiblePlaybookOptions) PlaybookOptionsFunc`: Set the playbook options for the command.
- `WithPlaybooks(playbooks ...string) PlaybookOptionsFunc`: Set the playbooks for the _ansible-playbook_ command.

Next is an example of how to use the `AnsiblePlaybookCmd` struct to generate an _ansible-playbook_ command:

```go
Expand All @@ -744,10 +763,10 @@ ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Inventory: "127.0.0.1,",
}

playbookCmd := &playbook.AnsiblePlaybookCmd{
Playbook: "site.yml",
PlaybookOptions: ansiblePlaybookOptions,
}
playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

// Generate the command to be executed
cmd, err := playbookCmd.Command()
Expand Down
17 changes: 11 additions & 6 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release notes

## v2.0.0-rc.2
## v2.0.0-rc.3

Version 2.0.0 of *go-ansible* introduces several disruptive changes. Read the upgrade guide carefully before proceeding with the upgrade.

Expand All @@ -11,7 +11,7 @@ Version 2.0.0 of *go-ansible* introduces several disruptive changes. Read the up

- The Go module name has been changed from `github.com/apenella/go-ansible` to `github.com/apenella/go-ansible/v2`. So, you need to update your import paths to use the new module name.
- The relationship between the executor and `AnsiblePlaybookCmd` / `AnsibleAdhocCmd` / `AnsibleInvetoryCmd` has undergone important changes.
- **Inversion of responsabilities**: The executor is now responsible for executing external commands, while `AnsiblePlaybookCmd`, `AnsibleInventoryCmd` and `AnsibleAdhocCmd` have cut down their responsibilities, primarily focusing on generating the command to be executed.
- **Inversion of responsibilities**: The executor is now responsible for executing external commands, while `AnsiblePlaybookCmd`, `AnsibleInventoryCmd` and `AnsibleAdhocCmd` have cut down their responsibilities, primarily focusing on generating the command to be executed.
- **Method and Attribute Removal**: The following methods and attributes have been removed on `AnsiblePlaybookCmd`, `AnsibleInventoryCmd` and `AnsibleAdhocCmd`:
- The `Run` method.
- The `Exec` and `StdoutCallback` attributes.
Expand All @@ -23,23 +23,28 @@ Version 2.0.0 of *go-ansible* introduces several disruptive changes. Read the up
- The functions `AnsibleForceColor`, `AnsibleAvoidHostKeyChecking` and `AnsibleSetEnv` have been removed from the `github.com/apenella/go-ansible/pkg/options` package. Use the `ExecutorWithAnsibleConfigurationSettings` decorator instead defined in the `github.com/apenella/go-ansible/v2/pkg/execute/configuration` package.
- The methods `WithWrite` and `WithShowduration` have been removed from the `ExecutorTimeMeasurement` decorator. Instead, a new method named `Duration` has been introduced for obtaining the duration of the execution.

### Fixed

- Quote properly the attributes `SCPExtraArgs`, `SFTPExtraArgs`, `SSHCommonArgs`, `SSHExtraArgs` in `AnsibleAdhocOptions` and `AnsiblePlaybookOptions` structs when generating the command to be executed. #140

### Added

- A new _executor_ `AnsibleAdhocExecute` has been introduced. That _executor_ allows you to create an executor to run `ansible` commands using the default settings of `DefaultExecute`. This _executor_ is located in the `github.com/apenella/go-ansible/v2/pkg/execute/adhoc` package.
- A new _executor_ `AnsibleInventoryExecute` has been introduced. That _executor_ allows you to create an executor to run `ansible-inventory` commands using the default settings of `DefaultExecute`. This _executor_ is located in the `github.com/apenella/go-ansible/v2/pkg/execute/inventory` package.
- A new _executor_ `AnsiblePlaybookExecute` has been introduced. That _executor_ allows you to create an executor to run `ansible-playbook` commands using the default settings of `DefaultExecute`. This _executor_ is located in the `github.com/apenella/go-ansible/v2/pkg/execute/playbook` package.
- A new interface `Commander` has been introduced in the `github.com/apenella/go-ansible/v2/pkg/execute` package. This interface defines the criteria for a struct to be compliant in generating execution commands.
- A new interface `Executabler` has been introduced in the `github.com/apenella/go-ansible/v2/pkg/execute` package. This interface defines the criteria for a struct to be compliant in executing external commands.
- A new interface `ExecutorEnvVarSetter` in `github.com/apenella/go-ansible/v2/pkg/execute/configuration` that defines the criteria for a struct to be compliant in setting Ansible configuration.
- A new interface `ExecutorEnvVarSetter` in `github.com/apenella/go-ansible/v2/pkg/execute/configuration` defines the criteria for a struct to be compliant in setting Ansible configuration.
- A new interface `ExecutorStdoutCallbackSetter` has been introduced in the `github.com/apenella/go-ansible/v2/pkg/execute/stdoutcallback` package. This interface defines the criteria for a struct to be compliant in setting an executor that accepts the stdout callback configuration for Ansible executions.
- A new interface named `ResultsOutputer` has been introduced in the `github.com/apenella/go-ansible/v2/pkg/execute/result` pacakge. This interface defines the criteria for a struct to be compliant in printing execution results.
- A new interface named `ResultsOutputer` has been introduced in the `github.com/apenella/go-ansible/v2/pkg/execute/result` package. This interface defines the criteria for a struct to be compliant in printing execution results.
- A new package `github.com/apenella/go-ansible/v2/internal/executable/os/exec` has been introduced. This package serves as a wrapper for `os.exec`.
- A new package `github.com/apenella/go-ansible/v2/pkg/execute/configuration` that incldues the `ExecutorWithAnsibleConfigurationSettings` struct, which acts as a decorator that facilitates the configuration of Ansible settings within the executor.
- A new package `github.com/apenella/go-ansible/v2/pkg/execute/configuration` includes the `ExecutorWithAnsibleConfigurationSettings` struct, which acts as a decorator that facilitates the configuration of Ansible settings within the executor.
- A new package `github.com/apenella/go-ansible/v2/pkg/execute/result/default` has been introduced. This package offers the default component for printing execution results. It supersedes the `DefaultStdoutCallbackResults` function that was previously defined in the `github.com/apenella/go-ansible/v2/pkg/stdoutcallback` package.
- A new package `github.com/apenella/go-ansible/v2/pkg/execute/result/json` has been introduced. This package offers the component for printing execution results from the JSON stdout callback. It supersedes the `JSONStdoutCallbackResults` function that was previously defined in the `github.com/apenella/go-ansible/v2/pkg/stdoutcallback` package.
- A new package `github.com/apenella/go-ansible/v2/pkg/execute/stdoutcallback`. This package offers multiple decorators designed to set the stdout callback for Ansible executions.
- A new package `github.com/apenella/go-ansible/v2/pkg/execute/workflow` has been introduced. This package allows you to define a workflow for executing multiple commands in a sequence.
- An utility to generate the code for the configuration package has been introduced. This utility is located in the `utils/cmd/configGenerator.go`.
- A utility to generate the code for the configuration package has been introduced. This utility is located in the `utils/cmd/configGenerator.go`.
- New functions `NewAnsibleAdhocCmd`, `NewAnsibleInventoryCmd` and `NewAnsiblePlaybookCmd` have been introduced. These functions are responsible for creating the `AnsibleAdhocCmd`, `AnsibleInventoryCmd` and `AnsiblePlaybookCmd` structs, respectively.

### Changed

Expand Down
Loading
Loading