Tad*_*spy 6 digital-ocean terraform terraform-template-file
我正在使用Terraform设置在DigitalOcean上运行Consul的多个液滴。也许我缺少一些基本的知识,但是为它们提供正确的配置似乎很困难。
resource "digitalocean_droplet" "prime" {
count = 3
image = "${data.digitalocean_image.prime.image}"
name = "${format("%s-%02d-%s", "prime", count.index + 1, var.region)}"
private_networking = true
# ...
}
Run Code Online (Sandbox Code Playgroud)
每台机器都有两个网络接口-公共和专用。通过这种设置,似乎有必要提供bind_addr指向每个Droplet的私有IP地址的指示-否则领事退出并出现错误,指出存在多个私有(?!)地址。
最直接的解决方案是为每台机器提供一个配置文件,该文件在每种情况下几乎都是相同的,但bind_addr字段的值却不同,如下所示:
{
"server": true,
"data_dir": "/var/consul/data",
"ui": true,
"bind_addr": "${ private_ipv4_address }"
}
Run Code Online (Sandbox Code Playgroud)
模板是不是?我不知道如何使用它们。定义模板时,似乎只能为模板提供一次变量:
data "template_file" "consul-config" {
template = "${file("templates/consul-config.json.tpl")}"
vars {
private_ipv4_address = "10.0.0.1" # At this point the real address is not known
}
}
resource "digitalocean_droplet" "prime" {
...
provisioner "file" {
content = "${data.template_file.consul-config.rendered}"
destination = "/etc/consul.d/server.json"
# At this point the address is known: ${ self.private_ipv4_address },
# but is it possible to pass it to the template?
}
}
Run Code Online (Sandbox Code Playgroud)
我试图将数据块嵌套在资源块中,但随后出现如下错误:
Error: resource 'digitalocean_droplet.prime' provisioner file (#7): unknown resource 'data.template_file.consul-config' referenced in variable data.template_file.consul-config.rendered
Run Code Online (Sandbox Code Playgroud)
我当前使用的解决方法是将配置分为两部分(服务器和定制),并在文件配置器中内联定制的内容:
resource "digitalocean_droplet" "prime" {
# ...
provisioner "file" {
content = "${data.template_file.consul-config.rendered}"
destination = "/etc/consul.d/server.json"
}
# This is a custom configuration for each particular droplet
provisioner "file" {
content = "{ \"bind_addr\": \"${ self.ipv4_address_private }\", \"bootstrap\": ${ count.index == 0 } }"
destination = "/etc/consul.d/custom.json"
}
}
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是由于以下几个原因而影响了可读性:
所有引号都必须转义
一切都必须单行(?)
没有语法突出显示或文本编辑器提供的类似帮助
另外,我考虑过使用外部程序(如envsubst)来渲染模板,或者将内置format函数与文件函数一起使用,但是每一个似乎都很麻烦。
有实现我想要的目标的简单方法吗?
小智 2
您是否尝试过使用编写模块?
这可能是一个很好的起点: https://blog.gruntwork.io/how-to-create-reusable-infrastruct-with-terraform-modules-25526d65f73d
| 归档时间: |
|
| 查看次数: |
991 次 |
| 最近记录: |