使用 AWS Go SDK 的动态清单

Dynamic inventories using AWS Go SDK

如前所述 here,如果您使用的是云提供商,则不应在静态文件中管理库存。而是使用动态库存


Ansible 文档仅提供 python boto sdk 作为动态清单,如图 here.

     ansible -i ec2.py -u ubuntu us-east-1d -m ping

ansible 是否允许(-i)执行使用 AWS Go sdk 编写的动态清单?而不是 python boto sdk.

是的,ansible 将使用任何可以产生必要的 JSON 输出的命令,包括 shell 脚本,如 the fine manual:

所指定

In previous versions you had to create a script or program that can output JSON in the correct format when invoked with the proper arguments. You can still use and write inventory scripts, as we ensured backwards compatibility via the script inventory plugin and there is no restriction on the programming language used.

作为一个具体的 golang 示例:

package main
import (
    "encoding/json"
    "fmt"
)
func main() {
    i := map[string]interface{}{
        "_meta": map[string]interface{}{
            "hostvars": map[string]interface{}{
                "example.host": map[string]interface{}{
                    "ansible_host": "127.0.0.1",
                    "ansible_user": "ubuntu",
                },
            },
        },
        "all": map[string]interface{}{
            "children": []string{"ungrouped"},
        },
        "ungrouped": map[string]interface{}{
            "hosts": []string{"example.host"},
        },
    }
    ba, err := json.Marshal(i)
    if err != nil { panic(err) }
    fmt.Println(string(ba))
}

通过常规机制调用:

go build -o sample-inv ./main.go
ansible -i ./sample-inv -m ping all