pve-server-tf/main.tf
Robert Kaussow ef0b91ae3d
All checks were successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build-package Pipeline was successful
ci/woodpecker/tag/test Pipeline was successful
ci/woodpecker/tag/build-package Pipeline was successful
fix: use try to set default for server list (#7)
Reviewed-on: #7
Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
Co-committed-by: Robert Kaussow <mail@thegeeklab.de>
2024-04-11 15:52:04 +02:00

120 lines
3.4 KiB
HCL

locals {
ipv4_addresses = {
for server_key, server in var.server : server.name => flatten([
for k, v in try(proxmox_virtual_environment_vm.server[server.name].ipv4_addresses, []) :
v if length(regexall(try(server.netif_filter, var.server_netif_filter), proxmox_virtual_environment_vm.server[server.name].network_interface_names[k])) > 0
])
}
}
locals {
ipv6_addresses = {
for server_key, server in var.server : server.name => flatten([
for k, v in try(proxmox_virtual_environment_vm.server[server.name].ipv6_addresses, []) :
v if length(regexall(try(server.netif_filter, var.server_netif_filter), proxmox_virtual_environment_vm.server[server.name].network_interface_names[k])) > 0
])
}
}
resource "proxmox_virtual_environment_vm" "server" {
for_each = { for row in var.server : row.name => row }
name = "${each.value.name}.${try(each.value.dns_zone, var.server_dns_zone)}"
description = try(each.value.description, null)
tags = concat(
[
"provisioner_terraform",
"os_${each.value.os}",
"zone_${each.value.zone}"
],
[for service in try(each.value.services, []) : "service_${service}"],
[for type in try(each.value.types, []) : "type_${type}"],
try(each.value.labels, [])
)
node_name = var.pve_node_name
vm_id = each.value.vm_id
on_boot = try(each.value.on_boot, true)
reboot = try(each.value.reboot, false)
scsi_hardware = "virtio-scsi-pci"
keyboard_layout = try(each.value.keyboard_layout, var.server_keyboard_layout)
agent {
enabled = try(each.value.guest_agent, var.server_guest_agent)
}
cpu {
type = try(each.value.cpu_type, "x86-64-v2-AES")
cores = try(each.value.cpu_cores, 1)
sockets = try(each.value.cpu_sockets, 1)
}
memory {
dedicated = try(each.value.memory, 1024)
floating = try(each.value.balloon, true) ? try(each.value.memory, 1024) : 0
}
dynamic "disk" {
for_each = concat(
[{ size = 20, backup = true }],
try(each.value.volumes, [])
)
content {
interface = "scsi${disk.key}"
datastore_id = try(disk.value.datastore_id, var.server_datastore_id)
path_in_datastore = "vm-${each.value.vm_id}-disk-${disk.key}"
size = disk.value.size
file_format = "raw"
cache = "none"
ssd = try(disk.value.ssd, false)
discard = "on"
}
}
network_device {
model = "virtio"
bridge = try(each.value.networks[0].bridge, var.server_network_bridge)
vlan_id = try(each.value.networks[0].vlan_id, null)
}
operating_system {
type = "l26"
}
clone {
vm_id = each.value.clone_vm_id
full = try(each.value.clone_full, true)
}
initialization {
datastore_id = try(each.value.datastore_id, var.server_datastore_id)
ip_config {
ipv4 {
address = try(each.value.networks[0].ipv4_address, "dhcp")
gateway = try(each.value.networks[0].ipv4_gateway, null)
}
}
user_account {
keys = var.pve_ssh_keys
username = "root"
}
}
}
resource "restapi_object" "ucs_server" {
for_each = { for row in var.server : row.name => row }
path = "/dns/host_record/"
data = jsonencode({
"position" : var.ucs_zones[try(each.value.dns_zone, var.server_dns_zone)],
"properties" : {
"name" : each.value.name,
"a" : concat(local.ipv4_addresses[each.key]),
}
})
}