我正在使用 Terraform 配置具有 Cinder 块设备附件的虚拟机集群。问题是,当我重放 Terraform 或仅扩展虚拟机数量时,Terraform 计划更新现有虚拟机的块设备附件(这意味着删除附件资源然后创建)。
resource "openstack_compute_volume_attach_v2" "worker-hd1" {
count = "${var.worker_count}"
volume_id = "${element(openstack_blockstorage_volume_v2.hdd1_volume.*.id,count.index)}"
instance_id = "${element(openstack_compute_instance_v2.worker_node.*.id,count.index)}"
}
Terraform 计划的输出。第一个附件 ([2]) 是现有的,第二个附件 ([3]) 是用于新的 VM/块
-/+ openstack_compute_volume_attach_v2.worker-hd2[2] (new resource required)
id: "2310c2aa-bfbf-4135-a73a-972748578613/f8a1964a-6589-41e9-9bc4-a44bcc865c97" => <computed>
device: "/dev/vde" => <computed>
instance_id: "2310c2aa-bfbf-4135-a73a-972748578613" => "${element(openstack_compute_instance_v2.worker_node.*.id,count.index)}"
region: "fr1" => <computed>
volume_id: "f8a1964a-6589-41e9-9bc4-a44bcc865c97" => "${element(openstack_blockstorage_volume_v2.hdd2_volume.*.id,count.index)}"
+ openstack_compute_volume_attach_v2.worker-hd2[3]
id: <computed>
device: <computed>
instance_id: "${element(openstack_compute_instance_v2.worker_node.*.id,count.index)}"
region: <computed>
volume_id: "${element(openstack_blockstorage_volume_v2.hdd2_volume.*.id,count.index)}"
如何让 Terraform 不改变现有的附件?
答案1
看来您的资源依赖于变量,这就是为什么 terraform 将其视为新资源。但您始终可以忽略其中任何一个以防止重新创建:
resource "openstack_compute_volume_attach_v2" "worker-hd1" {
count = "${var.worker_count}"
volume_id = "${element(openstack_blockstorage_volume_v2.hdd1_volume.*.id,count.index)}"
instance_id = "${element(openstack_compute_instance_v2.worker_node.*.id,count.index)}"
lifecycle {
ignore_changes = ["volume_id","instance_id"]
}
}