数值与 Terraform 模板文件模板中两个变量的比较
Comparision of numeric values with two variables in Terraform templatefile template
我正在尝试使用 Terraform 的模板文件功能创建渲染文件。
这是我的代码:
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"
}
[all]
%{ for index, ip in private ~}
${prefix}-${index} ansible_host=${ip}
%{ endfor ~}
[kube-master]
%{ for index, ip in private ~}
%{ if index < masters_count ~}${prefix}-${index}%{ endif ~}
%{ endfor ~}
[calico-rr]
[k8s-cluster:children]
kube-master
kube-node
calico-rr
我的问题是 %{ if index < masters_count ~}
条件不起作用,我收到 Terraform 错误:
Call to function "templatefile" failed: inventory.ini.tmpl:11,7-12: Invalid
operand; Unsuitable value for left operand: a number is required., and 4 other
diagnostic(s).
看起来它不知道 if 块中的索引变量。
我怎样才能使该行的比较可以访问索引变量?
您的 local.ips
将是一个 map 的形式:
{
"addressname1" = "ip1"
"addressname2" = "ip2"
}
因此,在您的模板中编写 %{ for index, ip in private ~}
时,index
将是字符串 (例如 addressname1
和 addressname2
), 而不是数字。这意味着您不能在 if 表达式中使用 index
。
不清楚你想用模板中的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 ~}
我正在尝试使用 Terraform 的模板文件功能创建渲染文件。
这是我的代码:
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"
}
[all]
%{ for index, ip in private ~}
${prefix}-${index} ansible_host=${ip}
%{ endfor ~}
[kube-master]
%{ for index, ip in private ~}
%{ if index < masters_count ~}${prefix}-${index}%{ endif ~}
%{ endfor ~}
[calico-rr]
[k8s-cluster:children]
kube-master
kube-node
calico-rr
我的问题是 %{ if index < masters_count ~}
条件不起作用,我收到 Terraform 错误:
Call to function "templatefile" failed: inventory.ini.tmpl:11,7-12: Invalid
operand; Unsuitable value for left operand: a number is required., and 4 other
diagnostic(s).
看起来它不知道 if 块中的索引变量。 我怎样才能使该行的比较可以访问索引变量?
您的 local.ips
将是一个 map 的形式:
{
"addressname1" = "ip1"
"addressname2" = "ip2"
}
因此,在您的模板中编写 %{ for index, ip in private ~}
时,index
将是字符串 (例如 addressname1
和 addressname2
), 而不是数字。这意味着您不能在 if 表达式中使用 index
。
不清楚你想用模板中的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 ~}