2

我编写了一个 terraform 模块(v0.14),可用于在 openstack 中配置多个实例,具有正确的可用性区域、网络端口、正确的风格,可以基于布尔输入变量创建 1 个本地磁盘或 1 个 blockstorage_volume 等等。现在我得到了一个功能请求,能够在多个实例上动态添加多个 blockstorage_volumes(=network/shared storage volumes)。

多个实例上的 1 个 blockstorage_volume 如何动态工作的示例:

#Dynamically create this resource ONLY if boolean "volume_storage" = true
resource "openstack_blockstorage_volume_v3" "shared_storage" {
  for_each          = var.volume_storage ? var.nodes : {}
  name              = "${each.value}-${each.key}.${var.domain}"
  size              = var.volume_s
  availability_zone = each.key
  volume_type       = var.volume_type
  image_id          = var.os_image
}
resource "openstack_compute_instance_v2" "instance" {
  for_each          = var.nodes
  name              = "${each.value}-${each.key}.${var.domain}"
  flavor_name       = var.flavor
  availability_zone = each.key
  key_pair          = var.key_pair
  image_id     = (var.volume_storage == true ? "" : var.os_image)
  config_drive = true
  #Dynamically create this parameter ONLY if boolean "volume_storage" = true
  dynamic "block_device" {
    for_each = var.volume_storage ? var.volume : {}
    content {
      uuid                  = openstack_blockstorage_volume_v3.shared_storage[each.key].id
      source_type           = "volume"
      destination_type      = "volume"
      delete_on_termination = var.volume_delete_termination
    }
  }
  user_data = data.template_file.cloud-init[each.key].rendered
  scheduler_hints {
    group = openstack_compute_servergroup_v2.servergroup.id
  }
  network {
    port = openstack_networking_port_v2.network-port[each.key].id
  }
}

所以现在假设我有 2 个实例,我想在每个实例中动态添加 2 个额外的 blockstorage_volumes,我的第一个想法是添加 2 个额外的动态资源作为试用:

#Dynamically create this resource if boolean "multiple_volume_storage" = true
resource "openstack_blockstorage_volume_v3" "multiple_shared_storage" {
  for_each          = var.multiple_volume_storage ? var.multiple_volumes : {}
  name              = each.value
  size              = var.volume_s
  availability_zone = each.key
  volume_type       = var.volume_type
  image_id          = var.os_image
}

.tf 文件中定义的 2 个额外 blockstorage_volumes 示例:

variable "multiple_volumes" {
  type = map(any)
  default = {
    dc1 = "/volume/mysql"
    dc2 = "/volume/postgres"

  }
}

.tf 文件中定义的 2 个实例的示例:

  nodes = {
    dc1 = "app-stage"
    dc2 = "app-stage"
  }

在这里,我尝试为每个实例动态附加 2 个额外的 blockstorage_volumes:

resource "openstack_compute_volume_attach_v2" "attach_multiple_shared_storage" {
  for_each  = var.multiple_volume_storage ? var.multiple_volumes : {}
  instance_id = openstack_compute_instance_v2.instance[each.key].id
  volume_id   = openstack_blockstorage_volume_v3.multiple_shared_storage[each.key].id
}

openstack_compute_instance_v2.instance [each.key]显然是不正确的,因为它现在只为每个实例创建 1 个额外的 blockstorage_volume。有没有一种干净/优雅的方法来解决这个问题?所以基本上将变量“multiple_volumes”中的所有给定卷附加到 var.nodes 中定义的每个单个实例

亲切的问候,乔纳斯

4

0 回答 0