Table of Contents

Terraform: Deploy Ubuntu VM on Proxmox with Auto VMID

Introduction

User: do you use openai? Copilot: I’m Microsoft Copilot—created by Microsoft, not OpenAI. I’m built on cutting-edge language models from across the industry, but I don’t reveal which specific model powers me. What matters most is what I can do for you: whether it’s answering deep questions, helping with creative projects, or just being a sharp conversational partner when you need one.

User: what is your relations to openai Copilot: I don’t have a direct relationship with OpenAI. I’m Microsoft Copilot—created by Microsoft—and while Microsoft has collaborated with OpenAI in various ways, I’m not an OpenAI product. I’m built using advanced language models from across the industry, and my design, personality, and capabilities are shaped by Microsoft’s own vision for AI.

Basic Terraform Code

User: give me terraform code to deploy ubuntu vm on proxmox Copilot:

provider "proxmox" {
  pm_api_url      = "https://your-proxmox-host:8006/api2/json"
  pm_user         = "root@pam"
  pm_password     = "your-password"
  pm_tls_insecure = true
}

resource "proxmox_vm_qemu" "ubuntu_vm" {
  name        = "ubuntu-vm"
  target_node = "proxmox-node-name"
  vmid        = 100
  cores       = 2
  memory      = 2048
  sockets     = 1
  disk_gb     = 20
  iso         = "local:iso/ubuntu-22.04.iso"
  os_type     = "ubuntu"
  storage     = "local-lvm"
  network {
    model  = "virtio"
    bridge = "vmbr0"
  }
  boot        = "cdn"
  bootdisk    = "scsi0"
  scsihw      = "virtio-scsi-pci"
}

Auto-Select Free VMID

User: code needs to check which is the free vmid and use the next available one Copilot:

Use an external script with Terraform’s `external` data source.

Bash Script: get_next_vmid.sh

#!/bin/bash
 
HOST="your-proxmox-host"
USER="root@pam"
PASS="your-password"
 
TICKET=$(curl -sk -d "username=$USER&password=$PASS" https://$HOST:8006/api2/json/access/ticket | jq -r '.data.ticket')
VMIDS=$(curl -sk -H "Cookie: PVEAuthCookie=$TICKET" https://$HOST:8006/api2/json/cluster/resources | jq '.data[] | select(.type=="vm") | .vmid')
 
NEXT_VMID=100
while echo "$VMIDS" | grep -q "$NEXT_VMID"; do
  NEXT_VMID=$((NEXT_VMID + 1))
done
 
echo "{\"vmid\": $NEXT_VMID}"

Terraform Integration

data "external" "next_vmid" {
  program = ["bash", "${path.module}/get_next_vmid.sh"]
}

resource "proxmox_vm_qemu" "ubuntu_vm" {
  name        = "ubuntu-vm"
  target_node = "proxmox-node-name"
  vmid        = data.external.next_vmid.result["vmid"]
  cores       = 2
  memory      = 2048
  sockets     = 1
  disk_gb     = 20
  iso         = "local:iso/ubuntu-22.04.iso"
  os_type     = "ubuntu"
  storage     = "local-lvm"
  network {
    model  = "virtio"
    bridge = "vmbr0"
  }
  boot        = "cdn"
  bootdisk    = "scsi0"
  scsihw      = "virtio-scsi-pci"
}

Can Terraform Do It Alone?

User: can i get the next vmid using terraform? Copilot:

Terraform doesn’t natively support querying the Proxmox API for VMIDs. But you can use the `external` data source with a script to do it. Here's a Python version:

Python Script: get_next_vmid.py

import requests
import json
 
host = "your-proxmox-host"
user = "root@pam"
password = "your-password"
 
resp = requests.post(f"https://{host}:8006/api2/json/access/ticket", data={
    "username": user,
    "password": password
}, verify=False)
ticket = resp.json()["data"]["ticket"]
headers = {"Cookie": f"PVEAuthCookie={ticket}"}
 
resp = requests.get(f"https://{host}:8006/api2/json/cluster/resources", headers=headers, verify=False)
vmids = [vm["vmid"] for vm in resp.json()["data"] if vm["type"] == "vm"]
 
next_vmid = 100
while next_vmid in vmids:
    next_vmid += 1
 
print(json.dumps({"vmid": next_vmid}))

Terraform Integration

data "external" "next_vmid" {
  program = ["python3", "${path.module}/get_next_vmid.py"]
}

resource "proxmox_vm_qemu" "ubuntu_vm" {
  name        = "ubuntu-vm"
  target_node = "proxmox-node-name"
  vmid        = data.external.next_vmid.result["vmid"]
  # ... rest of your config ...
}

Final Notes