go 调用 salt-api 接口
go get github.com/daixijun/go-salt
package main
import (
"context"
"fmt"
salt "github.com/daixijun/go-salt/v2"
)
func main() {
ctx := context.TODO()
// 初始化客户端
client := salt.NewClient(
salt.WithEndpoint("https://saltapi.example.com"),
salt.WithUsername("saltapi"),
salt.WithPassword("saltapi"),
salt.WithAuthBackend("pam"),
salt.WithInsecure(),
)
if err := client.Login(ctx); err != nil {
panic(err)
}
// 列表 minions
minions, err := client.ListMinions(ctx)
if err != nil {
panic(err)
}
fmt.Println(minions)
// 执行命令
resp, err := client.LocalClient(ctx, "cmd.run", []string{"whoami"}, salt.WithGlobTarget("minion1"))
if err != nil {
panic(err)
}
fmt.Println(resp)
}