Cleaning VM Creation
- Remove useless tf files - add extra disks capacity
This commit is contained in:
132
terraform/prod/01-virtual-machines.tf
Normal file
132
terraform/prod/01-virtual-machines.tf
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
# Fichier: terraform/prod/01-virtual-machines.tf
|
||||||
|
# Description: Déploiement des machines virtuelles sur Proxmox
|
||||||
|
|
||||||
|
# 1. Récupération des templates disponibles sur Proxmox
|
||||||
|
data "proxmox_virtual_environment_vms" "templates" {
|
||||||
|
node_name = var.target_node
|
||||||
|
tags = ["template"]
|
||||||
|
}
|
||||||
|
|
||||||
|
# 2. Mapping des templates et préparation des données pour Ansible
|
||||||
|
locals {
|
||||||
|
# OS supportés
|
||||||
|
supported_os = toset(["ubuntu", "debian", "alpine", "rocky", "centos"])
|
||||||
|
|
||||||
|
# Map des templates par OS
|
||||||
|
template_map = {
|
||||||
|
for template in data.proxmox_virtual_environment_vms.templates.vms :
|
||||||
|
one(setintersection(local.supported_os, template.tags)) => template
|
||||||
|
if length(setintersection(local.supported_os, template.tags)) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
ansible_vms = {
|
||||||
|
for key, vm in proxmox_virtual_environment_vm.vms : key => {
|
||||||
|
# Données issues de la ressource VM créée
|
||||||
|
name = vm.name
|
||||||
|
proxmox_node = vm.node_name
|
||||||
|
|
||||||
|
# Données issues de vos variables d'entrée
|
||||||
|
ip_address = var.virtual_machines[key].ip
|
||||||
|
os_distro = var.virtual_machines[key].os
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 3. Création des machines virtuelles
|
||||||
|
resource "proxmox_virtual_environment_vm" "vms" {
|
||||||
|
for_each = var.virtual_machines
|
||||||
|
|
||||||
|
name = "${each.key}.${var.domain_suffix}"
|
||||||
|
node_name = var.target_node
|
||||||
|
tags = concat(["terraform-managed", "vm"], each.value.tags)
|
||||||
|
on_boot = lookup(each.value, "on_boot", true)
|
||||||
|
|
||||||
|
# Configuration de l'agent QEMU
|
||||||
|
agent {
|
||||||
|
enabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
# Configuration CPU
|
||||||
|
cpu {
|
||||||
|
cores = each.value.cores
|
||||||
|
type = lookup(each.value, "cpu_type", "host")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Configuration mémoire
|
||||||
|
memory {
|
||||||
|
dedicated = each.value.memory
|
||||||
|
}
|
||||||
|
|
||||||
|
# Configuration disque principal
|
||||||
|
disk {
|
||||||
|
interface = "scsi0"
|
||||||
|
datastore_id = lookup(each.value, "datastore", "local-lvm")
|
||||||
|
size = each.value.disk_gb
|
||||||
|
file_format = lookup(each.value, "disk_format", "raw")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Disques supplémentaires (optionnel)
|
||||||
|
dynamic "disk" {
|
||||||
|
for_each = lookup(each.value, "additional_disks", [])
|
||||||
|
content {
|
||||||
|
interface = disk.value.interface
|
||||||
|
datastore_id = disk.value.datastore
|
||||||
|
size = disk.value.size
|
||||||
|
file_format = lookup(disk.value, "format", "raw")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Configuration réseau
|
||||||
|
network_device {
|
||||||
|
bridge = lookup(each.value, "network_bridge", "vmbr0")
|
||||||
|
model = lookup(each.value, "network_model", "virtio")
|
||||||
|
}
|
||||||
|
|
||||||
|
# Clonage depuis le template
|
||||||
|
clone {
|
||||||
|
vm_id = local.template_map[each.value.os].vm_id
|
||||||
|
}
|
||||||
|
|
||||||
|
# On utilise le bloc 'initialization' pour configurer directement
|
||||||
|
# le réseau et l'utilisateur, sans fichier user_data externe.
|
||||||
|
initialization {
|
||||||
|
# Configuration IP
|
||||||
|
ip_config {
|
||||||
|
ipv4 {
|
||||||
|
address = "${each.value.ip}/${lookup(each.value, "netmask", "24")}"
|
||||||
|
gateway = each.value.gateway
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Configuration utilisateur
|
||||||
|
user_account {
|
||||||
|
username = lookup(each.value, "username", var.default_vm_user)
|
||||||
|
keys = [file(var.admin_ssh_public_key_path)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 5. Outputs pour récupérer les informations des VMs
|
||||||
|
output "vm_info" {
|
||||||
|
description = "Informations des machines virtuelles créées"
|
||||||
|
value = {
|
||||||
|
for k, v in proxmox_virtual_environment_vm.vms : k => {
|
||||||
|
id = v.id
|
||||||
|
name = v.name
|
||||||
|
node = v.node_name
|
||||||
|
ip = var.virtual_machines[k].ip
|
||||||
|
mac = v.network_device[0].mac_address
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Génération automatique de l'inventaire Ansible
|
||||||
|
resource "local_file" "ansible_inventory" {
|
||||||
|
filename = "${path.module}/../../ansible/inventory/01_lab_hosts.yml"
|
||||||
|
|
||||||
|
content = templatefile("${path.module}/templates/inventory.tftpl", {
|
||||||
|
vms = local.ansible_vms
|
||||||
|
})
|
||||||
|
|
||||||
|
depends_on = [proxmox_virtual_environment_vm.vms]
|
||||||
|
}
|
||||||
196
terraform/prod/02-lxc-containers.tf
Normal file
196
terraform/prod/02-lxc-containers.tf
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
# Fichier: terraform/prod/lxc-containers.tf
|
||||||
|
# Description: Déploiement des conteneurs LXC sur Proxmox
|
||||||
|
# Status: Préparatoire - À développer selon les besoins
|
||||||
|
|
||||||
|
# NOTE: Ce fichier est préparé pour le futur déploiement de conteneurs LXC
|
||||||
|
# Il n'est pas encore fonctionnel et servira de base pour le développement
|
||||||
|
|
||||||
|
# 1. Récupération des templates LXC disponibles
|
||||||
|
# data "proxmox_virtual_environment_container_templates" "lxc_templates" {
|
||||||
|
# node_name = var.target_node
|
||||||
|
# }
|
||||||
|
|
||||||
|
# 2. Mapping des templates LXC par distribution
|
||||||
|
# locals {
|
||||||
|
# # Distributions LXC supportées
|
||||||
|
# supported_lxc_distros = toset(["ubuntu", "debian", "alpine", "centos"])
|
||||||
|
#
|
||||||
|
# # Map des templates LXC par distribution (à implémenter)
|
||||||
|
# lxc_template_map = {
|
||||||
|
# # Structure exemple :
|
||||||
|
# # "ubuntu" = "ubuntu-20.04-standard_20.04-1_amd64.tar.gz"
|
||||||
|
# # "debian" = "debian-11-standard_11.0-1_amd64.tar.gz"
|
||||||
|
# # "alpine" = "alpine-3.14-default_20210623_amd64.tar.xz"
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
|
||||||
|
# 3. Création des conteneurs LXC
|
||||||
|
# resource "proxmox_virtual_environment_container" "lxc_containers" {
|
||||||
|
# for_each = var.lxc_containers
|
||||||
|
#
|
||||||
|
# # Configuration de base
|
||||||
|
# node_name = var.target_node
|
||||||
|
# vm_id = each.value.vm_id
|
||||||
|
#
|
||||||
|
# # Configuration du conteneur
|
||||||
|
# description = "LXC Container: ${each.key}"
|
||||||
|
# tags = concat(["terraform-managed", "lxc"], each.value.tags)
|
||||||
|
#
|
||||||
|
# # Configuration système
|
||||||
|
# unprivileged = lookup(each.value, "unprivileged", true)
|
||||||
|
# on_boot = lookup(each.value, "on_boot", true)
|
||||||
|
# started = lookup(each.value, "started", true)
|
||||||
|
#
|
||||||
|
# # Configuration OS
|
||||||
|
# operating_system {
|
||||||
|
# template_file_id = each.value.template
|
||||||
|
# type = lookup(each.value, "os_type", "ubuntu")
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# # Configuration CPU et mémoire
|
||||||
|
# cpu {
|
||||||
|
# cores = each.value.cores
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# memory {
|
||||||
|
# dedicated = each.value.memory
|
||||||
|
# swap = lookup(each.value, "swap", 512)
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# # Configuration réseau
|
||||||
|
# network_interface {
|
||||||
|
# name = "eth0"
|
||||||
|
# bridge = lookup(each.value, "network_bridge", "vmbr0")
|
||||||
|
# enabled = true
|
||||||
|
# firewall = lookup(each.value, "firewall", false)
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# # Configuration stockage
|
||||||
|
# disk {
|
||||||
|
# datastore_id = lookup(each.value, "datastore", "local-lvm")
|
||||||
|
# size = each.value.disk_gb
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# # Configuration d'initialisation
|
||||||
|
# initialization {
|
||||||
|
# hostname = "${each.key}.${var.domain_suffix}"
|
||||||
|
#
|
||||||
|
# ip_config {
|
||||||
|
# ipv4 {
|
||||||
|
# address = "${each.value.ip}/${lookup(each.value, "netmask", "24")}"
|
||||||
|
# gateway = each.value.gateway
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# dns {
|
||||||
|
# servers = lookup(each.value, "dns_servers", ["8.8.8.8", "8.8.4.4"])
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# user_account {
|
||||||
|
# keys = [file(var.admin_ssh_public_key_path)]
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
|
||||||
|
# 4. Outputs pour les conteneurs LXC
|
||||||
|
# output "lxc_info" {
|
||||||
|
# description = "Informations des conteneurs LXC créés"
|
||||||
|
# value = {
|
||||||
|
# for k, v in proxmox_virtual_environment_container.lxc_containers : k => {
|
||||||
|
# id = v.id
|
||||||
|
# name = v.hostname
|
||||||
|
# node = v.node_name
|
||||||
|
# ip = var.lxc_containers[k].ip
|
||||||
|
# status = v.status
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
|
||||||
|
# ===== SECTION COMMENTÉE - EXEMPLES DE CONFIGURATION =====
|
||||||
|
|
||||||
|
# Exemple de configuration pour les variables LXC (à ajouter dans variables.tf) :
|
||||||
|
# variable "lxc_containers" {
|
||||||
|
# description = "Configuration des conteneurs LXC à déployer"
|
||||||
|
# type = map(object({
|
||||||
|
# # Configuration de base
|
||||||
|
# vm_id = number
|
||||||
|
# template = string
|
||||||
|
# cores = number
|
||||||
|
# memory = number
|
||||||
|
# disk_gb = number
|
||||||
|
# ip = string
|
||||||
|
# gateway = string
|
||||||
|
#
|
||||||
|
# # Configuration optionnelle
|
||||||
|
# os_type = optional(string, "ubuntu")
|
||||||
|
# netmask = optional(string, "24")
|
||||||
|
# datastore = optional(string, "local-lvm")
|
||||||
|
# network_bridge = optional(string, "vmbr0")
|
||||||
|
# unprivileged = optional(bool, true)
|
||||||
|
# on_boot = optional(bool, true)
|
||||||
|
# started = optional(bool, true)
|
||||||
|
# firewall = optional(bool, false)
|
||||||
|
# swap = optional(number, 512)
|
||||||
|
# dns_servers = optional(list(string), ["8.8.8.8", "8.8.4.4"])
|
||||||
|
# tags = optional(list(string), [])
|
||||||
|
# }))
|
||||||
|
# default = {}
|
||||||
|
# }
|
||||||
|
|
||||||
|
# Exemple de configuration dans terraform.tfvars :
|
||||||
|
# lxc_containers = {
|
||||||
|
# "web-proxy" = {
|
||||||
|
# vm_id = 200
|
||||||
|
# template = "ubuntu-20.04-standard_20.04-1_amd64.tar.gz"
|
||||||
|
# cores = 1
|
||||||
|
# memory = 1024
|
||||||
|
# disk_gb = 8
|
||||||
|
# ip = "192.168.1.200"
|
||||||
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["proxy", "nginx"]
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# "monitoring-light" = {
|
||||||
|
# vm_id = 201
|
||||||
|
# template = "alpine-3.14-default_20210623_amd64.tar.xz"
|
||||||
|
# cores = 1
|
||||||
|
# memory = 512
|
||||||
|
# disk_gb = 4
|
||||||
|
# ip = "192.168.1.201"
|
||||||
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["monitoring", "lightweight"]
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
|
||||||
|
# ===== AVANTAGES DES LXC =====
|
||||||
|
#
|
||||||
|
# 1. Consommation réduite de ressources par rapport aux VMs
|
||||||
|
# 2. Démarrage plus rapide
|
||||||
|
# 3. Idéal pour les services légers (reverse proxy, monitoring, etc.)
|
||||||
|
# 4. Partage du kernel avec l'hôte = meilleure efficacité
|
||||||
|
# 5. Isolation suffisante pour la plupart des cas d'usage homelab
|
||||||
|
#
|
||||||
|
# ===== QUAND UTILISER LXC VS VM =====
|
||||||
|
#
|
||||||
|
# Utiliser LXC pour :
|
||||||
|
# - Services web légers (nginx, apache)
|
||||||
|
# - Outils de monitoring légers
|
||||||
|
# - Reverse proxy
|
||||||
|
# - Services de fichiers simples
|
||||||
|
# - Environnements de développement
|
||||||
|
#
|
||||||
|
# Utiliser VM pour :
|
||||||
|
# - Services nécessitant un kernel spécifique
|
||||||
|
# - Applications nécessitant une isolation forte
|
||||||
|
# - Kubernetes nodes
|
||||||
|
# - Bases de données critiques
|
||||||
|
# - Services Windows
|
||||||
|
|
||||||
|
# ===== TODO POUR ACTIVATION =====
|
||||||
|
#
|
||||||
|
# Pour activer ce fichier :
|
||||||
|
# 1. Décommenter le code ci-dessus
|
||||||
|
# 2. Ajouter la variable lxc_containers dans variables.tf
|
||||||
|
# 3. Configurer les templates LXC disponibles
|
||||||
|
# 4. Tester avec un conteneur simple
|
||||||
|
# 5. Documenter les templates disponibles
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
# Fichier: terraform/environments/prod/02-monitoring-services.tf
|
|
||||||
|
|
||||||
# 1. On cherche TOUS les templates disponibles sur le noeud Proxmox
|
|
||||||
data "proxmox_virtual_environment_vms" "all_templates" {
|
|
||||||
node_name = var.target_node
|
|
||||||
tags = ["template"] # On suppose que tous tes templates ont le tag "template"
|
|
||||||
}
|
|
||||||
|
|
||||||
# 2. On transforme la liste de templates en une map pratique pour les retrouver par OS
|
|
||||||
locals {
|
|
||||||
# Liste des tags d'OS que nous utilisons
|
|
||||||
known_os_tags = toset(["ubuntu", "alpine", "rocky", "debian"])
|
|
||||||
|
|
||||||
# Crée une map du type : { "ubuntu" = { vm_id = 101, name = "ubuntu-..." }, "alpine" = { ... } }
|
|
||||||
template_map = {
|
|
||||||
for t in data.proxmox_virtual_environment_vms.all_templates.vms :
|
|
||||||
one(setintersection(local.known_os_tags, t.tags)) => t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# 3. On crée nos VMs en utilisant la syntaxe correcte
|
|
||||||
resource "proxmox_virtual_environment_vm" "monitoring_vms" {
|
|
||||||
for_each = var.monitoring_vms
|
|
||||||
|
|
||||||
name = "${each.key}.lab.home.arnodo.fr"
|
|
||||||
node_name = var.target_node
|
|
||||||
tags = ["terraform-managed", "monitoring", each.value.os]
|
|
||||||
on_boot = true
|
|
||||||
|
|
||||||
agent {
|
|
||||||
enabled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
cpu {
|
|
||||||
cores = each.value.cores
|
|
||||||
type = lookup(each.value, "cpu_type", "qemu64")
|
|
||||||
}
|
|
||||||
|
|
||||||
memory {
|
|
||||||
dedicated = each.value.memory
|
|
||||||
}
|
|
||||||
|
|
||||||
disk {
|
|
||||||
interface = "scsi0"
|
|
||||||
datastore_id = "local-lvm"
|
|
||||||
size = each.value.disk_gb
|
|
||||||
}
|
|
||||||
|
|
||||||
network_device {
|
|
||||||
bridge = "vmbr0"
|
|
||||||
model = "virtio"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Bloc de clonage utilisant l'ID du template trouvé dynamiquement
|
|
||||||
clone {
|
|
||||||
vm_id = local.template_map[each.value.os].vm_id
|
|
||||||
}
|
|
||||||
|
|
||||||
# Bloc d'initialisation (Cloud-Init) simplifié
|
|
||||||
initialization {
|
|
||||||
# Configuration réseau
|
|
||||||
ip_config {
|
|
||||||
ipv4 {
|
|
||||||
address = "${each.value.ip}/24"
|
|
||||||
gateway = each.value.gateway
|
|
||||||
}
|
|
||||||
}
|
|
||||||
# Configuration de l'utilisateur
|
|
||||||
user_account {
|
|
||||||
username = "damien" # Ou un autre utilisateur par défaut
|
|
||||||
keys = [file(var.admin_ssh_public_key_path)]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
224
terraform/prod/README.md
Normal file
224
terraform/prod/README.md
Normal file
@@ -0,0 +1,224 @@
|
|||||||
|
# Terraform Configuration - Production Environment
|
||||||
|
|
||||||
|
Ce dossier contient la configuration Terraform pour déployer l'infrastructure du homelab sur Proxmox.
|
||||||
|
|
||||||
|
## Structure des fichiers
|
||||||
|
|
||||||
|
### Fichiers principaux
|
||||||
|
|
||||||
|
- **`virtual-machines.tf`** - Configuration et déploiement des machines virtuelles
|
||||||
|
- **`lxc-containers.tf`** - Configuration des conteneurs LXC (préparatoire)
|
||||||
|
- **`providers.tf`** - Configuration des providers Terraform
|
||||||
|
- **`variables.tf`** - Définition des variables
|
||||||
|
- **`terraform.tfvars`** - Valeurs des variables (à personnaliser)
|
||||||
|
- **`inventory.tf`** - Génération de l'inventaire Ansible
|
||||||
|
|
||||||
|
### Dossier templates
|
||||||
|
|
||||||
|
- **`cloud-init.yaml.tpl`** - Template de configuration Cloud-Init pour les VMs
|
||||||
|
- **`inventory.tpl`** - Template pour générer l'inventaire Ansible
|
||||||
|
|
||||||
|
## Déploiement des VMs
|
||||||
|
|
||||||
|
### Configuration de base
|
||||||
|
|
||||||
|
Les VMs sont configurées dans le fichier `terraform.tfvars` sous la variable `virtual_machines` :
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
virtual_machines = {
|
||||||
|
"nom-vm" = {
|
||||||
|
os = "debian" # OS du template à utiliser
|
||||||
|
cores = 2 # Nombre de CPU cores
|
||||||
|
memory = 4096 # RAM en MB
|
||||||
|
disk_gb = 30 # Taille du disque en GB
|
||||||
|
ip = "192.168.1.100" # Adresse IP statique
|
||||||
|
gateway = "192.168.1.254" # Passerelle réseau
|
||||||
|
tags = ["monitoring"] # Tags pour l'organisation
|
||||||
|
packages = ["curl", "vim"] # Packages à installer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Options avancées
|
||||||
|
|
||||||
|
Chaque VM peut avoir des configurations optionnelles :
|
||||||
|
|
||||||
|
```hcl
|
||||||
|
"nom-vm-avancee" = {
|
||||||
|
# Configuration de base
|
||||||
|
os = "ubuntu"
|
||||||
|
cores = 4
|
||||||
|
memory = 8192
|
||||||
|
disk_gb = 50
|
||||||
|
ip = "192.168.1.101"
|
||||||
|
gateway = "192.168.1.254"
|
||||||
|
|
||||||
|
# Options avancées
|
||||||
|
cpu_type = "host" # Type de CPU (défaut: host)
|
||||||
|
datastore = "local-lvm" # Datastore Proxmox
|
||||||
|
network_bridge = "vmbr0" # Bridge réseau
|
||||||
|
username = "admin" # Utilisateur (défaut: damien)
|
||||||
|
dns_servers = ["1.1.1.1"] # Serveurs DNS
|
||||||
|
post_install_commands = ["apt update"] # Commandes post-installation
|
||||||
|
|
||||||
|
# Disques supplémentaires
|
||||||
|
additional_disks = [
|
||||||
|
{
|
||||||
|
interface = "scsi1"
|
||||||
|
datastore = "local-lvm"
|
||||||
|
size = 100
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Templates supportés
|
||||||
|
|
||||||
|
Les templates doivent être présents sur Proxmox avec les tags appropriés :
|
||||||
|
|
||||||
|
- **ubuntu** - Ubuntu Server (tag: `ubuntu`, `template`)
|
||||||
|
- **debian** - Debian (tag: `debian`, `template`)
|
||||||
|
- **alpine** - Alpine Linux (tag: `alpine`, `template`)
|
||||||
|
- **rocky** - Rocky Linux (tag: `rocky`, `template`)
|
||||||
|
- **centos** - CentOS (tag: `centos`, `template`)
|
||||||
|
|
||||||
|
## Utilisation
|
||||||
|
|
||||||
|
### 1. Prérequis
|
||||||
|
|
||||||
|
- Terraform installé
|
||||||
|
- Accès à l'API Proxmox
|
||||||
|
- Templates VM configurés sur Proxmox
|
||||||
|
- Clés SSH configurées
|
||||||
|
|
||||||
|
### 2. Configuration
|
||||||
|
|
||||||
|
1. Copier `terraform.tfvars.example` vers `terraform.tfvars`
|
||||||
|
2. Personnaliser les valeurs dans `terraform.tfvars`
|
||||||
|
3. Vérifier les chemins des clés SSH
|
||||||
|
|
||||||
|
### 3. Déploiement
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Initialisation
|
||||||
|
terraform init
|
||||||
|
|
||||||
|
# Planification
|
||||||
|
terraform plan
|
||||||
|
|
||||||
|
# Application
|
||||||
|
terraform apply
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Vérification
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Liste des ressources
|
||||||
|
terraform state list
|
||||||
|
|
||||||
|
# Informations des VMs
|
||||||
|
terraform output vm_info
|
||||||
|
```
|
||||||
|
|
||||||
|
## Intégration Ansible
|
||||||
|
|
||||||
|
L'inventaire Ansible est généré automatiquement dans `../../ansible/inventory/hosts.ini`.
|
||||||
|
|
||||||
|
### Groupes générés automatiquement
|
||||||
|
|
||||||
|
- **Par OS** : `ubuntu`, `debian`, `alpine`, etc.
|
||||||
|
- **Par tags** : groupes basés sur les tags des VMs
|
||||||
|
- **Par pattern de nom** : `monitoring`, `web`, `database`, `kubernetes`
|
||||||
|
|
||||||
|
### Utilisation avec Ansible
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Test de connectivité
|
||||||
|
ansible all -i inventory/hosts.ini -m ping
|
||||||
|
|
||||||
|
# Playbook sur un groupe
|
||||||
|
ansible-playbook -i inventory/hosts.ini playbooks/common.yml --limit monitoring
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conteneurs LXC (Futur)
|
||||||
|
|
||||||
|
Le fichier `lxc-containers.tf` est préparé pour le déploiement de conteneurs LXC.
|
||||||
|
Il n'est pas encore fonctionnel mais servira de base pour le développement futur.
|
||||||
|
|
||||||
|
### Avantages des LXC
|
||||||
|
|
||||||
|
- Consommation réduite de ressources
|
||||||
|
- Démarrage plus rapide
|
||||||
|
- Idéal pour les services légers
|
||||||
|
|
||||||
|
### Cas d'usage recommandés
|
||||||
|
|
||||||
|
**LXC pour :**
|
||||||
|
- Reverse proxy (nginx)
|
||||||
|
- Monitoring léger
|
||||||
|
- Services web simples
|
||||||
|
- Environnements de développement
|
||||||
|
|
||||||
|
**VM pour :**
|
||||||
|
- Kubernetes nodes
|
||||||
|
- Bases de données critiques
|
||||||
|
- Services nécessitant une isolation forte
|
||||||
|
- Applications avec besoins kernel spécifiques
|
||||||
|
|
||||||
|
## Maintenance
|
||||||
|
|
||||||
|
### Mise à jour des VMs
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Modifier terraform.tfvars
|
||||||
|
# Puis appliquer les changements
|
||||||
|
terraform plan
|
||||||
|
terraform apply
|
||||||
|
```
|
||||||
|
|
||||||
|
### Suppression d'une VM
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Retirer la VM de terraform.tfvars
|
||||||
|
# Puis appliquer
|
||||||
|
terraform apply
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sauvegarde de l'état
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Sauvegarde manuelle
|
||||||
|
cp terraform.tfstate terraform.tfstate.backup.$(date +%Y%m%d)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Dépannage
|
||||||
|
|
||||||
|
### Problèmes courants
|
||||||
|
|
||||||
|
1. **Template non trouvé** : Vérifier que le template existe sur Proxmox avec les bons tags
|
||||||
|
2. **IP déjà utilisée** : Vérifier les conflits d'adresses IP
|
||||||
|
3. **Clé SSH** : Vérifier le chemin vers la clé publique SSH
|
||||||
|
|
||||||
|
### Logs utiles
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Logs Terraform détaillés
|
||||||
|
TF_LOG=DEBUG terraform apply
|
||||||
|
|
||||||
|
# État des ressources
|
||||||
|
terraform show
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sécurité
|
||||||
|
|
||||||
|
- Les clés SSH privées ne sont jamais stockées dans le code
|
||||||
|
- Les tokens API sont dans des variables d'environnement
|
||||||
|
- Les VMs sont configurées avec des utilisateurs non-root
|
||||||
|
- SSH par mot de passe est désactivé
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
Pour toute question ou problème, consulter :
|
||||||
|
- Documentation Terraform Proxmox Provider
|
||||||
|
- Logs Proxmox (`/var/log/pveproxy/`)
|
||||||
|
- État Terraform (`terraform show`)
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
# inventory.tf
|
|
||||||
|
|
||||||
# On fusionne toutes nos ressources VM en une seule map
|
|
||||||
locals {
|
|
||||||
all_lab_vms = merge(
|
|
||||||
proxmox_virtual_environment_vm.monitoring_vms,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
locals {
|
|
||||||
# On boucle sur chaque VM créée par Terraform pour enrichir les données.
|
|
||||||
vms_with_metadata = {
|
|
||||||
for key, vm in local.all_lab_vms : key => {
|
|
||||||
# On garde les informations de base
|
|
||||||
name : vm.name
|
|
||||||
ip_address : var.monitoring_vms[key].ip
|
|
||||||
proxmox_node : vm.node_name
|
|
||||||
|
|
||||||
# On trouve le tag de l'OS de manière robuste
|
|
||||||
# - setintersection compare les tags de la VM avec notre liste d'OS connus.
|
|
||||||
# - Le résultat est une liste avec l'élément commun (ex: ["ubuntu"]).
|
|
||||||
# - one() prend cet unique élément et échoue si on trouve 0 ou plus d'un OS, ce qui est une sécurité.
|
|
||||||
os_distro : one(setintersection(local.known_os_tags, toset(vm.tags)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Création du fichier d'inventaire pour Ansible
|
|
||||||
resource "local_file" "ansible_inventory" {
|
|
||||||
filename = "${path.module}/../../ansible/inventory/01_lab_hosts.yml"
|
|
||||||
|
|
||||||
# On envoie notre nouvelle map enrichie au template !
|
|
||||||
content = templatefile("${path.module}/templates/inventory.yml.tftpl", {
|
|
||||||
vms = local.vms_with_metadata # On utilise la nouvelle variable locale
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,47 +1,199 @@
|
|||||||
# Fichier: terraform/prod/terraform.tfvars.example
|
# terraform.tfvars.example
|
||||||
|
# Fichier d'exemple pour la configuration Terraform du homelab
|
||||||
#
|
#
|
||||||
# Copiez ce fichier vers terraform.tfvars et remplissez les valeurs appropriées.
|
# Instructions :
|
||||||
# Le fichier terraform.tfvars est ignoré par Git (voir .gitignore) pour protéger les informations sensibles.
|
# 1. Copiez ce fichier vers terraform.tfvars
|
||||||
|
# 2. Personnalisez les valeurs selon votre environnement
|
||||||
|
# 3. Le fichier terraform.tfvars est ignoré par Git pour la sécurité
|
||||||
|
|
||||||
# Le nom du noeud Proxmox sur lequel déployer les VMs.
|
#############################
|
||||||
# Par défaut : "pve01"
|
# Configuration Proxmox
|
||||||
# target_node = "pve01"
|
#############################
|
||||||
|
|
||||||
# URL du noeud Proxmox sur lequel déployer les VMs. (Requis)
|
# URL de votre serveur Proxmox (REQUIS)
|
||||||
# Exemple : "https://votre-proxmox-ip-ou-hostname:8006"
|
proxmox_url = "https://pve01.lab.home.arnodo.fr:8006"
|
||||||
proxmox_url = "https://proxmox.example.com:8006"
|
|
||||||
|
|
||||||
# Token pour accéder à l'API Proxmox. (Requis et sensible)
|
# Token API Proxmox (REQUIS - À configurer dans Proxmox)
|
||||||
# Créez un token dans Proxmox (Datacenter -> Permissions -> API Tokens)
|
# Créer dans : Datacenter -> Permissions -> API Tokens
|
||||||
# Exemple : "utilisateur@pve!nom_token=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
|
|
||||||
proxmox_api_token = "terraform@pve!terraform_token=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
|
proxmox_api_token = "terraform@pve!terraform_token=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
|
||||||
|
|
||||||
# Chemin vers le fichier de la clé SSH publique à autoriser pour l'utilisateur admin sur les VMs.
|
# Nœud Proxmox cible (défaut: pve01)
|
||||||
# Par défaut : "~/.ssh/keys/id_rsa.pub"
|
target_node = "pve01"
|
||||||
# admin_ssh_public_key_path = "~/.ssh/id_rsa.pub"
|
|
||||||
|
|
||||||
# Une map décrivant les VMs pour la supervision.
|
#############################
|
||||||
# Vous pouvez laisser cette map vide si vous ne souhaitez pas déployer de VMs de supervision
|
# Configuration réseau
|
||||||
# ou la remplir comme dans l'exemple ci-dessous.
|
#############################
|
||||||
#
|
|
||||||
# monitoring_vms = {
|
# Suffixe de domaine pour les VMs
|
||||||
# "prometheus" = {
|
domain_suffix = "lab.home.arnodo.fr"
|
||||||
# os = "ubuntu" # Nom du template ou de l'image ISO
|
|
||||||
|
#############################
|
||||||
|
# Configuration SSH
|
||||||
|
#############################
|
||||||
|
|
||||||
|
# Chemin vers votre clé SSH publique
|
||||||
|
admin_ssh_public_key_path = "~/.ssh/keys/id_rsa.pub"
|
||||||
|
|
||||||
|
# Utilisateur par défaut sur les VMs
|
||||||
|
default_vm_user = "damien"
|
||||||
|
|
||||||
|
#############################
|
||||||
|
# Machines virtuelles
|
||||||
|
#############################
|
||||||
|
|
||||||
|
virtual_machines = {
|
||||||
|
# Exemple VM de monitoring
|
||||||
|
"vision" = {
|
||||||
|
os = "debian"
|
||||||
|
cores = 2
|
||||||
|
memory = 4096
|
||||||
|
disk_gb = 30
|
||||||
|
ip = "192.168.1.161"
|
||||||
|
gateway = "192.168.1.254"
|
||||||
|
tags = ["monitoring", "grafana"]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Exemples d'autres VMs (décommentez et adaptez selon vos besoins)
|
||||||
|
|
||||||
|
# # Serveur web
|
||||||
|
# "webserver" = {
|
||||||
|
# os = "ubuntu"
|
||||||
# cores = 2
|
# cores = 2
|
||||||
# cpu_type = "host" # Type de CPU (kvm64, host, etc.)
|
# memory = 2048
|
||||||
# memory = 2048 # En Mo
|
# disk_gb = 20
|
||||||
# disk_gb = 20 # En Go
|
# ip = "192.168.1.162"
|
||||||
# ip = "192.168.1.50/24" # IP statique avec le masque CIDR
|
# gateway = "192.168.1.254"
|
||||||
# gateway = "192.168.1.1" # Passerelle
|
# tags = ["web", "nginx"]
|
||||||
# },
|
# }
|
||||||
# "grafana" = {
|
|
||||||
|
# # Base de données
|
||||||
|
# "database" = {
|
||||||
# os = "debian"
|
# os = "debian"
|
||||||
|
# cores = 4
|
||||||
|
# memory = 8192
|
||||||
|
# disk_gb = 50
|
||||||
|
# ip = "192.168.1.163"
|
||||||
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["database", "postgresql"]
|
||||||
|
#
|
||||||
|
# # Disque supplémentaire pour les données
|
||||||
|
# additional_disks = [
|
||||||
|
# {
|
||||||
|
# interface = "scsi1"
|
||||||
|
# datastore = "local-lvm"
|
||||||
|
# size = 100
|
||||||
|
# }
|
||||||
|
# ]
|
||||||
|
# }
|
||||||
|
|
||||||
|
# # Node Kubernetes master
|
||||||
|
# "k8s-master" = {
|
||||||
|
# os = "ubuntu"
|
||||||
|
# cores = 4
|
||||||
|
# memory = 4096
|
||||||
|
# disk_gb = 40
|
||||||
|
# ip = "192.168.1.170"
|
||||||
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["kubernetes", "master"]
|
||||||
|
# }
|
||||||
|
|
||||||
|
# # Node Kubernetes worker
|
||||||
|
# "k8s-worker1" = {
|
||||||
|
# os = "ubuntu"
|
||||||
|
# cores = 4
|
||||||
|
# memory = 8192
|
||||||
|
# disk_gb = 60
|
||||||
|
# ip = "192.168.1.171"
|
||||||
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["kubernetes", "worker"]
|
||||||
|
# }
|
||||||
|
|
||||||
|
# # Serveur de fichiers/NAS
|
||||||
|
# "fileserver" = {
|
||||||
|
# os = "debian"
|
||||||
|
# cores = 2
|
||||||
|
# memory = 4096
|
||||||
|
# disk_gb = 40
|
||||||
|
# ip = "192.168.1.180"
|
||||||
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["storage", "nas"]
|
||||||
|
#
|
||||||
|
# # Multiples disques pour le stockage
|
||||||
|
# additional_disks = [
|
||||||
|
# {
|
||||||
|
# interface = "scsi1"
|
||||||
|
# datastore = "local-lvm"
|
||||||
|
# size = 500
|
||||||
|
# },
|
||||||
|
# {
|
||||||
|
# interface = "scsi2"
|
||||||
|
# datastore = "local-lvm"
|
||||||
|
# size = 500
|
||||||
|
# }
|
||||||
|
# ]
|
||||||
|
# }
|
||||||
|
|
||||||
|
# # VM de développement
|
||||||
|
# "devbox" = {
|
||||||
|
# os = "ubuntu"
|
||||||
|
# cores = 4
|
||||||
|
# memory = 8192
|
||||||
|
# disk_gb = 80
|
||||||
|
# ip = "192.168.1.190"
|
||||||
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["development", "docker"]
|
||||||
|
# }
|
||||||
|
}
|
||||||
|
|
||||||
|
#############################
|
||||||
|
# Conteneurs LXC (Futur)
|
||||||
|
#############################
|
||||||
|
|
||||||
|
# Configuration préparée pour les conteneurs LXC
|
||||||
|
# Actuellement désactivée - sera implémentée dans une future version
|
||||||
|
|
||||||
|
# lxc_containers = {
|
||||||
|
# # Exemple proxy léger
|
||||||
|
# "nginx-proxy" = {
|
||||||
|
# vm_id = 200
|
||||||
|
# template = "ubuntu-20.04-standard"
|
||||||
# cores = 1
|
# cores = 1
|
||||||
# cpu_type = "kvm64"
|
|
||||||
# memory = 1024
|
# memory = 1024
|
||||||
# disk_gb = 10
|
# disk_gb = 8
|
||||||
# ip = "192.168.1.51/24"
|
# ip = "192.168.1.200"
|
||||||
# gateway = "192.168.1.1"
|
# gateway = "192.168.1.254"
|
||||||
|
# tags = ["proxy", "nginx"]
|
||||||
# }
|
# }
|
||||||
# }
|
# }
|
||||||
|
|
||||||
|
#############################
|
||||||
|
# Maintien compatibilité (deprecated)
|
||||||
|
#############################
|
||||||
|
|
||||||
|
# Ancienne variable - sera supprimée dans une future version
|
||||||
|
# Utilisez virtual_machines à la place
|
||||||
monitoring_vms = {}
|
monitoring_vms = {}
|
||||||
|
|
||||||
|
#############################
|
||||||
|
# Notes de configuration
|
||||||
|
#############################
|
||||||
|
|
||||||
|
# Templates requis sur Proxmox :
|
||||||
|
# - Template Ubuntu avec tags : ["ubuntu", "template"]
|
||||||
|
# - Template Debian avec tags : ["debian", "template"]
|
||||||
|
# - Template Alpine avec tags : ["alpine", "template"]
|
||||||
|
# - Template Rocky avec tags : ["rocky", "template"]
|
||||||
|
# - Template CentOS avec tags : ["centos", "template"]
|
||||||
|
|
||||||
|
# Réseau recommandé :
|
||||||
|
# - VLAN/Bridge : vmbr0
|
||||||
|
# - Plage IP : 192.168.1.0/24
|
||||||
|
# - Passerelle : 192.168.1.254
|
||||||
|
# - DNS : 8.8.8.8, 8.8.4.4 (ou votre DNS local)
|
||||||
|
|
||||||
|
# Ressources recommandées par type de service :
|
||||||
|
# - Monitoring : 2 CPU, 4GB RAM, 30GB disque
|
||||||
|
# - Web server : 2 CPU, 2GB RAM, 20GB disque
|
||||||
|
# - Database : 4 CPU, 8GB RAM, 50GB+ disque
|
||||||
|
# - Kubernetes : 4 CPU, 4GB+ RAM, 40GB+ disque
|
||||||
|
# - Development : 4 CPU, 8GB RAM, 80GB disque
|
||||||
|
|||||||
@@ -19,19 +19,51 @@ variable "proxmox_api_token" {
|
|||||||
variable "admin_ssh_public_key_path" {
|
variable "admin_ssh_public_key_path" {
|
||||||
description = "Chemin vers le fichier de la clé SSH publique à autoriser."
|
description = "Chemin vers le fichier de la clé SSH publique à autoriser."
|
||||||
type = string
|
type = string
|
||||||
default = "~/.ssh/keys/id_rsa.pub"
|
default = "~/.ssh/id_rsa.pub"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "monitoring_vms" {
|
variable "domain_suffix" {
|
||||||
description = "Une map décrivant les VMs pour la supervision."
|
description = "Suffixe de domaine pour les VMs (ex: lab.home.arnodo.fr)"
|
||||||
|
type = string
|
||||||
|
default = "lab.home.arnodo.fr"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "default_vm_user" {
|
||||||
|
description = "Utilisateur par défaut pour les VMs"
|
||||||
|
type = string
|
||||||
|
default = "root"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "virtual_machines" {
|
||||||
|
description = "Configuration des machines virtuelles à déployer"
|
||||||
type = map(object({
|
type = map(object({
|
||||||
|
# Configuration de base
|
||||||
os = string
|
os = string
|
||||||
cores = number
|
cores = number
|
||||||
cpu_type = string
|
|
||||||
memory = number
|
memory = number
|
||||||
disk_gb = number
|
disk_gb = number
|
||||||
ip = string
|
ip = string
|
||||||
gateway = string
|
gateway = string
|
||||||
|
|
||||||
|
# Configuration optionnelle
|
||||||
|
cpu_type = optional(string, "host")
|
||||||
|
netmask = optional(string, "24")
|
||||||
|
datastore = optional(string, "local-lvm")
|
||||||
|
disk_format = optional(string, "raw")
|
||||||
|
network_bridge = optional(string, "vmbr0")
|
||||||
|
network_model = optional(string, "virtio")
|
||||||
|
username = optional(string)
|
||||||
|
on_boot = optional(bool, true)
|
||||||
|
dns_servers = optional(list(string), ["8.8.8.8", "8.8.4.4"])
|
||||||
|
tags = optional(list(string), [])
|
||||||
|
|
||||||
|
# Disques supplémentaires (optionnel)
|
||||||
|
additional_disks = optional(list(object({
|
||||||
|
interface = string
|
||||||
|
datastore = string
|
||||||
|
size = number
|
||||||
|
format = optional(string, "raw")
|
||||||
|
})), [])
|
||||||
}))
|
}))
|
||||||
default = {}
|
default = {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user