本文介绍了当前从 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 库存的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:20