【问题标题】:spin multiple instances of different images using terraform on openstack在 openstack 上使用 terraform 旋转不同图像的多个实例
【发布时间】:2020-05-26 22:12:12
【问题描述】:

我一直在尝试使用 terraform 启动不同图像的多个实例,但到目前为止还没有运气。我可以使用count 旋转单个图像的多个实例。在使用for_each 旋转不同的图像时,我无法获得正确的配置。

我有以下变量 .tf 配置文件。

variable "images" {
  type = map
  default = {
    "rhel-8-factory-os-ready" = {
       "az_zone" = "eu-fra-1ah"
       "ins_count" = 2
     }
    "rhel-7-factory-os-ready" = {
       "az_zone" = "eu-fra-1ai"
       "ins_count" = 2
    }
  }
}

resource "openstack_compute_instance_v2" "instance" {
   for_each = var.images
   flavor_id = var.flavor
   image_name = each.key
   name = "${var.image_name}-${lower(random_id.random-instance.hex)}"
   availability_zone = each.value.az_zone
   security_groups = var.security_group
   key_pair = "fop-mgt-key"
   network {
     name = var.network_name
   }
}

resource "random_id" "random-instance" {
  byte_length = 4
}

resource "random_id" "random-number" {
  byte_length = 8
}

resource "openstack_networking_floatingip_v2" "fip" {
  count = length(var.images)
  pool = var.floatingip_pool
}

resource "openstack_compute_floatingip_associate_v2" "fip" {

  depends_on = [openstack_compute_instance_v2.instance]

  count = length(var.images)
  floating_ip = openstack_networking_floatingip_v2.fip[count.index].address
  instance_id = openstack_compute_instance_v2.instance.*.id[count.index]
  fixed_ip    = openstack_compute_instance_v2.instance.*.network.0.fixed_ip_v4[count.index]
}

terraform {
  backend "artifactory" {}
}

data "terraform_remote_state" "foo" {
  backend = "artifactory"
  config = {
    repo = "${var.repo}"
    subpath = "${var.subpath}"
  }
}

我看到以下错误消息::

错误:不支持的属性

在 tf-backend-fra/main.tf 第 49 行,在资源中 “openstack_compute_floatingip_associate_v2”“fip”:49:instance_id = openstack_compute_instance_v2.instance.*.id[count.index]

此对象没有名为“id”的属性。

我不确定如何使用for_each 完成它。 Terraform 版本是 0.12.17。 非常感谢您的帮助。

谢谢, 哈沙

【问题讨论】:

    标签: terraform terraform-provider-openstack


    【解决方案1】:

    不知何故设法用lookupelement 解决,如下所示。

    resource "openstack_compute_instance_v2" "instance" {
       for_each = var.images
       flavor_id = each.value.flavor
       image_id = lookup(data.openstack_images_image_v2.os-image, each.key).id
       name = "${var.image_name}-${lower(random_id.random-instance.hex)}"
       availability_zone = each.value.az_zone
       security_groups = var.security_group
       key_pair = "fop-mgt-key"
       network {
         name = var.network_name
       }
    }
    
    resource "openstack_compute_floatingip_associate_v2" "fip" {
      for_each = var.images
      floating_ip = element(openstack_networking_floatingip_v2.fip, index(tolist(keys(var.images)), each.key)).address
      instance_id = lookup(openstack_compute_instance_v2.instance, each.key).id
      fixed_ip    = lookup(openstack_compute_instance_v2.instance, each.key).network.0.fixed_ip_v4
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多