Compare commits

...

13 Commits

18 changed files with 303 additions and 41 deletions

View File

@@ -1,3 +1,20 @@
# Homelab # 🧪 Homelab
Hello world ! > ⚠️ **Work in Progress** This repository is actively evolving as I automate and expand my homelab.
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

@@ -2,3 +2,4 @@
plugin: cloud.terraform.terraform_provider plugin: cloud.terraform.terraform_provider
project_path: project_path:
- /home/vez/homelab/terraform/projects/terransible - /home/vez/homelab/terraform/projects/terransible
state_file: terraform.tfstate.d/lab/terraform.tfstate

View File

@@ -8,9 +8,9 @@
vars: vars:
terraform_vm_state: "{{ state | default(omit) }}" terraform_vm_state: "{{ state | default(omit) }}"
terraform_vm_project_path: /home/vez/homelab/terraform/projects/terransible terraform_vm_project_path: /home/vez/homelab/terraform/projects/terransible
terraform_vars_vm_env: lab terraform_vars_vm_env: "{{ env | default('lab')}}"
terraform_vars_vm_tags: terraform_vars_vm_tags:
- lab - "{{ env | default('lab')}}"
terraform_vars_multi_node_deployment: "{{ multi_node_deployment | default(true) }}" terraform_vars_multi_node_deployment: "{{ multi_node_deployment | default(true) }}"
terraform_vars_target_node: "{{ target_node | default(omit) }}" terraform_vars_target_node: "{{ target_node | default(omit) }}"
terraform_vars_vm_attr: {"master": { "ram": 2048, "cpu": 2, "vlan": 66 }, "worker": { "ram": 1024, "cpu": 1, "vlan": 66 }} terraform_vars_vm_attr: {"master": { "ram": 2048, "cpu": 2, "vlan": 66 }, "worker": { "ram": 1024, "cpu": 1, "vlan": 66 }}

View File

@@ -1,2 +1,3 @@
--- ---
terraform_ansible_inventory: /home/vez/homelab/ansible/inventories/terraform.yml
terraform_vm_state: present terraform_vm_state: present

View File

@@ -4,6 +4,7 @@
cloud.terraform.terraform: cloud.terraform.terraform:
project_path: "{{ terraform_vm_project_path }}" project_path: "{{ terraform_vm_project_path }}"
state: "{{ terraform_vm_state }}" state: "{{ terraform_vm_state }}"
workspace: "{{ terraform_vars_vm_env | default(omit)}}"
parallelism: 3 parallelism: 3
complex_vars: true complex_vars: true
variables: variables:
@@ -12,7 +13,13 @@
vm_attr: "{{ terraform_vars_vm_attr | default(omit)}}" vm_attr: "{{ terraform_vars_vm_attr | default(omit)}}"
vm_env: "{{ terraform_vars_vm_env | default(omit)}}" vm_env: "{{ terraform_vars_vm_env | default(omit)}}"
vm_tags: "{{ terraform_vars_vm_tags | default(omit)}}" vm_tags: "{{ terraform_vars_vm_tags | default(omit)}}"
retries: 1
- name: Update Terraform workspace path
ansible.builtin.lineinfile:
path: "{{ terraform_ansible_inventory }}"
regexp: '^state_file:.*tfstate$'
line: "state_file: terraform.tfstate.d/{{ terraform_vars_vm_env }}/terraform.tfstate"
when: terraform_vars_vm_env is defined
- name: Refresh inventory - name: Refresh inventory
ansible.builtin.meta: refresh_inventory ansible.builtin.meta: refresh_inventory

View File

@@ -1,93 +1,108 @@
# 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}"] values = ["${var.vm_template}"] # The name of the template to clone from
} }
} }
# 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" content_type = "snippets" # Cloud-init files are stored as snippets in Proxmox
datastore_id = "local" datastore_id = "local" # Local datastore used to store the snippet
node_name = var.node_name node_name = var.node_name # The Proxmox node where the file will be uploaded
source_raw { source_raw {
file_name = "${var.vm_name}.cloud-config.yaml" file_name = "${var.vm_name}.cloud-config.yaml" # The name of the snippet file
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 - qemu-guest-agent # Ensures the guest agent is installed
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}" - "${var.vm_user_sshkey}" # Inject user's SSH key
sudo: ALL=(ALL) NOPASSWD:ALL sudo: ALL=(ALL) NOPASSWD:ALL
runcmd: runcmd:
- systemctl enable qemu-guest-agent - systemctl enable qemu-guest-agent
- reboot - reboot # Reboot the VM after provisioning
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 name = var.vm_name # VM name
node_name = var.node_name node_name = var.node_name # Proxmox node to deploy the VM
tags = var.vm_tags tags = var.vm_tags # Optional VM tags for categorization
agent { agent {
enabled = true enabled = true # Enable the QEMU guest agent
} }
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 vm_id = data.proxmox_virtual_environment_vms.template.vms[0].vm_id # ID of the source template
node_name = data.proxmox_virtual_environment_vms.template.vms[0].node_name node_name = data.proxmox_virtual_environment_vms.template.vms[0].node_name # Node of the source template
} }
bios = var.vm_bios
machine = var.vm_machine bios = var.vm_bios # BIOS type (e.g., seabios or ovmf)
machine = var.vm_machine # Machine type (e.g., q35)
cpu { cpu {
cores = var.vm_cpu cores = var.vm_cpu # Number of CPU cores
type = "host" type = "host" # Use host CPU type for best compatibility/performance
} }
memory { memory {
dedicated = var.vm_ram dedicated = var.vm_ram # RAM in MB
} }
disk { disk {
datastore_id = var.node_datastore datastore_id = var.node_datastore # Datastore to hold the disk
interface = "scsi0" interface = "scsi0" # Primary disk interface
size = 4 size = var.vm_disk_size # Disk size in GB
} }
initialization { initialization {
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id 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" interface = "scsi1" # Separate interface for cloud-init
ip_config { ip_config {
ipv4 { ipv4 {
address = "dhcp" address = "dhcp" # Get IP via DHCP
} }
} }
} }
network_device { network_device {
bridge = "vmbr0" bridge = "vlan${var.vm_vlan}" # VNet used with VLAN ID
vlan_id = var.vm_vlan
} }
operating_system { operating_system {
type = "l26" type = "l26" # Linux 2.6+ kernel
} }
vga { vga {
type = "std" type = "std" # Standard VGA type
} }
lifecycle { lifecycle {
ignore_changes = [ ignore_changes = [ # Ignore initialization section after first depoloyment for idempotency
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] value = proxmox_virtual_environment_vm.vm.ipv4_addresses[1][0] # Second network interface's first IP
description = "VM IP" description = "VM IP"
} }

View File

@@ -44,6 +44,12 @@ 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 = "${role}-${node}" vm_name = "${node}-${role}"
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

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

View File

@@ -0,0 +1,22 @@
# Define the required Terraform provider block
terraform {
required_providers {
proxmox = {
source = "bpg/proxmox" # Use the community Proxmox provider from the bpg namespace
}
}
}
# Configure the Proxmox provider with API and SSH access
provider "proxmox" {
endpoint = var.proxmox_endpoint # Proxmox API URL (e.g., https://proxmox.local:8006/api2/json)
api_token = var.proxmox_api_token # API token for authentication (should have appropriate permissions)
insecure = false # Reject self-signed or invalid TLS certificates (set to true only in trusted/test environments)
# Optional SSH settings used for VM customization via SSH
ssh {
agent = false # Do not use the local SSH agent; use key file instead
private_key = file("~/.ssh/id_ed25519") # Load SSH private key from the local file system
username = "root" # SSH username for connecting to the Proxmox host
}
}

View File

@@ -0,0 +1,5 @@
node_name = "zenith" # Name of the Proxmox node where the VM will be deployed
vm_name = "zenith-vm" # Desired name for the new virtual machine
vm_cpu = 2 # Number of CPU cores to allocate to the VM
vm_ram = 2048 # Amount of RAM in MB (2 GB)
vm_vlan = 66 # VLAN ID for network segmentation

View File

@@ -0,0 +1,80 @@
variable "proxmox_endpoint" {
description = "Proxmox URL endpoint"
type = string
}
variable "proxmox_api_token" {
description = "Proxmox API token"
type = string
sensitive = true
}
variable "node_name" {
description = "Proxmox host for the VM"
type = string
}
variable "node_datastore" {
description = "Datastore used for VM storage"
type = string
default = "ceph-workload"
}
variable "vm_template" {
description = "Template of the VM"
type = string
default = "ubuntu-cloud"
}
variable "vm_name" {
description = "Hostname of the VM"
type = string
}
variable "vm_user" {
description = "Admin user of the VM"
type = string
default = "vez"
}
variable "vm_user_sshkey" {
description = "Admin user SSH key of the VM"
type = string
default = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID62LmYRu1rDUha3timAIcA39LtcIOny1iAgFLnxoBxm vez@bastion"
}
variable "vm_cpu" {
description = "Number of CPU cores of the VM"
type = number
default = 1
}
variable "vm_ram" {
description = "Number of RAM (MB) of the VM"
type = number
default = 2048
}
variable "vm_bios" {
description = "Type of BIOS used for the VM"
type = string
default = "ovmf"
}
variable "vm_machine" {
description = "Type of machine used for the VM"
type = string
default = "q35"
}
variable "vm_vlan" {
description = "VLAN of the VM"
type = number
default = 66
}
variable "vm_tags" {
description = "Tags for the VM"
type = list(any)
default = ["test"]
}