当前从terraform创建ansible库存的最佳方法 [英] Best way currently to create an ansible inventory from terraform

查看:213
本文介绍了当前从terraform创建ansible库存的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长串机器,所有机器在系统功能上都有些不同.我想整理这些机器并自动将其添加到主机清单文件中,以便我可以运行ansible和管理清单.有好的解决方案吗?

I have a long list of machines, all of which are a little different in functionality in a system. I'd like to organize these machines and add to a hosts inventory file automatically so I can run ansible and manage inventory. Are there good solutions out there for this?

我认为Ansible主机应该看起来像...

I think ansible hosts should looks something like...

[webservers]
someip
someip
[integration]
someip
someip

等.

问了问题之后,我目前正在研究输出变量,并使用它们来从文件中渲染模板.

After asking the question, I currently am researching output vars and using those to render a template from a file.

推荐答案

我知道了.

data "template_file" "dev_hosts" {
  template = "${file("${path.module}/templates/dev_hosts.cfg")}"
  depends_on = [
    "aws_instance.dev-api-gateway",
    "aws_instance.dev-api-gateway-internal",
    ....
  ]
  vars {
    api_public = "${aws_instance.dev-api-gateway.private_ip}"
    api_internal = "${aws_instance.dev-api-gateway-internal.private_ip}"
  }
}

resource "null_resource" "dev-hosts" {
  triggers {
    template_rendered = "${data.template_file.dev_hosts.rendered}"
  }
  provisioner "local-exec" {
    command = "echo '${data.template_file.dev_hosts.rendered}' > dev_hosts"
  }
}

然后在之前引用的文件中创建一个模板

Then create a template in the file referenced earlier

dev_hosts.cfg示例的内容

Contents of example dev_hosts.cfg

[public]
${api_public}


[private]
${api_internal}

这篇关于当前从terraform创建ansible库存的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆