gocron/modules/ansible/ansible.go

53 lines
1.3 KiB
Go
Raw Normal View History

2017-03-10 09:08:51 +00:00
package ansible
2017-03-24 09:55:44 +00:00
// ansible ad-hoc 命令封装
2017-03-10 09:08:51 +00:00
import (
2017-04-02 02:38:49 +00:00
"errors"
"github.com/ouqiang/cron-scheduler/modules/utils"
2017-03-10 09:08:51 +00:00
)
2017-04-04 12:45:02 +00:00
// ansible是否有安装
func IsInstalled() bool {
_, err := utils.ExecShell("ansible", "--version")
return err == nil
}
2017-03-10 09:08:51 +00:00
/**
* ad-hoc
2017-03-24 09:55:44 +00:00
* hosts
* hostFile
* module
* args module
2017-04-02 02:19:52 +00:00
*/
func ExecCommand(hosts string, hostFile string, module string, args ...string) (output string, err error) {
2017-04-02 02:38:49 +00:00
if hosts == "" || hostFile == "" || module == "" {
err = errors.New("参数不完整")
return
}
commandArgs := []string{hosts, "-i", hostFile, "-m", module}
if len(args) > 0 {
commandArgs = append(commandArgs, args...)
}
output, err = utils.ExecShell("ansible", commandArgs...)
2017-03-10 09:08:51 +00:00
2017-04-02 02:38:49 +00:00
return
}
// 执行shell命令
2017-04-02 02:19:52 +00:00
func Shell(hosts string, hostFile string, args ...string) (output string, err error) {
2017-04-02 02:38:49 +00:00
return ExecCommand(hosts, hostFile, "shell", args...)
}
// 复制本地脚本到远程执行
2017-04-02 02:19:52 +00:00
func Script(hosts string, hostFile string, args ...string) (output string, err error) {
2017-04-02 02:38:49 +00:00
return ExecCommand(hosts, hostFile, "script", args...)
}
// 测试主机是否可通
2017-04-02 02:19:52 +00:00
func Ping(hosts string, hostFile string) (output string, err error) {
2017-04-02 02:38:49 +00:00
return ExecCommand(hosts, hostFile, "ping")
2017-04-02 02:19:52 +00:00
}