From 51a94faff4ca80457394d94cddc0aa13a11087ce Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Sat, 11 Oct 2025 17:07:47 +0000 Subject: [PATCH] add template vm in proxmox [ci skip] --- main.tf | 40 ++++++++++++---------- modules/create-template-vm/cloud_init.yaml | 22 ++++++++++++ modules/create-template-vm/main.tf | 24 +++++++++++++ 3 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 modules/create-template-vm/cloud_init.yaml diff --git a/main.tf b/main.tf index e075bbf4..6089051e 100644 --- a/main.tf +++ b/main.tf @@ -2,9 +2,11 @@ variable "prod" { type = bool default = false } -variable "vsphere_password" {} -variable "vsphere_user" {} -variable "vsphere_server" {} +variable "proxmox_pm_api_url" { type = string } +variable "proxmox_pm_api_token_id" { type = string } +variable "proxmox_pm_api_token_secret" { type = string } +variable "vm_wizard_password" { type = string } +variable "proxmox_host" { type = string } variable "tls_secret_name" {} variable "tls_crt" { default = "" @@ -114,9 +116,6 @@ variable "onlyoffice_jwt_token" { type = string } variable "xray_reality_clients" { type = list(map(string)) } variable "xray_reality_private_key" { type = string } variable "xray_reality_short_ids" { type = list(string) } -variable "proxmox_pm_api_url" { type = string } -variable "proxmox_pm_api_token_id" { type = string } -variable "proxmox_pm_api_token_secret" { type = string } # data "terraform_remote_state" "foo" { @@ -156,28 +155,33 @@ provider "proxmox" { # comment = "VLAN 99" # } +locals { + vm_template_name = "ubuntu-2404-cloudinit-template" + vm_cloud_init_snippet_name = "cloud_init.yaml" +} # Main module to init infra from module "template-vm" { source = "./modules/create-template-vm" - proxmox_host = "192.168.1.127" + proxmox_host = var.proxmox_host proxmox_user = "root" # SSH user on Proxmox host - cloud_image_url = "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img" - image_path = "/var/lib/vz/template/iso/jammy-server-cloudimg-amd64.img" + cloud_image_url = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" + image_path = "/var/lib/vz/template/iso/noble-server-cloudimg-amd64.img" template_id = 8000 - template_name = "ubuntu-2204-cloudinit-template" + template_name = local.vm_template_name + + snippet_name = local.vm_cloud_init_snippet_name + user_passwd = var.vm_wizard_password } # module "pxe-server" { -# source = "./modules/create-vm" -# vm_name = "pxe-server" -# network = "dManagementVMs" -# # provisioner_command = "${var.ansible_prefix} -t linux/pxe-server/add-distro" -# provisioner_command = "# no provisioner needed #" # Noop until ubuntu autoinstall is setup - -# cdrom_path = "ISO/ubuntu-server-20.04.1.iso" +# template_name = local.vm_template_name +# source = "./modules/create-vm" +# vm_name = "pxe-server" # vm_disk_size = 50 -# vm_mac_address = "00:50:56:87:4a:2d" +# cisnippet_name = local.vm_cloud_init_snippet_name + +# # vm_mac_address = "00:50:56:87:4a:2d" # } # module "k8s_master" { diff --git a/modules/create-template-vm/cloud_init.yaml b/modules/create-template-vm/cloud_init.yaml new file mode 100644 index 00000000..f8763303 --- /dev/null +++ b/modules/create-template-vm/cloud_init.yaml @@ -0,0 +1,22 @@ +#cloud-config +hostname: terraform-vm +users: + - name: wizard + sudo: ALL=(ALL) NOPASSWD:ALL + ssh_authorized_keys: + - ${authorized_ssh_key} + passwd: ${passwd} + lock_passwd: false # enable passwd login +package_update: true +package_upgrade: true +packages: + - htop + - vim + - curl + - jq + - tcpdump + - tree + - tmux + - wget + - net-tools + - zsh diff --git a/modules/create-template-vm/main.tf b/modules/create-template-vm/main.tf index 332d946d..e90400bc 100644 --- a/modules/create-template-vm/main.tf +++ b/modules/create-template-vm/main.tf @@ -7,6 +7,8 @@ variable "template_id" { default = 8000 } variable "template_name" { type = string } +variable "snippet_name" { type = string } +variable "user_passwd" { type = string } # hashed pw # SSH connection to Proxmox resource "null_resource" "create_template_remote" { @@ -39,3 +41,25 @@ resource "null_resource" "create_template_remote" { ] } } + +resource "null_resource" "upload_cloud_init" { + connection { + type = "ssh" + host = var.proxmox_host + user = var.proxmox_user + private_key = file("~/.ssh/id_ed25519") + } + + provisioner "remote-exec" { + inline = ["mkdir -p /var/lib/vz/snippets"] + } + + provisioner "file" { + destination = "/var/lib/vz/snippets/${var.snippet_name}" + content = templatefile("${path.module}/cloud_init.yaml", { authorized_ssh_key = file("~/.ssh/id_ed25519.pub"), passwd = var.user_passwd }) + } + + triggers = { + file_hash = filesha256("${path.module}/cloud_init.yaml") + } +}