我正在使用terraform在openstack中创建几个实例,我想在没有任何手动干预的情况下自动为它们分配浮动ip地址。
我的.tf文件如下:
resource "openstack_networking_floatingip_v2" "floating-ip" {
count = 4
pool = "floating-ip-pool"
}
resource "openstack_compute_floatingip_associate_v2" "fip-associate" {
floating_ip = openstack_networking_floatingip_v2.floating-ip.address[count.0]
instance_id = openstack_compute_instance_v2.terraform-vm.id[count.0]
}`
我收到一个错误
"错误:缺少资源实例密钥
在image-provision.tf第33行,在资源"openstack_compute_floatingip_associate_v2"fip-associate"中:33:instance_id=openstack_compute_instance_v2.terraform vm.id[count.0]">
我的地形版本是:terraform v0.12.24+provider.openstack 1.26.0
能够在地形中使用for_each选项进行解析:
resource "openstack_compute_instance_v2" "terraform_vm" {
image_id = "f8b9189d-2518-4a32-b1ba-2046ea8d47fd"
for_each = var.instance_name
name = each.key
flavor_id = "3"
key_pair = "openstack vm key"
security_groups = ["default"]
network {
name = "webapps-network"
}
}
resource "openstack_networking_floatingip_v2" "floating_ip" {
pool = "floating-ip-pool"
for_each = var.instance_name
}
resource "openstack_compute_floatingip_associate_v2" "fip_associate" {
for_each = var.instance_name
floating_ip = openstack_networking_floatingip_v2.floating_ip[each.key].address
instance_id = openstack_compute_instance_v2.terraform_vm[each.key].id
}
您需要在关联中使用port_id
。可以使用计算实例IPv4地址查找:
resource "openstack_networking_floatingip_v2" "floating-ip" {
# count = 4
pool = "floating-ip-pool"
}
data "openstack_networking_port_v2" "terraform-vm" {
fixed_ip = openstack_compute_instance_v2.terraform-vm.access_ip_v4
}
resource "openstack_compute_floatingip_associate_v2" "fip-associate" {
floating_ip = openstack_networking_floatingip_v2.floating-ip.address
port_id = data.openstack_networking_port_v2.terraform-vm.id
}