Initial commit: Terraform configuration for Proxmox VM with Loki and Promtail
This commit is contained in:
128
main.tf
Normal file
128
main.tf
Normal file
@@ -0,0 +1,128 @@
|
||||
|
||||
terraform {
|
||||
required_version = ">= 1.0"
|
||||
required_providers {
|
||||
proxmox = {
|
||||
source = "telmate/proxmox"
|
||||
version = "~> 2.9"
|
||||
}
|
||||
tls = {
|
||||
source = "hashicorp/tls"
|
||||
version = "~> 4.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Proxmox Provider Configuration
|
||||
provider "proxmox" {
|
||||
pm_api_url = var.proxmox_api_url
|
||||
pm_api_token_id = var.proxmox_token_id
|
||||
pm_api_token_secret = var.proxmox_token_secret
|
||||
pm_tls_insecure = var.proxmox_tls_insecure
|
||||
}
|
||||
|
||||
# Generate SSH Key Pair
|
||||
resource "tls_private_key" "vm_ssh_key" {
|
||||
algorithm = "RSA"
|
||||
rsa_bits = 4096
|
||||
}
|
||||
|
||||
# Proxmox VM Resource
|
||||
resource "proxmox_vm_qemu" "loki_vm" {
|
||||
name = var.vm_name
|
||||
target_node = var.target_node
|
||||
clone = var.template_name
|
||||
|
||||
# VM Resources
|
||||
cores = var.vm_cores
|
||||
sockets = 1
|
||||
memory = var.vm_memory
|
||||
|
||||
# Boot Configuration
|
||||
boot = "order=scsi0"
|
||||
agent = 1
|
||||
onboot = true
|
||||
|
||||
# Disk Configuration
|
||||
disk {
|
||||
size = var.vm_disk_size
|
||||
type = "scsi"
|
||||
storage = var.storage
|
||||
iothread = 1
|
||||
}
|
||||
|
||||
# Network Configuration
|
||||
network {
|
||||
model = "virtio"
|
||||
bridge = var.network_bridge
|
||||
}
|
||||
|
||||
# Cloud-init Configuration
|
||||
os_type = "cloud-init"
|
||||
ipconfig0 = "ip=dhcp"
|
||||
|
||||
# SSH Keys
|
||||
sshkeys = tls_private_key.vm_ssh_key.public_key_openssh
|
||||
|
||||
# Wait for cloud-init to complete
|
||||
lifecycle {
|
||||
ignore_changes = [
|
||||
network,
|
||||
]
|
||||
}
|
||||
|
||||
# Connection settings for provisioners
|
||||
connection {
|
||||
type = "ssh"
|
||||
user = var.ssh_user
|
||||
private_key = tls_private_key.vm_ssh_key.private_key_pem
|
||||
host = self.default_ipv4_address
|
||||
timeout = "5m"
|
||||
}
|
||||
|
||||
# Wait for VM to be ready
|
||||
provisioner "remote-exec" {
|
||||
inline = [
|
||||
"cloud-init status --wait",
|
||||
"echo 'VM is ready'",
|
||||
]
|
||||
}
|
||||
|
||||
# Copy Loki configuration
|
||||
provisioner "file" {
|
||||
source = "${path.module}/loki-config.yaml"
|
||||
destination = "/tmp/loki-config.yaml"
|
||||
}
|
||||
|
||||
# Copy Promtail configuration
|
||||
provisioner "file" {
|
||||
source = "${path.module}/promtail-config.yaml"
|
||||
destination = "/tmp/promtail-config.yaml"
|
||||
}
|
||||
|
||||
# Copy Loki installation script
|
||||
provisioner "file" {
|
||||
source = "${path.module}/install_loki.sh"
|
||||
destination = "/tmp/install_loki.sh"
|
||||
}
|
||||
|
||||
# Copy Promtail installation script
|
||||
provisioner "file" {
|
||||
source = "${path.module}/install_promtail.sh"
|
||||
destination = "/tmp/install_promtail.sh"
|
||||
}
|
||||
|
||||
# Execute installation scripts
|
||||
provisioner "remote-exec" {
|
||||
inline = [
|
||||
"echo 'Starting installation...'",
|
||||
"sudo chmod +x /tmp/install_loki.sh",
|
||||
"sudo chmod +x /tmp/install_promtail.sh",
|
||||
"echo 'Installing Grafana Loki...'",
|
||||
"sudo /tmp/install_loki.sh",
|
||||
"echo 'Installing Promtail...'",
|
||||
"sudo /tmp/install_promtail.sh",
|
||||
"echo 'Installation completed!'",
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user