Terraform 如何将动态数量的数据磁盘添加到 vSphere VM
Terraform How to add Dyanmic number of DATA disks to vSphere VMs
我正在尝试创建具有动态磁盘数的 vSphere VM。从资源块不知道如何迭代数据磁盘列表的数量。
这是我的代码:
varible.tf
variable "virtual_machines" {
type = map(object({
system_cores = number
system_cores_per_socket = number
system_memory = number
system_ipv4_address = string
system_ipv4_netmask = string
system_ipv4_gateway = string
system_domainname = string
disks = list(number)
}))
}
values.tfvar
virtual_machines = {
server1 = {
system_cores = 4
system_cores_per_socket = 2
system_memory = 4096
system_disk1_size = 110
disks = [ 10, 20]
}
server2 = {
system_cores = 4
system_cores_per_socket = 2
system_memory = 4096
system_disk1_size = 110
disks = [ 50, 20,80]
}
}
resource.tf
resource "vsphere_virtual_machine" "server" {
for_each = var.virtual_machines
# VM-Name
name = each.key
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
# CPU
num_cpus = each.value.system_cores
num_cores_per_socket = each.value.system_cores_per_socket
# Memory
memory = each.value.system_memory
memory_hot_add_enabled = true
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
}
# Root Disk
disk {
label = "disk0"
size = data.vsphere_virtual_machine.template.disks.0.size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
unit_number = 0
}
# Data Disk need to loop based on disks list
disk {
label = "disk1"
unit_number = 1
size = each.value.system_disk1_size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
}
# Template clone and OS settings
clone {
template_uuid = data.vsphere_virtual_machine.template.id
customize {
timeout = 15
linux_options {
host_name = each.key
domain = each.value.system_domainname
}
network_interface {
ipv4_address = each.value.system_ipv4_address
ipv4_netmask = each.value.system_ipv4_netmask
}
ipv4_gateway = each.value.system_ipv4_gateway
dns_suffix_list = var.vm-dns-suffix-list
dns_server_list = var.vm_dns_servers
}
}
}
是否可以帮助在此资源块上添加磁盘列表?
谢谢
您可以使用 dynamic blocks:
# Root Disk
disk {
label = "disk0"
size = data.vsphere_virtual_machine.template.disks.0.size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
unit_number = 0
}
dynamic "disk" {
for_each = {for idx, size in each.value.disks: idx=>size}
content {
label = "disk${disk.key+1}"
unit_number = idx + 1
size = disk.value
# not sure if these must be changed or not:
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
}
}
我正在尝试创建具有动态磁盘数的 vSphere VM。从资源块不知道如何迭代数据磁盘列表的数量。
这是我的代码:
varible.tf
variable "virtual_machines" {
type = map(object({
system_cores = number
system_cores_per_socket = number
system_memory = number
system_ipv4_address = string
system_ipv4_netmask = string
system_ipv4_gateway = string
system_domainname = string
disks = list(number)
}))
}
values.tfvar
virtual_machines = {
server1 = {
system_cores = 4
system_cores_per_socket = 2
system_memory = 4096
system_disk1_size = 110
disks = [ 10, 20]
}
server2 = {
system_cores = 4
system_cores_per_socket = 2
system_memory = 4096
system_disk1_size = 110
disks = [ 50, 20,80]
}
}
resource.tf
resource "vsphere_virtual_machine" "server" {
for_each = var.virtual_machines
# VM-Name
name = each.key
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
# CPU
num_cpus = each.value.system_cores
num_cores_per_socket = each.value.system_cores_per_socket
# Memory
memory = each.value.system_memory
memory_hot_add_enabled = true
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
}
# Root Disk
disk {
label = "disk0"
size = data.vsphere_virtual_machine.template.disks.0.size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
unit_number = 0
}
# Data Disk need to loop based on disks list
disk {
label = "disk1"
unit_number = 1
size = each.value.system_disk1_size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
}
# Template clone and OS settings
clone {
template_uuid = data.vsphere_virtual_machine.template.id
customize {
timeout = 15
linux_options {
host_name = each.key
domain = each.value.system_domainname
}
network_interface {
ipv4_address = each.value.system_ipv4_address
ipv4_netmask = each.value.system_ipv4_netmask
}
ipv4_gateway = each.value.system_ipv4_gateway
dns_suffix_list = var.vm-dns-suffix-list
dns_server_list = var.vm_dns_servers
}
}
}
是否可以帮助在此资源块上添加磁盘列表?
谢谢
您可以使用 dynamic blocks:
# Root Disk
disk {
label = "disk0"
size = data.vsphere_virtual_machine.template.disks.0.size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
unit_number = 0
}
dynamic "disk" {
for_each = {for idx, size in each.value.disks: idx=>size}
content {
label = "disk${disk.key+1}"
unit_number = idx + 1
size = disk.value
# not sure if these must be changed or not:
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
}
}