需要使用terraform通过动态变量将PCI设备添加到vSphere。
任务是编写一个 terraform 清单以支持添加 PCI 设备,并创建一台未添加 PCI 设备的机器。
以下清单是根据文档中的信息编写的。
目前的解决方案是在
main.tf
中的manifest中写入:
resource "vsphere_virtual_machine" "vmware_vm" {
...
dynamic "vsphere_host_pci_device" {
for_each = var.pci_device
content {
host_id = data.vsphere_host.host.id
name_regex = data.pci_device.value.pci_device_name
vendor_id = data.pci_device.value.vendor_id
class_id = data.pci_device.value.class_id
}
...
}
在
terraform.tf
...
data "vsphere_host_pci_device" "dev" {
count = length(var.pci_device)
host_id = data.vsphere_host.host.id
name_regex = var.pci_device[count.index].pci_device_name
vendor_id = var.pci_device[count.index].vendor_id
class_id = var.pci_device[count.index].class_id
}
...
variables.tf
...
variable "pci_device" {
description = "PCI device block used to configure PCI devices"
type = list(any)
default = []
}
variable "pci_device_name" {
description = "PCI device name"
type = string
default = null
}
variable "vendor_id" {
description = "Define vendor ID (without '0x')"
type = string
default = null
}
variable "class_id" {
description = "Define device class ID (without '0x')"
type = string
default = null
}
...
运行时
terraform validate
出现这样的错误:
│ Error: Unsupported block type
│
│ on ../../../../infrastructure-modules/vmware_vm/main.tf line 37, in resource "vsphere_virtual_machine" "vmware_vm":
│ 37: dynamic "vsphere_host_pci_device" {
│
│ Blocks of type "vsphere_host_pci_device" are not expected here.
机器清单的main.tf
...
pci_device = [
{
pci_device_name = "GP107GL"
vendor_id = "10de"
class_id = "0300"
}
]
...
什么是正确的解决方案,可以将 PCI 设备添加到此清单中,以便 vSphere 提供商正常工作? 还需要不定义任何或多个 PCI 设备的选项。
vsphere_virtual_machine
资源没有vsphere_host_pci_device
参数。这就是您收到错误消息的原因。
pci_device_id
参数来指定 PCI 设备 ID 列表。由于它是一个列表,因此您不需要动态块。
vsphere_host_pci_device
数据源获取 PCI 设备 ID。