gocron/modules/ansible/ansible.go

46 lines
1.2 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 (
"errors"
2017-03-23 05:58:42 +00:00
"github.com/ouqiang/cron-scheduler/modules/utils"
2017-03-10 09:08:51 +00:00
)
/**
* ad-hoc
2017-03-24 09:55:44 +00:00
* hosts
* hostFile
* module
* args module
2017-03-10 09:08:51 +00:00
*/
func ExecCommand(hosts string, hostFile string, module string, args... string) (output string, err error) {
if hosts== "" || hostFile == "" || module == "" {
2017-03-10 09:08:51 +00:00
err = errors.New("参数不完整")
return
}
commandArgs := []string{hosts, "-i", hostFile, "-m", module}
if len(args) > 0 {
commandArgs = append(commandArgs, args...)
}
2017-03-10 09:08:51 +00:00
output, err = utils.ExecShell("ansible", commandArgs...)
return
}
// 执行shell命令
func Shell(hosts string, hostFile string, args... string) (output string, err error) {
return ExecCommand(hosts, hostFile, "shell", args...)
}
// 复制本地脚本到远程执行
func Script(hosts string, hostFile string, args... string) (output string, err error) {
return ExecCommand(hosts, hostFile, "script", args...)
}
// 测试主机是否可通
func Ping(hosts string, hostFile string) (output string, err error) {
return ExecCommand(hosts, hostFile, "ping")
2017-03-24 09:55:44 +00:00
}