有没有一种实现以下逻辑的方法
variable "environment" {
description = "The environment this will be run in can only be set to [preprod|test|prod]"
type = string
default = "test"
validation {
condition = can(regex("^(prod|preprod|test)$", var.environment))
error_message = "The environment variable can only be set to [prod|preprod|test]."
}
}
variable "fet_code" {
description = "Set the feature code"
type = string
default = ""
validation {
condition = var.environment == "test" && length(var.fet_code) != 3
error_message = "The environment has been set to 'test' but the fet_code has not be defined."
}
}
目前我遇到以下错误:
Error: Invalid reference in variable validation
on variable.tf line 17, in variable "fet_code":
17: condition = var.environment == "fet" && length(var.fet_code) == 3
The condition for variable "fet_code" can only refer to the variable itself,
using var.fet_code.
我理解代码存在的问题,只是想知道是否有绕过限制的方法?
fet_code
的condition
中实现什么目标还不清楚,能否澄清一下?另外,为什么要检查environment
变量是否等于feature
,如果根据environment
上的condition
,这不是允许的输入值之一呢? - Matt Schuchardenvironment
被设置为test
,那么变量fet_code
必须被设置,否则它应该为空。 - alexistrue
/false
返回,例如:var.environment == "test" ? (length(var.fet_code) == 3) : true
。请注意,您还切换了对fet_code
长度验证的true
/false
返回。https://www.terraform.io/docs/configuration/expressions.html#conditional-expressions - Matt Schuchard