将null_data_source输出输出为普通地图,而不是terraform中的地图数组

时间:2019-02-05 02:21:42

标签: terraform

Terraform v0.11.11
+ provider.null v2.0.0

在此处输入代码

variable "vpc1" {
  type = "list"

  default = [
    "name1:ip1:az1:dedicated",
    "name2:ip2:az2:dedicated",
  ]
}

variable vpc2 {
  type = "list"

  default = [
    "foo:10.1:east:yes",
    "bar:10.2:west:no",
  ]
}

data "null_data_source" "values" {
  count = "${length(var.vpc1)}"

  inputs = "${
          map (

                "${element(split(":",var.vpc1[count.index]),0)}",
               "${element(split(":", var.vpc2[count.index]),0)}"
              )
        }"
}

output "mappings" {
  value = "${data.null_data_source.values.*.outputs}"
}

如何获得所需的输出,我缺少什么

1 个答案:

答案 0 :(得分:1)

您需要flatten()

的功能
  output  "mappings" {
    value = "${flatten(data.null_data_source.values.*.outputs)}"
  }

通过功能concat()

可以轻松获得所需的内容
variable "vpc1" {
  type = "list"

  default = [
    "name1:ip1:az1:dedicated",
    "name2:ip2:az2:dedicated",
  ]
}

variable vpc2 {
  type = "list"

  default = [
    "foo:10.1:east:yes",
    "bar:10.2:west:no",
  ]
}

output "mappings" {
  value = "${flatten(concat(var.vpc1, var.vpc2))}"
}

这是输出

$ terraform apply

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

Outputs:

mappings = [
    name1:ip1:az1:dedicated,
    name2:ip2:az2:dedicated,
    foo:10.1:east:yes,
    bar:10.2:west:no
]

参考:

https://www.terraform.io/docs/configuration/interpolation.html#flatten-list-of-lists-

https://www.terraform.io/docs/configuration/interpolation.html#concat-list1-list2-