ansible

package
v2.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 2 Imported by: 0

README

goutils/v2/ansible

The ansible package is a collection of utility functions designed to simplify common ansible tasks.


Table of contents


Functions

Ping(string)
Ping(string) error

Ping runs the ansible all -m ping command against all nodes found in the provided hosts file by using the mage/sh package to execute the command. If the command execution fails, an error is returned.

Parameters:

hostsFile: A string representing the path to the hosts file to be used by the ansible command.

Returns:

error: An error if the ansible command execution fails.


Installation

To use the goutils/v2/ansible package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/ansible

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/ansible"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/ansible:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ping

func Ping(hostsFile string) error

Ping runs the `ansible all -m ping` command against all nodes found in the provided hosts file by using the mage/sh package to execute the command. If the command execution fails, an error is returned.

**Parameters:**

hostsFile: A string representing the path to the hosts file to be used by the `ansible` command.

**Returns:**

error: An error if the `ansible` command execution fails.

Example
package main

import (
	"fmt"
	"io"
	"os"

	"github.com/l50/goutils/v2/ansible"
)

func main() {
	hostsFile := "/path/to/your/hosts.ini"

	// We have to replace the standard output temporarily to capture it.
	old := os.Stdout
	r, w, err := os.Pipe()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
	os.Stdout = w

	if err := ansible.Ping(hostsFile); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	// Close the Pipe Writer to let the ReadString finish properly.
	w.Close()

	// Restore the standard output.
	os.Stdout = old

	out, err := io.ReadAll(r)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	// Display the result we've captured.
	fmt.Print(string(out))

}
Output:

localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL