Compare commits

..

3 Commits

Author SHA1 Message Date
29a0d32870 fix: add lifecycle section for idempotency 2025-05-28 07:40:41 +00:00
22f6403417 Add comments to describe the code 2025-05-27 20:42:23 +00:00
a7491df751 Restore simple-vm without module 2025-05-27 19:49:22 +00:00
6 changed files with 40 additions and 77 deletions

View File

@@ -1,20 +1,3 @@
# 🧪 Homelab # Homelab
> ⚠️ **Work in Progress** This repository is actively evolving as I automate and expand my homelab. Hello world !
Welcome to my homelab repository! This is where I manage and document the infrastructure powering my personal lab environment using modern DevOps tools and best practices.
## 🚀 Goals
- Automate VM and infrastructure deployment with **Terraform**
- Configure systems and services using **Ansible**
- Deploy and manage Kubernetes with **Flux CD** using a **GitOps** approach
- Keep everything **declarative**, **reproducible**, and **version-controlled**
## 📌 Notes
This repository is intended for **educational and experimental purposes**. Feel free to explore, fork, and adapt ideas for your own homelab setup.
---
Stay tuned — more coming soon! 🚧

View File

@@ -1,108 +1,93 @@
# Retrieve VM templates available in Proxmox that match the specified name
data "proxmox_virtual_environment_vms" "template" { data "proxmox_virtual_environment_vms" "template" {
filter { filter {
name = "name" name = "name"
values = ["${var.vm_template}"] # The name of the template to clone from values = ["${var.vm_template}"]
} }
} }
# Create a cloud-init configuration file as a Proxmox snippet
resource "proxmox_virtual_environment_file" "cloud_config" { resource "proxmox_virtual_environment_file" "cloud_config" {
content_type = "snippets" # Cloud-init files are stored as snippets in Proxmox content_type = "snippets"
datastore_id = "local" # Local datastore used to store the snippet datastore_id = "local"
node_name = var.node_name # The Proxmox node where the file will be uploaded node_name = var.node_name
source_raw { source_raw {
file_name = "${var.vm_name}.cloud-config.yaml" # The name of the snippet file file_name = "${var.vm_name}.cloud-config.yaml"
data = <<-EOF data = <<-EOF
#cloud-config #cloud-config
hostname: ${var.vm_name} hostname: ${var.vm_name}
package_update: true package_update: true
package_upgrade: true package_upgrade: true
packages: packages:
- qemu-guest-agent # Ensures the guest agent is installed - qemu-guest-agent
users: users:
- default - default
- name: ${var.vm_user} - name: ${var.vm_user}
groups: sudo groups: sudo
shell: /bin/bash shell: /bin/bash
ssh-authorized-keys: ssh-authorized-keys:
- "${var.vm_user_sshkey}" # Inject user's SSH key - "${var.vm_user_sshkey}"
sudo: ALL=(ALL) NOPASSWD:ALL sudo: ALL=(ALL) NOPASSWD:ALL
runcmd: runcmd:
- systemctl enable qemu-guest-agent - systemctl enable qemu-guest-agent
- reboot # Reboot the VM after provisioning - reboot
EOF EOF
} }
} }
# Define and provision a new VM by cloning the template and applying initialization
resource "proxmox_virtual_environment_vm" "vm" { resource "proxmox_virtual_environment_vm" "vm" {
name = var.vm_name # VM name name = var.vm_name
node_name = var.node_name # Proxmox node to deploy the VM node_name = var.node_name
tags = var.vm_tags # Optional VM tags for categorization tags = var.vm_tags
agent { agent {
enabled = true # Enable the QEMU guest agent enabled = true
} }
stop_on_destroy = true
stop_on_destroy = true # Ensure VM is stopped gracefully when destroyed
clone { clone {
vm_id = data.proxmox_virtual_environment_vms.template.vms[0].vm_id # ID of the source template vm_id = data.proxmox_virtual_environment_vms.template.vms[0].vm_id
node_name = data.proxmox_virtual_environment_vms.template.vms[0].node_name # Node of the source template node_name = data.proxmox_virtual_environment_vms.template.vms[0].node_name
} }
bios = var.vm_bios
bios = var.vm_bios # BIOS type (e.g., seabios or ovmf) machine = var.vm_machine
machine = var.vm_machine # Machine type (e.g., q35)
cpu { cpu {
cores = var.vm_cpu # Number of CPU cores cores = var.vm_cpu
type = "host" # Use host CPU type for best compatibility/performance type = "host"
} }
memory { memory {
dedicated = var.vm_ram # RAM in MB dedicated = var.vm_ram
} }
disk { disk {
datastore_id = var.node_datastore # Datastore to hold the disk
interface = "scsi0" # Primary disk interface
size = var.vm_disk_size # Disk size in GB
}
initialization {
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id # Link the cloud-init file
datastore_id = var.node_datastore datastore_id = var.node_datastore
interface = "scsi1" # Separate interface for cloud-init interface = "scsi0"
size = 4
}
initialization {
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id
datastore_id = var.node_datastore
interface = "scsi1"
ip_config { ip_config {
ipv4 { ipv4 {
address = "dhcp" # Get IP via DHCP address = "dhcp"
} }
} }
} }
network_device { network_device {
bridge = "vlan${var.vm_vlan}" # VNet used with VLAN ID bridge = "vmbr0"
vlan_id = var.vm_vlan
} }
operating_system { operating_system {
type = "l26" # Linux 2.6+ kernel type = "l26"
} }
vga { vga {
type = "std" # Standard VGA type type = "std"
} }
lifecycle { lifecycle {
ignore_changes = [ # Ignore initialization section after first depoloyment for idempotency ignore_changes = [
initialization initialization
] ]
} }
} }
# Output the assigned IP address of the VM after provisioning
output "vm_ip" { output "vm_ip" {
value = proxmox_virtual_environment_vm.vm.ipv4_addresses[1][0] # Second network interface's first IP value = proxmox_virtual_environment_vm.vm.ipv4_addresses[1][0]
description = "VM IP" description = "VM IP"
} }

View File

@@ -44,12 +44,6 @@ variable "vm_ram" {
default = 2048 default = 2048
} }
variable "vm_disk_size" {
description = "Size of the disk (GB) of the VM"
type = number
default = 10
}
variable "vm_bios" { variable "vm_bios" {
description = "Type of BIOS used for the VM" description = "Type of BIOS used for the VM"
type = string type = string

View File

@@ -19,7 +19,7 @@ locals {
for node in data.proxmox_virtual_environment_nodes.pve_nodes.names : [ for node in data.proxmox_virtual_environment_nodes.pve_nodes.names : [
for role, config in local.vm_attr : { for role, config in local.vm_attr : {
node_name = node node_name = node
vm_name = "${node}-${role}" vm_name = "${role}-${node}"
vm_cpu = config.cpu vm_cpu = config.cpu
vm_ram = config.ram vm_ram = config.ram
vm_vlan = config.vlan vm_vlan = config.vlan

View File

@@ -83,7 +83,8 @@ resource "proxmox_virtual_environment_vm" "vm" {
} }
network_device { network_device {
bridge = "vlan${var.vm_vlan}" # VNet used with VLAN ID bridge = "vmbr0" # Use the default bridge
vlan_id = var.vm_vlan # VLAN tagging if used
} }
operating_system { operating_system {