有没有办法实现以下逻辑
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.
我明白代码有什么问题,我只是想知道是否有办法绕过限制?
虽然有一个Github问题 https://github.com/hashicorp/terraform/issues/25609为了将其实现为一项功能,验证多个变量的唯一方法是使用局部变量在运行时抛出错误:
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 = ""
}
locals {
validate_fet_code_cnd = var.environment == "test" && length(var.fet_code) != 3
validate_fet_code_msg = "The environment has been set to 'test' but the fet_code has not been defined."
validate_fet_code_chk = regex(
"^${local.validate_fet_code_msg}$",
( !local.validate_fet_code_cnd
? local.validate_fet_code_msg
: "" ) )
}
这是一个混乱、粗略的黑客行为,但它应该可以防止应用无效值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)