2017-03-10 09:08:51 +00:00
|
|
|
package ansible
|
|
|
|
|
|
|
|
// ansible ad-hoc playbook命令封装
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"errors"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"io/ioutil"
|
2017-03-23 05:58:42 +00:00
|
|
|
"github.com/ouqiang/cron-scheduler/modules/utils"
|
2017-03-10 09:08:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler map[string]interface{}
|
|
|
|
|
|
|
|
type Playbook struct {
|
|
|
|
Name string
|
|
|
|
Hosts string
|
|
|
|
Tasks []Handler
|
|
|
|
Handlers []Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func(playbook *Playbook) SetHosts(hosts string) {
|
|
|
|
playbook.Hosts = hosts
|
|
|
|
}
|
|
|
|
|
|
|
|
func(playbook *Playbook) SetName(name string) {
|
|
|
|
playbook.Name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
func(playbook *Playbook) AddTask(handler Handler) {
|
|
|
|
playbook.Tasks = append(playbook.Tasks, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func(playbook *Playbook) AddHandler(handler Handler) {
|
|
|
|
playbook.Handlers = append(playbook.Handlers, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 执行ad-hoc
|
2017-03-23 05:31:16 +00:00
|
|
|
* hosts 主机名 逗号分隔
|
2017-03-10 09:08:51 +00:00
|
|
|
* module 调用模块
|
|
|
|
* args 传递给模块的参数
|
|
|
|
*/
|
2017-03-23 05:31:16 +00:00
|
|
|
func ExecCommand(hosts string, module string, args... string) (output string, err error) {
|
|
|
|
if hosts== "" || module == "" {
|
2017-03-10 09:08:51 +00:00
|
|
|
err = errors.New("参数不完整")
|
|
|
|
return
|
|
|
|
}
|
2017-03-23 05:31:16 +00:00
|
|
|
hostFile, err := DefaultHosts.GetHostFile()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
os.Remove(hostFile)
|
|
|
|
}()
|
|
|
|
commandArgs := []string{hosts, "-i", hostFile, "-m", module}
|
2017-03-10 09:08:51 +00:00
|
|
|
if len(args) != 0 {
|
2017-03-23 05:31:16 +00:00
|
|
|
commandArgs = append(commandArgs, "-a")
|
|
|
|
commandArgs = append(commandArgs, args...)
|
2017-03-10 09:08:51 +00:00
|
|
|
}
|
|
|
|
output, err = utils.ExecShell("ansible", commandArgs...)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 执行playbook
|
2017-03-23 05:31:16 +00:00
|
|
|
func ExecPlaybook(playbook Playbook) (result string, err error) {
|
2017-03-10 09:08:51 +00:00
|
|
|
data, err := yaml.Marshal([]Playbook{playbook})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-23 05:31:16 +00:00
|
|
|
playbookFile, err := ioutil.TempFile(GetTmpDir(), "playbook")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hostFile, err := DefaultHosts.GetHostFile()
|
2017-03-10 09:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
2017-03-23 05:31:16 +00:00
|
|
|
playbookFile.Close()
|
|
|
|
os.Remove(playbookFile.Name())
|
|
|
|
os.Remove(hostFile)
|
2017-03-10 09:08:51 +00:00
|
|
|
}()
|
2017-03-23 05:31:16 +00:00
|
|
|
_, err = playbookFile.Write(data)
|
2017-03-10 09:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-23 05:31:16 +00:00
|
|
|
commandArgs := []string{"-i", hostFile, playbookFile.Name()}
|
2017-03-10 09:08:51 +00:00
|
|
|
result, err = utils.ExecShell("ansible-playbook", commandArgs...)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 判断 获取临时目录,默认/dev/shm
|
2017-03-23 05:31:16 +00:00
|
|
|
func GetTmpDir() string {
|
2017-03-10 09:08:51 +00:00
|
|
|
dir := "/dev/shm"
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|