Terraform templatefile template中两个变量数值的比较
我正在尝试使用Terraform的templatefile函数创建渲染文件 这是我的密码:Terraform templatefile template中两个变量数值的比较,terraform,terraform-template-file,Terraform,Terraform Template File,我正在尝试使用Terraform的templatefile函数创建渲染文件 这是我的密码: variable "prefix" { default = "k8s" } variable "masters_count" { default = 2 } resource "google_compute_address" "k8s-static-ips" { name = "$
variable "prefix" {
default = "k8s"
}
variable "masters_count" {
default = 2
}
resource "google_compute_address" "k8s-static-ips" {
name = "${var.prefix}-${count.index}"
count = var.num_k8s_nodes
}
locals {
ips = {
for k in google_compute_address.k8s-static-ips : k.name => k.address
}
}
resource "local_file" "AnsibleInventory" {
content = templatefile("inventory.ini.tmpl",
{
private = local.ips
prefix = var.prefix
masters_count = var.masters_count
}
)
filename = "inventory.ini"
}
看起来它不知道if块中的索引变量。
如何才能访问索引变量使该行的比较成为可能?您的
local.ips
将是以下形式的映射:
{
"addressname1" = "ip1"
"addressname2" = "ip2"
}
因此,在您的模板中,当您编写%{for index,ip in private}
时,index
将是字符串(例如地址名1
和地址名2
),而不是数字。这意味着您不能在if表达式中使用索引
不清楚您希望通过模板中的local.ips
和for
循环实现什么。但是,如果您在AnsibleInventory
中使用以下命令,它至少会起作用:
private = values(local.ips)
我现在的解决方法不同了
[kube-master]
%{ for index, ip in private ~}
%{ if tonumber(element(split("-",index),1)) < masters_count ~}${index}
%{ endif ~}
%{ endfor ~}
[kube master]
%{对于索引,私有~}
%{if tonumber(元素(拆分(“-”,索引),1))
private = values(local.ips)
[kube-master]
%{ for index, ip in private ~}
%{ if tonumber(element(split("-",index),1)) < masters_count ~}${index}
%{ endif ~}
%{ endfor ~}