赋值上下文中的多行 if 表达式

问题描述 投票:0回答:1

我有这个代码:

output "output_value" {
  value = (
    data.oci_identity_compartments.compartment[0].compartments[0].id if 
    length(data.oci_identity_compartments.compartment) > 0 && 
    length(data.oci_identity_compartments.compartment[0].compartments) > 0 
    else null
  )
  description = "This is the output value"
)

terraform fmt
给了我这个错误:

│ Error: Unbalanced parentheses
│
│   on path/to/my.tf line 16, in output "compartment_id":
│   15:   value = (
│   16:      data.oci_identity_compartments.compartment[0].compartments[0].id if
│
│ Expected a closing parenthesis to terminate the expression.
╵

╷
│ Error: Argument or block definition required
│
│   on path/to/my.tf line 17, in output "compartment_id":
│   17:      length(data.oci_identity_compartments.compartment) > 0 &&
│
│ An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.
╵

╷
│ Error: Argument or block definition required
│
│   on path/to/my.tf line 18, in output "compartment_id":
│   18:      length(data.oci_identity_compartments.compartment[0].compartments) > 0
│
│ An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.
╵

╷
│ Error: Invalid block definition
│
│   on path/to/my.tf line 19, in output "compartment_id":
│   19:      else null
│   20:   )
│
│ A block definition must have block content delimited by "{" and "}", starting on the same line as the block header.

我该怎么写呢?

terraform
1个回答
0
投票

您可能想要 terraform 中的 ternary 表达式,所以像这样:

output "output_value" {
  value = length(data.oci_identity_compartments.compartment) > 0 && length(data.oci_identity_compartments.compartment[0].compartments) > 0
  ? data.oci_identity_compartments.compartment[0].compartments[0].id
  : null
  description = "This is the output value"
)
© www.soinside.com 2019 - 2024. All rights reserved.