network/terraform/node.tf

113 lines
2.6 KiB
Terraform
Raw Normal View History

2024-11-02 18:53:21 +00:00
locals {
domain = "network.tjo.cloud"
nodes = {
for k, v in var.nodes : k => merge(v, {
domain = local.domain
id = 700 + index(keys(var.nodes), k)
hash = sha1(v.host)
2024-11-03 10:28:09 +00:00
wan_mac_address = v.mac_address != null ? v.mac_address : "AA:BB:00:00:${format("%v:%v", substr(sha1(v.host), 0, 2), substr(sha1(v.host), 2, 2))}"
private_mac_address = "AA:BB:00:11:${format("%v:%v", substr(sha1(v.host), 0, 2), substr(sha1(v.host), 2, 2))}"
2024-11-02 18:53:21 +00:00
})
}
}
resource "proxmox_virtual_environment_network_linux_bridge" "vmbr0" {
for_each = local.nodes
node_name = each.value.host
name = "vmbr0"
comment = "Main interface bridge for VMs."
address = each.value.address
gateway = each.value.gateway
ports = each.value.bridge_ports
}
resource "proxmox_virtual_environment_network_linux_bridge" "vmbr1" {
for_each = local.nodes
node_name = each.value.host
name = "vmbr1"
comment = "Private network for VMs."
}
resource "proxmox_virtual_environment_file" "iso" {
for_each = local.nodes
content_type = "iso"
datastore_id = each.value.iso_storage
node_name = each.value.host
source_file {
2024-11-03 17:43:15 +00:00
path = "${path.module}/../iso/openwrt-23.05.5-x86-64-generic-ext4-combined-efi.img"
2024-11-02 18:53:21 +00:00
}
}
resource "proxmox_virtual_environment_vm" "nodes" {
for_each = local.nodes
vm_id = each.value.id
name = "${each.value.host}.${each.value.domain}"
node_name = each.value.host
description = <<EOT
An network.tjo.cloud instance for ${each.value.host}.
Repo: https://code.tjo.space/tjo-cloud/network
EOT
tags = [each.value.domain]
2024-11-02 18:53:21 +00:00
stop_on_destroy = true
timeout_start_vm = 60
timeout_stop_vm = 60
timeout_shutdown_vm = 60
timeout_reboot = 60
timeout_create = 600
cpu {
cores = each.value.cores
type = "host"
}
memory {
dedicated = each.value.memory
}
bios = "ovmf"
efi_disk {
datastore_id = each.value.boot_storage
}
operating_system {
type = "l26"
}
agent {
enabled = false
}
network_device {
2024-11-03 17:43:15 +00:00
bridge = proxmox_virtual_environment_network_linux_bridge.vmbr0[each.key].name
2024-11-02 18:53:21 +00:00
mac_address = each.value.wan_mac_address
}
network_device {
bridge = proxmox_virtual_environment_network_linux_bridge.vmbr1[each.key].name
mac_address = each.value.private_mac_address
}
scsi_hardware = "virtio-scsi-single"
disk {
2024-11-03 17:43:15 +00:00
file_id = proxmox_virtual_environment_file.iso[each.key].id
2024-11-02 18:53:21 +00:00
interface = "scsi0"
datastore_id = each.value.boot_storage
2024-11-03 17:43:15 +00:00
size = 8
2024-11-02 18:53:21 +00:00
backup = true
iothread = true
file_format = "raw"
}
}