Skip to content

Commit

Permalink
Import changes 1.3.0 (#137)
Browse files Browse the repository at this point in the history
import changes from v1.3.0 and adapt the AnsibleInventoryCmd to implement the Commander interface.
  • Loading branch information
apenella authored Feb 18, 2024
1 parent 984f5c0 commit f5c47d5
Show file tree
Hide file tree
Showing 14 changed files with 856 additions and 14 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v1.3.0

### Added

- New feature to execute the Ansible inventory command. [#132](https://github.com/apenella/go-ansible/issues/132)

## v1.2.2

### Changed

- Bump golang.org/x/crypto from 0.8.0 to 0.17.0

## v1.2.1

### Fix
### Fixed

- In `AnsibleConnectionOptions`, add quotes to ssh, sftp, and scp arguments when generating the command

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ _**Important:** The master branch may contain unreleased or pre-released feature
- [Stdoutcallback package](#stdoutcallback-package)
- [ExecutorStdoutCallbackSetter interface](#executorstdoutcallbacksetter-interface)
- [Stdout Callback Execute structs](#stdout-callback-execute-structs)
- [Inventory package](#inventory-package)
- [AnsibleInventoryCmd struct](#ansibleinventorycmd-struct)
- [AnsibleInventoryOptions struct](#ansibleinventoryoptions-struct)
- [Options package](#options-package)
- [AnsibleConnectionOptions struct](#ansibleconnectionoptions-struct)
- [AnsiblePrivilegeEscalationOptions struct](#ansibleprivilegeescalationoptions-struct)
Expand Down Expand Up @@ -644,6 +647,23 @@ if err != nil {
}
```
### Inventory package
The information provided in this section gives an overview of the `Inventory` package in `go-ansible`.
The `github.com/apenella/go-ansible/pkg/inventory` package provides the functionality to execute `ansible-inventory`. To perform these tasks, you can use the following inventory structs:
#### AnsibleInventoryCmd struct
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.
> 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.
#### AnsibleInventoryOptions struct
The `AnsibleInventoryOptions` struct includes parameters described in the `Options` section of the _Ansible_ manual page. It defines the behavior of the Ansible inventory operations and specifies where to find the configuration settings.
### Options package
These options can be used to customize the behaviour of `ansible` and `ansible-playbook` commands executions.
Expand Down
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Version 2.0.0 of *go-ansible* introduces several disruptive changes. Read the up
- A new package `github.com/apenella/go-ansible/pkg/execute/stdoutcallback`. This package offers multiple decorators designed to set the stdout callback for Ansible executions.
- An utility to generate the code for the configuration package has been introduced. This utility is located in the `utils/cmd/configGenerator.go`.

### Changed
### Added

- The `AdhocPlaybookCmd` struct has been updated to implement the `Commander` interface.
- The `AnsiblePlaybookCmd` struct has been updated to implement the `Commander` interface.
Expand Down
34 changes: 34 additions & 0 deletions examples/ansibleinventory-graph/ansibleinventory-graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"fmt"

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

func main() {
ansibleInventoryOptions := inventory.AnsibleInventoryOptions{
Graph: true,
Inventory: "inventory.yml",
Vars: true,
Yaml: true,
}

inventoryCmd := &inventory.AnsibleInventoryCmd{
Pattern: "all",
Options: &ansibleInventoryOptions,
}

fmt.Println("Test strings:", inventoryCmd.String())

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to VaultPasswordFile
flows to a logging call.

exec := execute.NewDefaultExecute(
execute.WithCmd(inventoryCmd),
)

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
}
22 changes: 22 additions & 0 deletions examples/ansibleinventory-graph/inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all:
children:
webserver:
hosts:
web1:
ansible_host: 192.168.1.101
http_port: 80
max_clients: 200
web2:
ansible_host: 192.168.1.102
http_port: 80
max_clients: 150

database:
hosts:
db1:
ansible_host: 192.168.1.103
db_port: 5432
db_name: 'mydb'

vars:
ansible_user: 'admin'
33 changes: 33 additions & 0 deletions examples/ansibleinventory-simple/ansibleinventory-simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"context"
"fmt"

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

func main() {
ansibleInventoryOptions := inventory.AnsibleInventoryOptions{
Inventory: "inventory.yml",
List: true,
Yaml: true,
}

inventoryCmd := &inventory.AnsibleInventoryCmd{
Pattern: "all",
Options: &ansibleInventoryOptions,
}

fmt.Println("Test strings:", inventoryCmd.String())

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to VaultPasswordFile
flows to a logging call.

exec := execute.NewDefaultExecute(
execute.WithCmd(inventoryCmd),
)

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
}
22 changes: 22 additions & 0 deletions examples/ansibleinventory-simple/inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all:
children:
webserver:
hosts:
web1:
ansible_host: 192.168.1.101
http_port: 80
max_clients: 200
web2:
ansible_host: 192.168.1.102
http_port: 80
max_clients: 150

database:
hosts:
db1:
ansible_host: 192.168.1.103
db_port: 5432
db_name: 'mydb'

vars:
ansible_user: 'admin'
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"context"
"fmt"

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

func main() {
ansibleInventoryOptions := inventory.AnsibleInventoryOptions{
Graph: true,
Inventory: "inventory.yml",
Vars: true,
Yaml: true,
VaultPasswordFile: "vault_password.cfg",
}

inventoryCmd := &inventory.AnsibleInventoryCmd{
Pattern: "all",
Options: &ansibleInventoryOptions,
}

fmt.Println("Test strings:", inventoryCmd.String())

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to VaultPasswordFile
flows to a logging call.

exec := execute.NewDefaultExecute(
execute.WithCmd(inventoryCmd),
)

err := exec.Execute(context.TODO())
if err != nil {
panic(err)
}
}
16 changes: 16 additions & 0 deletions examples/ansibleinventory-vaulted-vars/inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
all:
children:
database_servers:
hosts:
db1.example.com:
db2.example.com:
vars:
ansible_ssh_user: dbadmin
ansible_ssh_pass: !vault |
$ANSIBLE_VAULT;1.1;AES256
35666433353234313161636238653633313264383230316233656236313935313465666533646164
3039353461343961616134653164666663646362646336360a383332663562396237613264386636
66663233616235396662383434343966333665383937653839326633333861366162616365353533
6237653031613664340a383736643130303935633164366536363561663334643763343661666138
62343234643536663061316666626466343265353065333439643065313134633132
db_port: 5432
1 change: 1 addition & 0 deletions examples/ansibleinventory-vaulted-vars/vault_password.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ThatIsAPassword
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.4.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -268,8 +268,8 @@ golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -331,8 +331,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand All @@ -345,8 +345,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
Loading

0 comments on commit f5c47d5

Please sign in to comment.