【问题标题】:Create resources iterating through the values of a map rather key - terraform创建资源迭代地图的值而不是关键 - terraform
【发布时间】:2020-02-18 20:06:02
【问题描述】:

我不知道如何循环遍历下面地图的值之和。

variable "images" {
  default = {
    "rhel-8-factory-os-ready" = {
       "availability_zone" = "eu-fra-1ah"
       "flavor" = 4
       "instance_count" = 2
       "image_name" = "rhel-8-factory-os-ready"
    },
    "rhel-7-factory-os-ready" = {
       "availability_zone" = "eu-fra-1ai"
       "instance_count" = 3
       "flavor" = 3
       "image_name" = "rhel-7-factory-os-ready"
    },
    "rhel-6-factory-os-ready" = {
       "availability_zone" = "eu-fra-1ah"
       "instance_count" = 3
       "flavor" = 3
       "image_name" = "rhel-6-factory-os-ready"
    }
  }
}

在这里,我要遍历所有键的instance_count 属性的总和,并根据instance_count 创建实例。

我可以使用以下内置函数计算instance_count 的总和。

locals {
  list_sum = length(flatten([for i in var.images: range(i["instance_count"])]))
}

如何遍历list_sum 变量并根据instance_count 创建资源?

我创建了以下列表以创建资源::

locals {
  list_images = tolist(keys(var.images))
  list_instance_count = [for i in var.images: i["instance_count"]]
  list_flavors = [for i in var.images: i["flavor"]]
  list_image_names = [for i in var.images: i["image_name"]]
  list_availability_zones = [for i in var.images: i["availability_zone"]]
}

我的资源::

resource "openstack_compute_instance_v2" "instance" {
  count = local.list_sum
  image_name = element(local.list_image_names, count.index +1 )
  flavor_id = element(local.list_flavors, (count.index + 1) )
  name = element(local.list_image_names, (count.index + 1) )
  security_groups = var.security_group
  availability_zone = element(local.list_availability_zones, (count.index + 1) )
  key_pair = "foptst"
  network {
    name = var.network_name
  }
}

到现在为止,您可能已经知道我的迭代是不正确的。我的资源块必须根据 instance_count 变量创建资源数量,即 rhel-8-factory-os-ready 的 2 个实例、rhel-7-factory-os-ready 的 3 个实例和 rhel-6-factory-os-ready 的 3 个实例。

由于循环不正确,我无法得到它。如果有人可以帮助我如何正确地迭代以按预期创建资源,那就太好了。

提前非常感谢, 哈沙

【问题讨论】:

  • 也许 Resource for_each 尚未发布的 Terraform 功能可以使用 (hashicorp.com/blog/…)?
  • terraform 0.12 已经发布,我正在使用它并且for_each 可用。我知道我的数据集很复杂但完全有效。我使用setproduct 来使用for_each,但它正在生成3*3 =9 资源,其中我只需要创建8 个
  • 我尝试使用dynamic,完全没用
  • 请仔细阅读。它说“很遗憾,我们无法在 Terraform 0.12 初始版本中完全完成此功能,但我们计划在后续版本中包含此功能”

标签: terraform


【解决方案1】:
locals {
  expanded_image_names = flatten([
    for key, value in var.images : [
      for counter in range(var.images[key]["instance_count"]): var.images[key]["image_name"]
    ]
  ])
}

output "expanded_image_names" {
  value = local.expanded_image_names
}

这将产生:

expanded_image_names = [
  "rhel-6-factory-os-ready",
  "rhel-6-factory-os-ready",
  "rhel-6-factory-os-ready",
  "rhel-7-factory-os-ready",
  "rhel-7-factory-os-ready",
  "rhel-7-factory-os-ready",
  "rhel-8-factory-os-ready",
  "rhel-8-factory-os-ready",
]

然后使用它们:

resource "openstack_compute_instance_v2" "instance" {
  count = local.list_sum
  image_name = element(local.expanded_image_names, count.index +1 )  # <-----
  ... # Do similar for the others.

  key_pair = "foptst"
  network {
    name = var.network_name
  }
}

【讨论】:

  • 非常感谢,这按预期创建了资源。但是,它使用count 有一个缺点。但是,那是我的选择。这只会在我增加/减少here 讨论的计数时影响。除非我使用正确的密钥,否则 terraform 不能保证按照我预期的方式进行更改。不过,非常感谢您的回答。
  • @harshavmb,你试过用 for_each 代替 count 吗? terraform.io/docs/configuration/…
猜你喜欢
  • 2021-11-06
  • 1970-01-01
  • 2021-09-08
  • 2023-02-16
  • 1970-01-01
  • 2021-07-10
  • 2020-04-28
  • 2021-05-22
  • 1970-01-01
相关资源
最近更新 更多