From 8990d345656c72e76fec2d67ba2e1e52970b696d Mon Sep 17 00:00:00 2001 From: rsoliman Date: Tue, 9 Jun 2026 09:39:23 +0200 Subject: [PATCH] PoC OpenTofu configuration works --- .gitignore | 2 ++ clone.tf | 36 ++++++++++++++++++++++++++++++++++++ cloud-conf.tf | 37 +++++++++++++++++++++++++++++++++++++ providers.tf | 10 ++++++++++ template.tf | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ variables.tf | 11 +++++++++++ 6 files changed, 144 insertions(+) create mode 100644 clone.tf create mode 100644 cloud-conf.tf create mode 100644 template.tf create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore index 0595912..13b5c60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .envrc .terraform .terraform.* +terraform* +id_* diff --git a/clone.tf b/clone.tf new file mode 100644 index 0000000..9b428a7 --- /dev/null +++ b/clone.tf @@ -0,0 +1,36 @@ +resource "proxmox_virtual_environment_vm" "ubuntu_clone" { + name = "ubuntu-clone" + node_name = var.virtual_environment_node_name + + clone { + vm_id = proxmox_virtual_environment_vm.ubuntu_template.id + } + + agent { + # NOTE: The agent is installed and enabled as part of the cloud-init configuration in the template VM, see cloud-config.tf + # The working agent is *required* to retrieve the VM IP addresses. + # If you are using a different cloud-init configuration, or a different clone source + # that does not have the qemu-guest-agent installed, you may need to disable the `agent` below and remove the `vm_ipv4_address` output. + # See https://bpg.sh/docs/resources/virtual_environment_vm#qemu-guest-agent for more details. + enabled = true + } + + memory { + dedicated = 768 + } + + initialization { + dns { + servers = ["1.1.1.1"] + } + ip_config { + ipv4 { + address = "dhcp" + } + } + } +} + +output "vm_ipv4_address" { + value = proxmox_virtual_environment_vm.ubuntu_clone.ipv4_addresses[1][0] +} \ No newline at end of file diff --git a/cloud-conf.tf b/cloud-conf.tf new file mode 100644 index 0000000..6f4804c --- /dev/null +++ b/cloud-conf.tf @@ -0,0 +1,37 @@ +data "local_file" "ssh_public_key" { + filename = "./id_ed25519_rocky.pub" +} + +resource "proxmox_virtual_environment_file" "user_data_cloud_config" { + content_type = "snippets" + datastore_id = "local" + node_name = var.virtual_environment_node_name + + source_raw { + data = <<-EOF + #cloud-config + hostname: test-ubuntu + timezone: Europe/Berlin + users: + - default + - name: ubuntu + groups: + - sudo + shell: /bin/bash + ssh_authorized_keys: + - ${trimspace(data.local_file.ssh_public_key.content)} + sudo: ALL=(ALL) NOPASSWD:ALL + package_update: true + packages: + - qemu-guest-agent + - net-tools + - curl + runcmd: + - systemctl enable qemu-guest-agent + - systemctl start qemu-guest-agent + - echo "done" > /tmp/cloud-config.done + EOF + + file_name = "user-data-cloud-config.yaml" + } +} \ No newline at end of file diff --git a/providers.tf b/providers.tf index 5e788b6..51ac955 100644 --- a/providers.tf +++ b/providers.tf @@ -1,4 +1,14 @@ provider "proxmox" { endpoint = "https://prox-console.tail1ad20b.ts.net/" # api_token read from PROXMOX_VE_API_TOKEN + + ssh { + agent = true + username = "root" + node { + name = "pve" + address = "pve.tail1ad20b.ts.net" + } + } + } diff --git a/template.tf b/template.tf new file mode 100644 index 0000000..a0ceda1 --- /dev/null +++ b/template.tf @@ -0,0 +1,48 @@ +resource "proxmox_virtual_environment_vm" "ubuntu_template" { + name = "ubuntu-template" + node_name = var.virtual_environment_node_name + + template = true + started = false + + machine = "q35" + bios = "ovmf" + description = "Managed by OpenTofu" + + cpu { + cores = 2 + } + + memory { + dedicated = 2048 + } + + efi_disk { + datastore_id = var.datastore_id + type = "4m" + } + + disk { + datastore_id = var.datastore_id + file_id = "local:iso/jammy-server-cloudimg-amd64.img" + interface = "virtio0" + iothread = true + discard = "on" + size = 20 + } + + initialization { + ip_config { + ipv4 { + address = "dhcp" + } + } + + user_data_file_id = proxmox_virtual_environment_file.user_data_cloud_config.id + } + + network_device { + bridge = "vmbr0" + } + +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..726472b --- /dev/null +++ b/variables.tf @@ -0,0 +1,11 @@ +variable "virtual_environment_node_name" { + type = string + description = "The node name for the Proxmox Virtual Environment API" + default = "pve" +} + +variable "datastore_id" { + type = string + description = "Datastore for VM disks" + default = "local-lvm" +} \ No newline at end of file