从defaults.tfvars中添加Terraform到列表



我正在尝试扩展一个声明的变量列表,这是可能的使用地形?

声明变量如下:

variable "nat_rules" {
type = list(object({
name          = string
protocol      = string
frontend_port = number
backend_port  = number
}))
}

则为默认值。我想把它设置为默认的NAT列表:

nat_rules = [
{
name = "Admin"
protocol      = "Tcp"
frontend_port = 11389
backend_port  = 3389
}, {
name = "HTTP"
protocol      = "Tcp"
frontend_port = 80
backend_port  = 80
}, {
name = "HTTPs"
protocol      = "Tcp"
frontend_port = 443
backend_port  = 443
}]

现在在环境覆盖tfvars文件中,我想从默认值中添加第4项。就像下面这个,但是没有成功。

nat_rules = concat(var.nat_rules,[{
name = "SSH"
protocol      = "Tcp"
frontend_port = 22
backend_port  = 22
}])

编辑:

Error: Function calls not allowed
│
│   on environments/prod_ci/prod_ci.tfvars line 98:
│   98: nat_rules = concat(var.nat_rules,[{
│   99:     name = "SSH"
│  100:     protocol      = "Tcp"
│  101:     frontend_port = 22
│  102:     backend_port  = 22
│  103: }])
│
│ Functions may not be called here.
╵
基本上,我希望从其他语言中得到类似+=操作符的东西。我们的想法是能够定义一个大的通用规则列表,然后根据prod/test/dev的需要添加任何额外的规则,而不必为了添加一个额外的条目而重复整个列表。

错误在于,您不能调用.tfvars中的函数,

宁愿在tfvars文件中传递var,如

additional_rules = [{
name = "SSH"
protocol      = "Tcp"
frontend_port = 22
backend_port  = 22
}]
variable "additional_rules" {
type = list(object({
name          = string
protocol      = string
frontend_port = number
backend_port  = number
}))
}

,然后在locals中。您可以按照如下方式进行连接:

locals{ 
default_rules =  [
{
name = "Admin"
protocol      = "Tcp"
frontend_port = 11389
backend_port  = 3389
}, {
name = "HTTP"
protocol      = "Tcp"
frontend_port = 80
backend_port  = 80
}, {
name = "HTTPs"
protocol      = "Tcp"
frontend_port = 443
backend_port  = 443
}]
rules = concat(local.default_rules, var.additional_rules)
}

最新更新