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

Adding feature to Execute the Inventory Command #134

Merged
merged 13 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Let's dive in and explore the capabilities of `go-ansible` together.
- [Packages](#packages)
- [Adhoc](#adhoc)
- [Playbook](#playbook)
- [Inventory](#Inventory)
- [Execute](#execute)
- [DefaultExecute](#defaultexecute)
- [Custom executor](#custom-executor)
Expand Down Expand Up @@ -160,6 +161,18 @@ The `github.com/apenella/go-ansible/pkg/playbook` package provides the functiona

Additionally, you can provide privilege escalation options or connection options to the `AnsiblePlaybookCmd`. These options are defined in the `github.com/apenella/go-ansible/pkg/options` package. Refer to the [options](#options) sections to know more about it.

### Inventory

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**: This main struct defines the Ansible inventory and specifies how to execute it. Defining an `AnsibleInventoryCmd` is mandatory for working with the inventory. The `AnsibleInventoryCmd` requires a parameter to specify which `Executor` to use. The executor acts as the worker responsible for launching the operations. If no `Executor` is explicitly specified, the `DefaultExecute` is used by default.

- **AnsibleInventoryOptions**: This struct offers parameters as 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.

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.

#### Execute

An executor in `go-ansible` is a component that executes the command and retrieves the results from stdout and stderr. The library includes a default executor implementation called `DefaultExecute`, which is located in the `github.com/apenella/go-ansible/pkg/execute` package. The `DefaultExecute` executor adheres to the `Executor` interface.
Expand Down
33 changes: 33 additions & 0 deletions examples/ansibleinventory-graph/ansibleinventory-graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"context"
"github.com/apenella/go-ansible/pkg/inventory"
"log"
)

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

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

results, _ := inventoryCmd.Command()
log.Println("Test strings", inventoryCmd.String())
for _, result := range results {
log.Println("Command Data: ", result)
}

err := inventoryCmd.Run(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'
32 changes: 32 additions & 0 deletions examples/ansibleinventory-simple/ansibleinventory-simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"github.com/apenella/go-ansible/pkg/inventory"
"log"
)

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

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

results, _ := inventoryCmd.Command()
log.Println("Test strings", inventoryCmd.String())
for _, result := range results {
log.Println("Command Data: ", result)
}

err := inventoryCmd.Run(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,34 @@
package main

import (
"context"
"github.com/apenella/go-ansible/pkg/inventory"
"log"
)

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

results, _ := inventoryCmd.Command()
log.Println("Test strings", inventoryCmd.String())
for _, result := range results {
log.Println("Command Data: ", result)
}

err := inventoryCmd.Run(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
Loading