我希望你能帮助我。
我已经使用 hasicorp/vsphere 提供商设置了 Terraform 管道,并且正在尝试从 tfvars 文件创建具有 2 个 NIC 的虚拟机。我正在尝试从 vsphere_virtual_machine 资源中创建一个模块,但我得到的只是错误
我的 tfvars 文件格式如下
Terraform\VMWare\NewBuildCluster\vmware_server_build.tfvars
tfvars = {
# Core vSphere Vars
vsphere_server = "vcentre.example.com" # Address of vCentre
vsphere_datacenter = "vsphere_datacenter" # Location to build new servers
vsphere_datastore = "datastore0001" # Data store to save VM files
vsphere_cluster = "NewBuildCluster" # Cluster to build on
#VM Related Vars
vm = {
TestVM01 = {
vsphere_vm_template = "WindowsTemplate2022" # Name of template
vsphere_vm_name = "TestVM01" # Name of VM
vsphere_vm_cpu_cores = 4 # Total cores
vsphere_vm_num_cores_per_socket = 2 # Cores per socket
vsphere_vm_memory = 8 # RAM assignment (Math conducted in module for MB conversion)
#VM Customisation Variables
vm_nics = {
eth0 = {
vsphere_vm_network = "VLAN10" # Name of Virtual Network (defined in vSphere)
vsphere_vm_domain = "ad.example.com" # Domain to join server to once built
vsphere_vm_ip = "10.200.10.240" # Static assigned IP address
vsphere_vm_ip_gateway = "10.200.10.1" # Gateway IP Address
vsphere_vm_ip_dnslist = ["10.200.10.10", "10.200.10.30"] # List of DNS addresses
},
eth1 = {
vsphere_vm_network = "VLAN10" # Name of Virtual Network (defined in vSphere)
vsphere_vm_domain = "ad.example.com" # Domain to join server to once built
vsphere_vm_ip = "10.200.10.241" # Static assigned IP address
vsphere_vm_ip_gateway = "10.200.10.1" # Gateway IP Address
vsphere_vm_ip_dnslist = ["10.200.10.10", "10.200.10.30"] # List of DNS addresses
}
}
data_disk = {
disk1 = {
size_gb = 50 # Data disk size
},
disk2 = {
size_gb = 10 # Data disk size
}
}
}
}
我的网络数据块是这样的,但我无法访问整个vm_nics。
Pipeline\VMWare\main.tf
data "vsphere_virtual_machine" "network" {
for_each = flatten([
for vm_key, vm_value in var.tfvars.vm : [
for nic_key, nic_value in vm_value.vm_nics : {
name = nic_value.vsphere_vm_network
datacenter_id = data.vsphere_datacenter.datacenter.id
}
]
])
name = each.value.name
datacenter_id = each.value.datacenter_id
}
这使我尽可能接近变量,但我收到此错误
│ The given "for_each" argument value is unsuitable: the "for_each" argument
│ must be a map, or set of strings, and you have provided a value of type
│ tuple.
是的,for_each 参数不正确...
我们可以提供一组字符串(使用
toset
)并将其更改为:
for_each = toset(flatten([
for vm_key, vm_value in var.tfvars.vm : [
for nic_key, nic_value in vm_value.vm_nics : nic_value.vsphere_vm_network
]
]))
我认为没有必要包括
datacenter_id = data.vsphere_datacenter.datacenter.id
这是一个完整的例子
variable "tfvars" {
type = any
default = {
vm = {
TestVM01 = {
vm_nics = {
eth0 = {
vsphere_vm_network = "VLAN10"
},
eth1 = {
vsphere_vm_network = "VLAN10"
}
}
}
}
}
}
resource "null_resource" "tion" {
for_each = toset(flatten([
for vm_key, vm_value in var.tfvars.vm : [
for nic_key, nic_value in vm_value.vm_nics : nic_value.vsphere_vm_network
]
]))
provisioner "local-exec" {
when = create
command = "echo ${each.value.name}"
}
}
地形计划将如下所示:
Terraform will perform the following actions:
# null_resource.tion["VLAN10"] will be created
+ resource "null_resource" "tion" {
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.