terraform和list,split返回了未知索引的最后一个元素

terraform和list,split返回了未知索引的最后一个元素,terraform,Terraform,在编写terraform代码的过程中遇到了一个问题,写了一个小例子,希望你们中的一些人能帮助我解决这个问题 当前代码如下所示: variable "aws-subnets" { description = "Test rule" default = [ "10.0.0.0/32:us-east-1-s3", "10.0.0.1/32:us-east-2-s3", "10.0.0.2/32", "", ] } data "template_file"

在编写terraform代码的过程中遇到了一个问题,写了一个小例子,希望你们中的一些人能帮助我解决这个问题

当前代码如下所示:

variable "aws-subnets" {
  description = "Test rule"

  default = [
    "10.0.0.0/32:us-east-1-s3",
    "10.0.0.1/32:us-east-2-s3",
    "10.0.0.2/32",
    "",
  ]
}

data "template_file" "ipset-wrapper" {
  count = "${length(var.aws-subnets)}"

  template = <<TXT
$${SUBNET_RANGE} comment $${SUBNET_COMMENT}
TXT

  vars {
    SUBNET_RANGE = "${element(split(":",var.aws-subnets[count.index]),0) != "" ? "${element(split(":",var.aws-subnets[count.index]),0)}" : "no_ip" }"
    SUBNET_COMMENT = "${element(split(":",var.aws-subnets[count.index]),1) != "" ? "${element(split(":",var.aws-subnets[count.index]),1)}" : "no_comment_provided" }"
),2))}"
  }
}

output "ipset-rules" {
  value = "${data.template_file.ipset-wrapper.*.rendered}"
}
3d字符串不是返回未知元素或元素为空,而是从split命令返回最后一个元素,这就是为什么条件:

${element(split(":",var.aws-subnets[count.index]),1) != ""}
这对我来说并不像预期的那样有效

如果没有定义第二个索引,那么在这种情况下编写代码的最佳方法是什么

我理解该代码:

    SUBNET_COMMENT = "${element(split(":",var.aws-subnets[count.index]),1) != element(split(":",var.aws-subnets[count.index]),0) ? "${element(split(":",var.aws-subnets[count.index]),1)}" : "no_comment_provided" }"

在这种情况下效果很好,但是如果我有两个以上的索引,if将不起作用


有什么帮助吗?

使用新的TF 0.12语法如何:

variable "aws-subnets" {
  description = "Test rule"
  type = map(string)
  default = {
    "10.0.0.0/32" = "us-east-1-s3",
    "10.0.0.1/32" = "us-east-2-s3",
    "10.0.0.2/32" = "",
    "" = "",
  }
}

data "template_file" "ipset-wrapper" {
  for_each = var.aws-subnets

  template = <<TXT
$${SUBNET_RANGE} comment $${SUBNET_COMMENT}
TXT

  vars = {
    SUBNET_RANGE   = each.key != "" ? each.key : "no_ip"
    SUBNET_COMMENT = each.value != "" ? each.value : "no_comment_provided"
  }
}

output "ipset-rules" {
  value = data.template_file.ipset-wrapper
}
variable "aws-subnets" {
  description = "Test rule"
  type = map(string)
  default = {
    "10.0.0.0/32" = "us-east-1-s3",
    "10.0.0.1/32" = "us-east-2-s3",
    "10.0.0.2/32" = "",
    "" = "",
  }
}

data "template_file" "ipset-wrapper" {
  for_each = var.aws-subnets

  template = <<TXT
$${SUBNET_RANGE} comment $${SUBNET_COMMENT}
TXT

  vars = {
    SUBNET_RANGE   = each.key != "" ? each.key : "no_ip"
    SUBNET_COMMENT = each.value != "" ? each.value : "no_comment_provided"
  }
}

output "ipset-rules" {
  value = data.template_file.ipset-wrapper
}
$ terraform apply
data.template_file.ipset-wrapper["10.0.0.0/32"]: Refreshing state...
data.template_file.ipset-wrapper["10.0.0.1/32"]: Refreshing state...
data.template_file.ipset-wrapper["10.0.0.2/32"]: Refreshing state...
data.template_file.ipset-wrapper[""]: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

ipset-rules = {
  "" = {
    "id" = "a30b544d33f455799f3b6d105d31eee10f326869f483ce22fd0300a56dd18c70"
    "rendered" = "no_ip comment no_comment_provided\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "no_comment_provided"
      "SUBNET_RANGE" = "no_ip"
    }
  }
  "10.0.0.0/32" = {
    "id" = "21372ccabc2941e632a3e7fc8db7629c743689fb8318b8219135468366edd21b"
    "rendered" = "10.0.0.0/32 comment us-east-1-s3\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "us-east-1-s3"
      "SUBNET_RANGE" = "10.0.0.0/32"
    }
  }
  "10.0.0.1/32" = {
    "id" = "87ab002bf580b46c48d580ed613f84c8730c6c238b087c59d6e876ff4838b6f8"
    "rendered" = "10.0.0.1/32 comment us-east-2-s3\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "us-east-2-s3"
      "SUBNET_RANGE" = "10.0.0.1/32"
    }
  }
  "10.0.0.2/32" = {
    "id" = "eaf64bde14c2d9e6921f6cb8f8297cf3335779450cddc734e9cb7f84849d742a"
    "rendered" = "10.0.0.2/32 comment no_comment_provided\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "no_comment_provided"
      "SUBNET_RANGE" = "10.0.0.2/32"
    }
  }
}