Add Cloud-Init automation with Tailscale

The commit rephrases the README to focus on Cloud-Init setup and removes
unused Ansible code, making Tailscale configuration automatic.

Subject line: Replace Ansible with automated Cloud-Init configuration

Body: Refactors infrastructure bootstrapping to leverage Cloud-Init
instead of Ansible for initial VM setup, particularly with respect to
Tailscale installation and authentication.

This simplifies the architecture by: - Removing manual post-provisioning
Ansible steps - Automating Tailscale setup via Cloud-Init - Updating
documentation to reflect new workflow
This commit is contained in:
darnodo
2025-07-03 09:28:09 +02:00
parent 26d97cbcfb
commit 7cd93ba978
23 changed files with 119 additions and 797 deletions

View File

@@ -7,7 +7,7 @@ data "proxmox_virtual_environment_vms" "templates" {
tags = ["template"]
}
# 2. Mapping des templates et préparation des données pour Ansible
# 2. Mapping des templates
locals {
# OS supportés
supported_os = toset(["ubuntu", "debian", "alpine", "rocky", "centos"])
@@ -18,18 +18,6 @@ locals {
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
@@ -87,8 +75,7 @@ resource "proxmox_virtual_environment_vm" "vms" {
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.
# On utilise le bloc 'initialization' pour configurer Cloud-Init
initialization {
# Configuration IP
ip_config {
@@ -98,15 +85,12 @@ resource "proxmox_virtual_environment_vm" "vms" {
}
}
# Configuration utilisateur
user_account {
username = lookup(each.value, "username", var.default_vm_user)
keys = [file(var.admin_ssh_public_key_path)]
}
# Données Cloud-Init via le fichier de template
user_data_file_id = proxmox_virtual_environment_file.cloud_config[each.key].id
}
}
# 5. Outputs pour récupérer les informations des VMs
# 4. Outputs pour récupérer les informations des VMs
output "vm_info" {
description = "Informations des machines virtuelles créées"
value = {
@@ -119,14 +103,3 @@ output "vm_info" {
}
}
}
# 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]
}

View File

@@ -0,0 +1,26 @@
# Fichier: terraform/prod/02-cloud-init.tf
resource "proxmox_virtual_environment_file" "cloud_config" {
for_each = var.virtual_machines
node_name = var.target_node
datastore_id = "local"
content_type = "snippets"
source_raw {
data = templatefile("${path.module}/../../cloud-init/user-data.yml.tftpl", {
# Variables pour définir les informations de l'utilisateur
username = lookup(each.value, "username", var.default_vm_user)
ssh_key = file(var.admin_ssh_public_key_path)
# Variables pour définir les informations de la VM
hostname = "${each.key}.${var.domain_suffix}"
# Nouvelles variables pour Tailscale
vm_name = each.key # Le nom de la VM (ex: "web-01")
tailscale_auth_key = var.tailscale_auth_key
})
file_name = "${each.key}-cloud-init.yml"
}
}

View File

@@ -58,7 +58,6 @@ Chaque VM peut avoir des configurations optionnelles :
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

View File

@@ -1,13 +0,0 @@
# Ansible Inventory - Généré par Terraform le ${timestamp()}
# Ne pas modifier ce fichier manuellement, il sera écrasé.
all:
children:
lab:
hosts:
%{ for key, vm_data in vms }
${vm_data.name}:
ansible_host: ${vm_data.ip_address}
os_distro: "${vm_data.os_distro}"
proxmox_node: "${vm_data.proxmox_node}"
%{ endfor }

View File

@@ -14,6 +14,13 @@ variable "proxmox_url" {
variable "proxmox_api_token" {
description = "Token pour accéder à l'API Proxmox"
type = string
sensitive = true
}
variable "tailscale_auth_key" {
description = "Clé d'authentification Tailscale pour enregistrer les noeuds."
type = string
sensitive = true
}
variable "admin_ssh_public_key_path" {
@@ -54,7 +61,6 @@ variable "virtual_machines" {
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)