terraform { required_providers { proxmox = { source = "bpg/proxmox" version = "0.86.0" } } } data "local_file" "ssh_public_key" { filename = var.ssh_pubkey_path } data "local_file" "proxmox_ssh_private_key" { filename = var.proxmox_ssh_privkey_path } provider "proxmox" { endpoint = var.proxmox_endpoint username = var.proxmox_user password = var.proxmox_password insecure = true ssh { agent = true username = var.proxmox_ssh_username private_key = trimspace(data.local_file.proxmox_ssh_private_key.content) } } resource "proxmox_virtual_environment_download_file" "ubuntu_cloud_image" { content_type = "import" datastore_id = var.proxmox_datastore node_name = var.proxmox_node url = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" file_name = "noble-server-cloudimg-amd64.qcow2" } resource "proxmox_virtual_environment_file" "user_data_cloud_config" { content_type = "snippets" datastore_id = var.proxmox_datastore node_name = var.proxmox_node source_raw { data = <<-EOF #cloud-config hostname: ${var.vm_name} timezone: America/Santiago users: - default - name: ubuntu groups: - sudo shell: /bin/bash ssh_authorized_keys: - ${trimspace(data.local_file.ssh_public_key.content)} sudo: ALL=(ALL) NOPASSWD:ALL package_update: true packages: - qemu-guest-agent - net-tools - curl runcmd: - systemctl enable qemu-guest-agent - systemctl start qemu-guest-agent - echo "done" > /tmp/cloud-config.done EOF file_name = "user-data-cloud-config.yaml" } } resource "proxmox_virtual_environment_vm" "pihole" { name = var.vm_name node_name = var.proxmox_node agent { enabled = true } cpu { cores = 1 type = "host" } memory { dedicated = 2048 } disk { datastore_id = var.vm_datastore import_from = proxmox_virtual_environment_download_file.ubuntu_cloud_image.id interface = "virtio0" iothread = true discard = "on" size = 20 } initialization { datastore_id = var.vm_datastore ip_config { ipv4 { address = "${var.vm_address}/${var.vm_cidr}" gateway = var.vm_gateway } } user_data_file_id = proxmox_virtual_environment_file.user_data_cloud_config.id } network_device { bridge = var.bridge } } resource "local_file" "ansible_inventory" { filename = "${path.module}/../ansible/inventory.yaml" content = <<-YAML all: children: servers: hosts: ${var.vm_name}: ansible_host: ${var.vm_address} ansible_user: ubuntu ansible_ssh_private_key_file: ${var.ssh_privkey_path} YAML depends_on = [proxmox_virtual_environment_vm.pihole] }