feat: Initial commit

This commit is contained in:
Tellsanguis 2025-11-07 09:33:38 +01:00
commit 850045e7ed
43 changed files with 1990 additions and 0 deletions

View file

@ -0,0 +1,70 @@
# Cloud-init configuration for K3s Server 1
locals {
base_user_data = {
package_upgrade = true
packages = [
"ansible",
"git",
"curl",
"wget",
"ca-certificates",
"gnupg",
"lsb-release"
]
users = [
{
name = "ansible"
sudo = "ALL=(ALL) NOPASSWD:ALL"
shell = "/bin/bash"
ssh_authorized_keys = [var.ssh_public_key]
groups = "sudo"
}
]
timezone = "Europe/Paris"
}
ansible_pull_script = <<-EOT
#!/bin/bash
set -e
source /etc/ansible-pull.conf
WORK_DIR="/var/lib/ansible-local"
mkdir -p $WORK_DIR
cd $WORK_DIR
REPO_WITH_AUTH=$(echo $REPO_URL | sed "s|https://|https://git:$FORGEJO_TOKEN@|")
if [ -d ".git" ]; then
git pull origin main 2>&1 | logger -t ansible-pull
else
git clone $REPO_WITH_AUTH . 2>&1 | logger -t ansible-pull
fi
ansible-playbook ansible/site.yml -i localhost, --connection=local -e "k3s_version=$K3S_VERSION" 2>&1 | logger -t ansible-pull
EOT
k3s_server_user_data = {
write_files = [
{
path = "/etc/node-role"
content = "server"
permissions = "0644"
},
{
path = "/etc/ansible-pull.conf"
content = "REPO_URL=${var.forgejo_repo_url}\nFORGEJO_TOKEN=${var.forgejo_token}\nK3S_VERSION=${var.k3s_version}"
permissions = "0600"
},
{
path = "/usr/local/bin/ansible-pull-wrapper.sh"
content = local.ansible_pull_script
permissions = "0755"
}
]
runcmd = [
"echo '*/15 * * * * root /usr/local/bin/ansible-pull-wrapper.sh' > /etc/cron.d/ansible-pull",
"sleep 60 && /usr/local/bin/ansible-pull-wrapper.sh &"
]
}
}
resource "local_file" "k3s_server_cloud_init" {
filename = "${path.module}/.generated/cloud-init-k3s-server-1.yaml"
content = yamlencode(merge(local.base_user_data, local.k3s_server_user_data))
}

64
terraform/pve1/main.tf Normal file
View file

@ -0,0 +1,64 @@
terraform {
required_version = ">= 1.6.0"
required_providers {
proxmox = {
source = "telmate/proxmox"
version = "~> 3.0"
}
local = {
source = "hashicorp/local"
version = "~> 2.1"
}
}
}
provider "proxmox" {
pm_api_url = var.proxmox_api_url
pm_api_token_id = var.proxmox_token_id
pm_api_token_secret = var.proxmox_token_secret
pm_tls_insecure = var.proxmox_tls_insecure
}
# K3s Server VM on pve1
resource "proxmox_vm_qemu" "k3s_server_1" {
name = "k3s-server-1"
target_node = "pve1"
clone = var.ubuntu_template
cores = var.k3s_server_1_config.cores
sockets = 1
memory = var.k3s_server_1_config.memory
agent = 1
boot = "order=scsi0"
scsihw = "virtio-scsi-single"
onboot = true
network {
model = "virtio"
bridge = var.k3s_network_bridge
}
disks {
scsi {
scsi0 {
disk {
size = var.k3s_server_1_config.disk_size
storage = var.storage_pool
iothread = true
}
}
}
}
ipconfig0 = "ip=${var.k3s_server_1_config.ip},gw=${var.k3s_gateway}"
cicustom = "user=${var.snippets_storage}:snippets/cloud-init-k3s-server-1.yaml"
nameserver = join(" ", var.k3s_dns)
lifecycle {
ignore_changes = [ network ]
}
depends_on = [local_file.k3s_server_cloud_init]
}

View file

@ -0,0 +1,8 @@
output "k3s_server_1" {
description = "K3s Server 1 VM information"
value = {
name = proxmox_vm_qemu.k3s_server_1.name
ip = var.k3s_server_1_config.ip
node = proxmox_vm_qemu.k3s_server_1.target_node
}
}

View file

@ -0,0 +1,84 @@
variable "proxmox_api_url" {
description = "Proxmox API URL"
type = string
default = "https://192.168.100.10:8006/api2/json"
}
variable "proxmox_token_id" {
description = "Proxmox API Token ID"
type = string
sensitive = true
}
variable "proxmox_token_secret" {
description = "Proxmox API Token Secret"
type = string
sensitive = true
}
variable "proxmox_tls_insecure" {
description = "Skip TLS verification for Proxmox API"
type = bool
default = true
}
variable "ssh_public_key" {
description = "SSH public key for admin access"
type = string
}
variable "forgejo_token" {
description = "Forgejo token for ansible-pull authentication"
type = string
sensitive = true
}
variable "forgejo_repo_url" {
description = "Forgejo repository URL (without credentials)"
type = string
}
variable "k3s_version" {
description = "K3s version to install"
type = string
}
variable "ubuntu_template" {
description = "Ubuntu cloud-init template name"
type = string
}
variable "storage_pool" {
description = "Proxmox storage pool for VM disks"
type = string
}
variable "snippets_storage" {
description = "Proxmox storage for cloud-init snippets"
type = string
}
variable "k3s_network_bridge" {
description = "SDN bridge for K3s VMs"
type = string
}
variable "k3s_gateway" {
description = "Gateway for K3s network"
type = string
}
variable "k3s_dns" {
description = "DNS servers for K3s network"
type = list(string)
}
variable "k3s_server_1_config" {
description = "K3s server-1 VM configuration"
type = object({
ip = string
cores = number
memory = number
disk_size = string
})
}