Created
June 27, 2025 16:34
-
-
Save nickel/3c4c560efc180981304f7d431e1a01d9 to your computer and use it in GitHub Desktop.
Proxmox setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script para crear un template de Ubuntu 22.04 en Proxmox | |
# Este script debe ejecutarse en el servidor Proxmox | |
set -e | |
# Variables configurables | |
TEMPLATE_ID=${1:-9000} | |
TEMPLATE_NAME="ubuntu-2204-template" | |
STORAGE="local-lvm" | |
BRIDGE="vmbr0" | |
# Colors | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
RED='\033[0;31m' | |
NC='\033[0m' | |
log() { | |
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}" | |
} | |
warn() { | |
echo -e "${YELLOW}[WARNING] $1${NC}" | |
} | |
error() { | |
echo -e "${RED}[ERROR] $1${NC}" | |
exit 1 | |
} | |
if [ "$EUID" -ne 0 ]; then | |
error "Este script debe ejecutarse como root" | |
fi | |
log "Creando template de Ubuntu 22.04 para Proxmox..." | |
# Descargar imagen de Ubuntu 22.04 | |
log "Descargando imagen de Ubuntu 22.04..." | |
cd /var/lib/vz/template/iso/ | |
if [ ! -f "ubuntu-22.04-server-cloudimg-amd64.img" ]; then | |
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img -O ubuntu-22.04-server-cloudimg-amd64.img | |
fi | |
# Crear VM | |
log "Creando VM con ID $TEMPLATE_ID..." | |
qm create $TEMPLATE_ID --memory 2048 --core 2 --name $TEMPLATE_NAME --net0 virtio,bridge=$BRIDGE | |
# Importar disco | |
log "Importando disco..." | |
qm importdisk $TEMPLATE_ID ubuntu-22.04-server-cloudimg-amd64.img $STORAGE | |
# Configurar VM | |
log "Configurando VM..." | |
qm set $TEMPLATE_ID --scsihw virtio-scsi-pci --scsi0 $STORAGE:vm-$TEMPLATE_ID-disk-0 | |
qm set $TEMPLATE_ID --boot c --bootdisk scsi0 | |
qm set $TEMPLATE_ID --ide2 $STORAGE:cloudinit | |
qm set $TEMPLATE_ID --serial0 socket --vga serial0 | |
qm set $TEMPLATE_ID --agent enabled=1 | |
# Configurar cloud-init | |
log "Configurando cloud-init..." | |
qm set $TEMPLATE_ID --ciuser ubuntu | |
qm set $TEMPLATE_ID --cipassword $(openssl rand -base64 32) | |
qm set $TEMPLATE_ID --sshkey ~/.ssh/authorized_keys 2>/dev/null || warn "No se encontró ~/.ssh/authorized_keys" | |
qm set $TEMPLATE_ID --ipconfig0 ip=dhcp | |
# Convertir a template | |
log "Convirtiendo VM a template..." | |
qm template $TEMPLATE_ID | |
log "Template '$TEMPLATE_NAME' creado exitosamente con ID $TEMPLATE_ID" | |
log "Ahora puedes usar este template en tu configuración de Terraform" | |
echo "" | |
echo "Configuración sugerida para terraform.tfvars:" | |
echo "template_name = \"$TEMPLATE_NAME\"" | |
echo "" | |
echo "Opcional: Añadir tu clave SSH pública al template:" | |
echo "qm set $TEMPLATE_ID --sshkey <(cat ~/.ssh/id_rsa.pub)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment