Make VMs dynamic with for_each map — spin up/down any number by name
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
b8ce272610
commit
dad5376064
+12
-7
@@ -1,7 +1,9 @@
|
||||
resource "proxmox_virtual_environment_vm" "vm" {
|
||||
name = var.vm_name
|
||||
for_each = var.vms
|
||||
|
||||
name = each.key
|
||||
node_name = "proxmox"
|
||||
vm_id = var.vm_id
|
||||
# vm_id omitted — Proxmox auto-assigns the next available ID
|
||||
|
||||
clone {
|
||||
vm_id = 9001 # debian-12-cloud template
|
||||
@@ -13,12 +15,12 @@ resource "proxmox_virtual_environment_vm" "vm" {
|
||||
}
|
||||
|
||||
cpu {
|
||||
cores = var.vm_cores
|
||||
cores = each.value.cores
|
||||
type = "x86-64-v2-AES"
|
||||
}
|
||||
|
||||
memory {
|
||||
dedicated = var.vm_memory
|
||||
dedicated = each.value.memory
|
||||
}
|
||||
|
||||
scsi_hardware = "virtio-scsi-single"
|
||||
@@ -26,7 +28,7 @@ resource "proxmox_virtual_environment_vm" "vm" {
|
||||
disk {
|
||||
datastore_id = "local-lvm"
|
||||
interface = "scsi0"
|
||||
size = var.vm_disk_size
|
||||
size = each.value.disk_size
|
||||
discard = "on"
|
||||
iothread = true
|
||||
}
|
||||
@@ -62,6 +64,9 @@ resource "proxmox_virtual_environment_vm" "vm" {
|
||||
}
|
||||
}
|
||||
|
||||
output "vm_ipv4_address" {
|
||||
value = proxmox_virtual_environment_vm.vm.ipv4_addresses
|
||||
output "vm_ip_addresses" {
|
||||
value = {
|
||||
for name, vm in proxmox_virtual_environment_vm.vm :
|
||||
name => [for ip in flatten(vm.ipv4_addresses) : ip if ip != "127.0.0.1"]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user