-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible.go
41 lines (35 loc) · 846 Bytes
/
ansible.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package ansible
import (
"os"
"github.com/magefile/mage/sh"
)
// 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.
func Ping(hostsFile string) error {
args := []string{
"all",
"-m",
"ping",
}
// Check if the hosts file exists
if _, err := os.Stat(hostsFile); os.IsNotExist(err) {
args = append(args, "-l", "localhost")
} else {
args = append(args, "-i", hostsFile)
}
if err := sh.RunV("ansible", args...); err != nil {
return err
}
return nil
}