There is a need to add PCI devices to vSphere using terraform by dynamic variable.
The task is to write one terraform manifest to support adding PCI devices and also create a machine without PCI devices added.
The below manifest is written based on information found in documentation.
The solution at the moment is to write in manifest in main.tf
:
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
}
...
}
in 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
}
...
At the moment during running terraform validate
such an error occurs:
│ 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
of the machine manifest
...
pci_device = [
{
pci_device_name = "GP107GL"
vendor_id = "10de"
class_id = "0300"
}
]
...
What is the proper solution to enable adding PCI device future to this manifest to work properly for the vSphere provider? The option without defining any or multiple PCI devices is also needed.
The
vsphere_virtual_machine
resource does not have avsphere_host_pci_device
argument. That's why you get the error message.You can use the
pci_device_id
argument instead to specify a list of PCI device IDs. As it is a list, you do not need a dynamic block for that.You can get the PCI device IDs by using the
vsphere_host_pci_device
data source like you already do.