ansible icon indicating copy to clipboard operation
ansible copied to clipboard

介绍用 golang 编写 Ansible 插件

Ansible

这个项目讲述了Ansible的基本使用方法,和开发自定义模块的方式和例子

目录

  • Ansible简介
    • Ansible的特点
    • Hello World
    • 安装方式
  • Hosts文件
    • 编写host文件
  • playbook
    • helloworld
    • 变量
    • 条件
  • 自定义模块
    • 参数
    • 返回值
    • 使用自定义模块
  • Ansible配置

demo

这里编写一个名字叫做fuck的模块,他的功能是往标准输出里面输出fuck.

package main

import (
	"encoding/json"
)

// Response 返回值的消息结构
type Response struct {
	Changed bool   `json:"changed"`
	Fail    bool   `json:"fail"`
	Msg     string `json:"msg"`
	RC      int    `json:"rc"`
}

func main() {
	println("fuck")
	var res = Response{
		Changed: false,
		Fail:    false,
		Msg:     "",
		RC:      0,
	}
	buf, _ := json.Marshal(res)
	println(string(buf))
}

编译之后放在模块的目录中

mac-pro:res jukay$ ansible dev -m fuck -u root
39.106.10.228 | SUCCESS => {
    "changed": false,
    "fail": false,
    "msg": "",
    "rc": 0
}
mac-pro:res jukay$