[英]If else condtion using terrafrom modules
我想在我的代码中创建一个 if else 条件。 假设我想提供一个服务器,我只想确定如果 (name = abc or name=xyz) & (type=pqr) 那么我的实例类型=jkl。
我无法在我的 variables.tf 文件中设置这样的条件。 PS:我是 Terraform 的新手(2 小时前)。 谢谢
这是一个例子。 我希望 instance_type 自动获取值“pqr”
testabc.tf
module "testabc" {
source ="/modules/xyz"
name = "abc"
hostname = "jdksnkfjsdn"
instance_type = "hfd"
}
您描述想要实现的目标的方式不可能以您尝试实现的方式实现。 但是,通过一些额外的配置,可以做到这一点。 例如,如果你要为根模块创建变量,那么你可以做你想做的。
variable "name" {
type = string
description = "Instance name."
}
variable "type" {
type = string
description = "Some type."
}
然后,在模块调用中,您可以将代码调整为以下几行:
module "testabc" {
source = "/modules/xyz"
name = var.name
hostname = "jdksnkfjsdn"
instance_type = (var.name == "abc" || var.name == "xyz" ) && (var.type == "pqr") ? "jkl" : "some other instance type"
}
上面的例子是在 terraform 中使用条件表达式 [1] 。它类似于编程语言中的三元运算符,就好像表达式的第一部分的计算结果为true
,然后是?
之后的值。 将分配给instance_type
参数。 如果它的计算结果为false
, instance_type
将在:
之后分配一个值。
换句话说,如果
(var.name == "abc" || var.name == "xyz" ) && (var.type == "pqr")
计算结果为true
,然后是instance_type = "jkl"
。 如果上述表达式为假,则instance_type = "some other instance type"
。
[1] https://developer.hashicorp.com/terraform/language/expressions/conditionals
我希望你知道你想要实现这一目标的原因和原因。 添加多个条件将使您的 Terraform 脚本更加复杂(阅读更多关于认知复杂性的信息),然后您可能会失去使用HCL等声明性语言的优势。 是的,您可以在许多情况下使用它,但通常您可能希望有一个可预测的 state,它由显式 Terraform 脚本表示。
现在,您可能想要更多地使用声明性语言进行练习,因此您可能会重新考虑您的逻辑以实现您想要的,而不是使用 Terraform 无法实现的“条件默认值”。
从您的其他问题( terraform modules if else condition ),我看到以下内容:
if (stack == dev | stack == staging ) & type = xyz):
instance_type =m5.large
else:
instance_type= anything_else
您可以使类似的事情变得更容易(在伪代码中):
if (stack != "prod" && type == xyz):
instance_type =m5.large
else:
instance_type= anything_else
这将使您的模块:
variable "name" {
type = string
description = "Instance name."
}
variable "environment" {
type = string
description = "Instance environment"
}
variable "type" {
type = string
description = "Some type."
}
module "testabc" {
source = "/modules/xyz"
name = "${var.environment}-${var.name}"
hostname = "jdksnkfjsdn"
instance_type = (var.environment != "prod" && var.type == "pqr") ? "jkl" : "some other instance type"
}
或者,您可以有多个*.tfvars
文件,您可以在其中设置变量的值,因此您的配置如下:
# dev.tfvars
name = "my dev instance name"
type = "my-dev-instance-type"
和
# prod.tfvars
name = "my production instance name"
type = "my-prod-instance-type"
然后你的模块看起来像:
variable "name" {
type = string
description = "Instance name."
}
variable "type" {
type = string
description = "Some type."
}
module "testabc" {
source = "/modules/xyz"
name = var.name
hostname = "jdksnkfjsdn"
instance_type = var.type
}
并且在应用时传递-env-file
参数,例如:
terraform apply -var-file="dev.tfvars"
如果这些方法都不适合您,请参考上面@Marko E 的回答,因为default
参数中的值之前应该是已知的,并且它们不应该引用配置中的其他内容。
此外,您可以查看local values
,您可以在其中定义将 object 或表达式的结果分配给局部变量:
variable "name" {
type = string
description = "Instance name."
}
variable "type" {
type = string
description = "Some type."
}
locals {
isSomething = (var.name == "abc" || var.name == "xyz" ) && (var.type == "pqr")
}
module "testabc" {
source = "/modules/xyz"
name = var.name
hostname = "jdksnkfjsdn"
instance_type = local.isSomething ? "jkl" : "some other instance type"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.