From f2a6f9ffde67c78d0115178168f03bd04178dd82 Mon Sep 17 00:00:00 2001 From: Littlefoot Date: Mon, 19 May 2025 02:26:42 +0000 Subject: [PATCH] Works for spinning up a VM --- Cloud-Init VM/main.tf | 106 ++++++++++++++++++++++++++++++++++++++++++ Cloud-Init VM/vars.tf | 83 +++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 Cloud-Init VM/main.tf create mode 100644 Cloud-Init VM/vars.tf diff --git a/Cloud-Init VM/main.tf b/Cloud-Init VM/main.tf new file mode 100644 index 0000000..b8b936b --- /dev/null +++ b/Cloud-Init VM/main.tf @@ -0,0 +1,106 @@ +# Initializes a new Proxmox VM given a specific cloud-init image, and attempts to enroll it in FreeIPA +terraform { + required_providers { + proxmox = { + source = "bpg/proxmox" + version = "0.78.0" + } + } +} + +provider "proxmox" { + endpoint = var.api_url + api_token = "${var.proxmox_token_id}=${var.proxmox_token_secret}" + insecure = true + + ssh { + agent = false + username = var.proxmox_ssh_username + password = var.proxmox_ssh_password + } +} + +resource "proxmox_virtual_environment_file" "user_data_cloud_config" { + content_type = "snippets" + datastore_id = "local" + node_name = var.proxmox_node_name + + source_raw { + data = <<-EOF + #cloud-config + hostname: ${var.vm_hostname} + fqdn: ${var.vm_hostname} + prefer_fqdn_over_hostname: true + package_update: true + packages: + - qemu-guest-agent + - net-tools + - curl + - freeipa-client + runcmd: + - systemctl enable qemu-guest-agent + - systemctl start qemu-guest-agent + - ipa-client-install --mkhomedir --unattended --principal ${var.freeipa_enrollment_principal} --password ${var.freeipa_enrollment_password} + - echo "done" > /tmp/cloud-config.done + EOF + + file_name = "user-data-cloud-config.yaml" + } +} + +resource "proxmox_virtual_environment_download_file" "vm_cloud_image" { + content_type = "iso" + datastore_id = "local" + node_name = var.proxmox_node_name + + url = var.image_url + file_name = var.image_file_name +} + +resource "proxmox_virtual_environment_vm" "ubuntu_vm" { + name = var.vm_hostname + node_name = var.proxmox_node_name + + agent { + enabled = true + } + + cpu { + cores = var.cores + type ="x86-64-v2-AES" + } + + memory { + dedicated = var.memory + } + + disk { + datastore_id = var.disk_storage + file_id = proxmox_virtual_environment_download_file.vm_cloud_image.id + interface = "scsi0" + iothread = false + discard = "ignore" + size = var.disk_size + } + + initialization { + ip_config { + ipv4 { + address = var.ipv4_address + gateway = var.ipv4_gateway + } + } + + dns { + domain = var.dns_domain + servers = var.dns_servers + } + + user_data_file_id = proxmox_virtual_environment_file.user_data_cloud_config.id + } + + network_device { + bridge = var.network_bridge_device + } + +} \ No newline at end of file diff --git a/Cloud-Init VM/vars.tf b/Cloud-Init VM/vars.tf new file mode 100644 index 0000000..4a330d2 --- /dev/null +++ b/Cloud-Init VM/vars.tf @@ -0,0 +1,83 @@ +variable "vm_hostname" { + default = "tofutest1.internal.irva.xyz" +} + +variable "proxmox_node_name" { + default = "opatos" +} + +variable "image_url" { + default = "https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2" +} + +variable "image_file_name" { + default = "AlmaLinux-9.img" +} + +variable "cores" { + default = 4 +} + +# In MiBs +variable "memory" { + default = 4096 +} + +# In GiBs +variable "disk_size" { + default = 20 +} + +variable "disk_storage" { + default = "samsung" +} + +# Should be CIDR notation (xxx.xxx.xxx.xxx/yy) +variable "ipv4_address" { + default = "192.168.20.7/24" +} + +variable "ipv4_gateway" { + default = "192.168.20.1" +} + +variable "network_bridge_device" { + default = "vmbr1" +} + +# For FreeIPA enrollment +variable "freeipa_enrollment_principal" { + default = "enrollment_admin@INTERNAL.IRVA.XYZ" +} + +variable "dns_servers" { + default = ["192.168.20.2"] +} + +variable "dns_domain" { + default = "internal.irva.xyz" +} + +#Provide the url of the host you would like the API to communicate on. +#It is safe to default to setting this as the URL for what you used +#as your `proxmox_host`, although they can be different +variable "api_url" { + default = "https://192.168.2.224:8006/" +} + +# Secrets + +variable "proxmox_token_secret" { +} + +variable "proxmox_token_id" { +} + +variable "freeipa_enrollment_password" { +} + +variable "proxmox_ssh_username" { +} + +variable "proxmox_ssh_password" { +} \ No newline at end of file