简体   繁体   English

Terraform 变量验证 Terraform v0.13 与长度和 Substr

[英]Terraform variable validation Terraform v0.13 with length & Substr

Terraform variable validation using length function Getting error while using length function & substr for vswitch_ids Condition - vswitch_name value must start with vsw- Terraform 变量验证使用长度 function 使用长度时出错 function & substr for vswitch_ids 条件 - vswitch_name 值必须以 vsw 开头

variable "vswitch_ids" {
description = "The vswitch IDs."
type        = list(string)
validation {
condition = (
  length(var.vswitch_ids) > 0 &&
  substr(var.switch_ids, 0, 4) == "vsw-" 
  )
error_message = "The vswitch_name value must start with \"vsw-\"."

} } } }

Error: Invalid function argument 
on modules/k8s/variables.tf line 34, in variable "vswitch_ids":
34:       substr(var.vswitch_ids, 0, 4) == "vsw-" 
|----------------
| var.vswitch_ids is list of string with 3 elements
Invalid value for "str" parameter: string required.

The following should work.以下应该工作。 It will check if all elements in your variable list start with vsw :它将检查变量列表中的所有元素是否以vsw开头:

variable "vswitch_ids" {
  description = "The vswitch IDs."
  type        = list(string)
  validation {
    condition = (
      length(var.vswitch_ids) > 0 &&
      length([for v in var.vswitch_ids: 1 if substr(v, 0, 4) == "vsw-"]) == length(var.vswitch_ids)
      )
      error_message = "The vswitch_name value must start with \"vsw-\"."
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM