简体   繁体   English

If else 条件使用 terrafrom 模块

[英]If else condtion using terrafrom modules

I want to create an if else condition in my code.我想在我的代码中创建一个 if else 条件。 Let's say I want to provision a server, I just want to make sure if (name = abc or name=xyz) & (type=pqr) then my instance type=jkl.假设我想提供一个服务器,我只想确定如果 (name = abc or name=xyz) & (type=pqr) 那么我的实例类型=jkl。

I am unable to set up such a condition in my variables.tf file.我无法在我的 variables.tf 文件中设置这样的条件。 PS: I am a newbie in Terraform(2hrs old). PS:我是 Terraform 的新手(2 小时前)。 thanks谢谢

here's an example.这是一个例子。 I want instance_type to automatically pick up a value "pqr"我希望 instance_type 自动获取值“pqr”

testabc.tf testabc.tf

module "testabc" {
 source ="/modules/xyz"
 name = "abc"
 hostname = "jdksnkfjsdn"
 instance_type = "hfd"
}

The way you have described what you want to achieve is not possible the way you are trying to do it.您描述想要实现的目标的方式不可能以您尝试实现的方式实现。 However, with some additional configuration, that can be done.但是,通过一些额外的配置,可以做到这一点。 For example, if you were to create variables for the root module, then you could do what you want.例如,如果你要为根模块创建变量,那么你可以做你想做的。

variable "name" {
  type        = string
  description = "Instance name."
}

variable "type" {
  type        = string
  description = "Some type."
}

Then, in the module call, you would adjust the code to something along the lines of:然后,在模块调用中,您可以将代码调整为以下几行:

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"
}

The above example is using conditional expression [1] in terraform. It is similar to ternary operators in programming languages, as if the first part of the expression evaluates to true , then the value after the ?上面的例子是在 terraform 中使用条件表达式 [1] 。它类似于编程语言中的三元运算符,就好像表达式的第一部分的计算结果为true ,然后是?之后的值。 will be assigned to the instance_type argument.将分配给instance_type参数。 If it evaluates to false , instance_type will be assigned a value after : .如果它的计算结果为falseinstance_type将在:之后分配一个值。

In other words, if换句话说,如果

(var.name == "abc" || var.name == "xyz" ) && (var.type == "pqr")

evaluates to true , then instance_type = "jkl" .计算结果为true ,然后是instance_type = "jkl" If the above expression is false, then instance_type = "some other instance type" .如果上述表达式为假,则instance_type = "some other instance type"


[1] https://developer.hashicorp.com/terraform/language/expressions/conditionals [1] https://developer.hashicorp.com/terraform/language/expressions/conditionals

I hope that you know what and why you want to achieve this.我希望你知道你想要实现这一目标的原因和原因。 Adding multiple conditions will make your Terraform script more complicated ( read more about Cognitive Complexity ), and then you may lose the advantage of using a declarative language like HCL .添加多个条件将使您的 Terraform 脚本更加复杂(阅读更多关于认知复杂性的信息),然后您可能会失去使用HCL等声明性语言的优势。 Yes, you may use it in MANY cases, but in general you may want to have a predictable state that's represented by an explicit Terraform scripts.是的,您可以在许多情况下使用它,但通常您可能希望有一个可预测的 state,它由显式 Terraform 脚本表示。

Now, you may want to practice more with declarative languages so you may rethink your logic to achieve what you want differently than by using a "conditional default value" which is not possible with Terraform.现在,您可能想要更多地使用声明性语言进行练习,因此您可能会重新考虑您的逻辑以实现您想要的,而不是使用 Terraform 无法实现的“条件默认值”。

From your other question ( terraform modules if else condition ), I see the following:从您的其他问题( terraform modules if else condition ),我看到以下内容:

if (stack == dev | stack == staging ) & type = xyz):
  instance_type =m5.large
else:
  instance_type= anything_else

You can make it easier to something like (in pseudo-code):您可以使类似的事情变得更容易(在伪代码中):

if (stack != "prod" && type == xyz):
  instance_type =m5.large
else:
  instance_type= anything_else

And this would make your module:这将使您的模块:

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"
}

Or, you can have multiple *.tfvars files where you set values for your variables, so you would have configuration like:或者,您可以有多个*.tfvars文件,您可以在其中设置变量的值,因此您的配置如下:

# dev.tfvars

name = "my dev instance name"
type = "my-dev-instance-type"

and

# prod.tfvars

name = "my production instance name"
type = "my-prod-instance-type"

Then your module would look like:然后你的模块看起来像:

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
}

And when applying you pass the -env-file parameter like:并且在应用时传递-env-file参数,例如:

terraform apply -var-file="dev.tfvars"

If none of these approaches is not suitable for you, please refer to @Marko E's answer above since the values in the default argument should be known before , and they shouldn't refer to something else in the configuration.如果这些方法都不适合您,请参考上面@Marko E 的回答,因为default参数中的值之前应该是已知的,并且它们不应该引用配置中的其他内容。

Also, you may have a look at local values where you can define assign an object or result of an expression to a local variable:此外,您可以查看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.

相关问题 React Native 并使用 npm 模块 - React Native and using npm modules 代码管道 Terrafrom || 操作“Deploy”的操作配置包含未知配置“DeploymentGroup” - CodePipline Terrafrom || Action configuration for action 'Deploy' contains unknown configuration 'DeploymentGroup' Terrafrom 数据源是一个本地文件,并从内容中检索一些键为 output - Terrafrom data source a localfile and retrieve some keys from the content as output 如何使用 Terrafrom 部署 azure 逻辑应用标准的工作流程 - how use Terrafrom to deploy azure logic app standard's workflows Cloud Scheduler - Terrafrom - 如何在 CloudSheduler 主体中传递当前日期 - Cloud Scheduler - Terrafrom - How to pass current date in CloudSheduler body 在 Ansible AWS 模块中使用变量作为标签名称 - Using variable for tag name in Ansible AWS modules 在大查询中使用 if-else 语句 - Using an if-else statement in big-query 如何使用 Terraform 制作 API 模块? - How to make API modules using Terraform? 使用Python Logging模块将日志保存到S3,如何抓取所有模块的各级日志? - Using Python Logging module to save logs to S3, how to capture all levels of logs for all modules? 在 terraform 中使用模块向 ec2 分配多个安全组时出错 - Getting error while assigning multiple security group using modules to ec2 in terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM