add template vm in proxmox [ci skip]

This commit is contained in:
Viktor Barzin 2025-10-11 17:07:47 +00:00
parent 52638b3783
commit 9831fe1bc3
No known key found for this signature in database
GPG key ID: 4056458DBDBF8863
5 changed files with 68 additions and 18 deletions

40
main.tf
View file

@ -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" {

View file

@ -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

View file

@ -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")
}
}

Binary file not shown.

Binary file not shown.