Skip to content

Commit

Permalink
Create VM resources in Netbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Feb 3, 2024
1 parent a2da0d6 commit c5cd87f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
6 changes: 6 additions & 0 deletions terraform/modules/supernode/management-ipv6.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ resource "netbox_ip_address" "management_ipv6" {

description = "Management Address ${var.supernode_name}"

virtual_machine_interface_id = netbox_interface.eth0.id

tags = toset(var.tags)
}

resource "netbox_primary_ip" "supernode" {
ip_address_id = netbox_ip_address.management_ipv6.id
virtual_machine_id = netbox_virtual_machine.supernode.id
}
12 changes: 12 additions & 0 deletions terraform/modules/supernode/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ variable "vm_ssh_keys" {
type = list(string)
description = "Public keys to grant access to"
}

variable "vm_cluster_name" {
type = string
description = "Cluster to assign the VM to"
default = "pve1"
}

variable "vm_role_name" {
type = string
description = "Name of an existing Netbox device role to assign to the VM"
default = "Supernode v2"
}
56 changes: 55 additions & 1 deletion terraform/modules/supernode/vm.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
locals {
vm_name = "supernode-${var.supernode_name}"
}

resource "proxmox_vm_qemu" "supernode" {
name = "supernode-${var.supernode_name}"
name = local.vm_name
target_node = var.vm_target_node
pool = var.vm_resource_pool
desc = "Supernode v2 - ${var.supernode_name}"
Expand Down Expand Up @@ -52,3 +56,53 @@ resource "proxmox_vm_qemu" "supernode" {
]
}
}

data "netbox_cluster" "vm_cluster" {
name = var.vm_cluster_name
}

data "netbox_device_role" "supernode" {
name = var.vm_role_name
}

locals {
size_constants = {
G = 1024 * 1024 * 1024
M = 1024 * 1024
K = 1024
}
disk_size_parts = regex("^([0-9]+)([GMK])$", proxmox_vm_qemu.supernode.disk[0].size)
disk_size_bytes = parseint(local.disk_size_parts[0], 10) * local.size_constants[local.disk_size_parts[1]]
}

resource "netbox_virtual_machine" "supernode" {
cluster_id = data.netbox_cluster.vm_cluster.id
name = local.vm_name
status = "staged"
role_id = data.netbox_device_role.supernode.id

vcpus = proxmox_vm_qemu.supernode.cores
memory_mb = proxmox_vm_qemu.supernode.memory
disk_size_gb = local.disk_size_bytes / local.size_constants.G

custom_fields = {
proxmox_vm_id = proxmox_vm_qemu.supernode.vmid
}

tags = toset(var.tags)

lifecycle {
ignore_changes = [
status,
]
}
}

resource "netbox_interface" "eth0" {
virtual_machine_id = netbox_virtual_machine.supernode.id

name = "eth0"
mac_address = proxmox_vm_qemu.supernode.network[0].macaddr

tags = toset(var.tags)
}

0 comments on commit c5cd87f

Please sign in to comment.