delete provider from module, local in main

This commit is contained in:
CC
2026-09-12 15:17:04 +01:00
parent b2c0a925af
commit 1041be3770
12 changed files with 59 additions and 371 deletions

View File

@@ -1,18 +1,18 @@
terraform { terraform {
backend "s3" { backend "s3" {
bucket = "terraform" bucket = "terraform"
key = "music/terraform.tfstate" key = "music/terraform.tfstate"
access_key = "GK242d456c0692a9d4cc102206" access_key = "GK242d456c0692a9d4cc102206"
secret_key = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09" secret_key = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09"
region = "garage" region = "garage"
endpoints = { endpoints = {
s3 = "http://192.168.10.109:3900" s3 = "http://192.168.10.109:3900"
} }
skip_credentials_validation = true skip_credentials_validation = true
skip_requesting_account_id = true skip_requesting_account_id = true
skip_metadata_api_check = true skip_metadata_api_check = true
skip_region_validation = true skip_region_validation = true
use_path_style = true use_path_style = true
use_lockfile = true use_lockfile = true
} }
} }

View File

@@ -1,23 +1,3 @@
locals {
instance_map = var.instance_mode == "single" ? {
main = var.instance
} : var.instances
vm_created = {
for k, v in local.instance_map :
k => {
service_name = v.service_name
vm_name = v.vm_name
node_name = v.node_name
ipv4_address = module.vm-music[k].vm_ipv4_address
vm_tags = concat(
try(v.vm_tags, []),
["terraform", "docker", v.service_name, "ip-${replace(module.vm-music[k].vm_ipv4_address, ".", "-")}"]
)
}
}
}
module "vm-music" { module "vm-music" {
for_each = local.instance_map for_each = local.instance_map
source = "./modules/proxmox_ubuntu_cloudinit_clone" source = "./modules/proxmox_ubuntu_cloudinit_clone"
@@ -31,7 +11,7 @@ module "vm-music" {
vm_size = coalesce(try(each.value.vm_size, null), var.vm_defaults.vm_size) vm_size = coalesce(try(each.value.vm_size, null), var.vm_defaults.vm_size)
vm_bios = var.vm_defaults.vm_bios vm_bios = var.vm_defaults.vm_bios
vm_machine = var.vm_defaults.vm_machine vm_machine = var.vm_defaults.vm_machine
vm_tags = concat( vm_tags = concat(
try(each.value.vm_tags, []), try(each.value.vm_tags, []),
["terraform", "docker", each.value.service_name] ["terraform", "docker", each.value.service_name]
) )
@@ -39,8 +19,8 @@ module "vm-music" {
} }
module "inventory" { module "inventory" {
source = "./modules/proxmox_ansible_inventory" source = "./modules/proxmox_ansible_inventory"
filename = "${abspath("${path.root}/..")}/ansible/inventory/inventory.yml" filename = "${abspath("${path.root}/..")}/ansible/inventory/inventory.yml"
instances = local.vm_created instances = local.vm_created
} }

View File

@@ -1,7 +1,7 @@
resource "proxmox_virtual_environment_file" "cloud_config" { resource "proxmox_virtual_environment_file" "cloud_config" {
content_type = "snippets" content_type = "snippets"
datastore_id = "hlst" datastore_id = "hlst"
node_name = "${var.node_name}" node_name = var.node_name
source_raw { source_raw {
file_name = "${var.vm_name}_vm.cloud-config.yaml" # The name of the snippet file file_name = "${var.vm_name}_vm.cloud-config.yaml" # The name of the snippet file
@@ -52,37 +52,37 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
} }
resource "proxmox_virtual_environment_vm" "ubuntu_22_minimal_clone" { resource "proxmox_virtual_environment_vm" "ubuntu_22_minimal_clone" {
name = var.vm_name # VM name name = var.vm_name # VM name
node_name = var.node_name # Proxmox node to deploy the VM node_name = var.node_name # Proxmox node to deploy the VM
tags = var.vm_tags # Optional VM tags for categorization tags = var.vm_tags # Optional VM tags for categorization
agent { agent {
enabled = true # Enable the QEMU guest agent enabled = true # Enable the QEMU guest agent
} }
stop_on_destroy = true # Ensure VM is stopped gracefully when destroyed stop_on_destroy = true # Ensure VM is stopped gracefully when destroyed
clone { clone {
vm_id = local.selected_instance.vmid # ID of the source template vm_id = local.selected_instance.vmid # ID of the source template
node_name = local.selected_instance.crispy_name # Node of the source template node_name = local.selected_instance.crispy_name # Node of the source template
} }
bios = var.vm_bios # BIOS type (e.g., seabios or ovmf) bios = var.vm_bios # BIOS type (e.g., seabios or ovmf)
machine = var.vm_machine # Machine type (e.g., q35) machine = var.vm_machine # Machine type (e.g., q35)
cpu { cpu {
cores = var.vm_cpu # Number of CPU cores cores = var.vm_cpu # Number of CPU cores
type = "host" # Use host CPU type for best compatibility/performance type = "host" # Use host CPU type for best compatibility/performance
} }
memory { memory {
dedicated = var.vm_ram # RAM in MB dedicated = var.vm_ram # RAM in MB
} }
disk { disk {
datastore_id = var.node_datastore # Datastore to hold the disk datastore_id = var.node_datastore # Datastore to hold the disk
interface = "scsi0" # Primary disk interface interface = "scsi0" # Primary disk interface
size = var.vm_size size = var.vm_size
} }
@@ -90,20 +90,20 @@ resource "proxmox_virtual_environment_vm" "ubuntu_22_minimal_clone" {
initialization { initialization {
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id # Link the cloud-init file user_data_file_id = proxmox_virtual_environment_file.cloud_config.id # Link the cloud-init file
datastore_id = var.node_datastore datastore_id = var.node_datastore
interface = "scsi1" # Separate interface for cloud-init interface = "scsi1" # Separate interface for cloud-init
ip_config { ip_config {
ipv4 { ipv4 {
address = "dhcp" # Get IP via DHCP address = "dhcp" # Get IP via DHCP
} }
} }
} }
network_device { network_device {
bridge = var.bridge # Use the default bridge bridge = var.bridge # Use the default bridge
} }
lifecycle { lifecycle {
ignore_changes = [ # Ignore initialization section after first depoloyment for idempotency ignore_changes = [ # Ignore initialization section after first depoloyment for idempotency
initialization initialization
] ]
} }

View File

@@ -1,13 +0,0 @@
terraform {
required_providers {
proxmox = {
source = "bpg/proxmox"
version = "0.106.0"
#url = https://registry.terraform.io/providers/bpg/proxmox/latest/docs/guides/clone-vm
}
aws = {
source = "hashicorp/aws"
version = "6.38.0"
}
}
}

View File

@@ -1,8 +1,8 @@
variable "pm_api_url" { default = "https://192.168.10.201:8006/api2/json" } variable "pm_api_url" { default = "https://192.168.10.201:8006/api2/json" }
variable "pm_api_token" { default = "terraform@pve!provider=760580c4-5c1f-462b-986a-dd244c6c95f2" } variable "pm_api_token" { default = "terraform@pve!provider=760580c4-5c1f-462b-986a-dd244c6c95f2" }
variable "storage" { default = "hlst" } variable "storage" { default = "hlst" }
variable "bridge" { default = "vmbr0" } variable "bridge" { default = "vmbr0" }
variable "os_type" { variable "os_type" {
default = "alpine" default = "alpine"
@@ -11,7 +11,7 @@ variable "os_type" {
variable "instance_configs" { variable "instance_configs" {
type = list(object({ type = list(object({
crispy_name = string crispy_name = string
vmid = string vmid = string
})) }))
default = [ default = [
{ crispy_name = "snap", vmid = "902" }, { crispy_name = "snap", vmid = "902" },
@@ -25,11 +25,11 @@ variable "clone_count" {
default = 1 default = 1
} }
variable "vm_count" { default = 1 } variable "vm_count" { default = 1 }
variable "name_prefix" { default = "dev" } variable "name_prefix" { default = "dev" }
variable "vm_ram" { default = 2048 } variable "vm_ram" { default = 2048 }
variable "vm_cpu" { default = 1 } variable "vm_cpu" { default = 1 }
variable "vm_size" { default = 10 } variable "vm_size" { default = 10 }
variable "vm_bios" { variable "vm_bios" {
description = "Type of BIOS used for the VM" description = "Type of BIOS used for the VM"
type = string type = string
@@ -48,57 +48,57 @@ variable "vm_tags" {
default = ["test", "terraform"] default = ["test", "terraform"]
} }
variable "ipconfig0" { default = "ip=dhcp" } variable "ipconfig0" { default = "ip=dhcp" }
variable "access_key" { variable "access_key" {
type = string type = string
default = "GK242d456c0692a9d4cc102206" default = "GK242d456c0692a9d4cc102206"
} }
variable "secret_key" { variable "secret_key" {
type = string type = string
default = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09" default = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09"
} }
variable "region" { variable "region" {
type = string type = string
default = "garage" default = "garage"
} }
variable "endpoints_s3" { variable "endpoints_s3" {
type = string type = string
description = "S3 endpoint" description = "S3 endpoint"
default = "http://192.168.10.109:3909" default = "http://192.168.10.109:3909"
} }
variable "skip_credentials_validation" { variable "skip_credentials_validation" {
type = bool type = bool
default = true default = true
} }
variable "skip_requesting_account_id" { variable "skip_requesting_account_id" {
type = bool type = bool
default = true default = true
} }
variable "skip_metadata_api_check" { variable "skip_metadata_api_check" {
type = bool type = bool
default = true default = true
} }
variable "skip_region_validation" { variable "skip_region_validation" {
type = bool type = bool
default = true default = true
} }
variable "use_path_style" { variable "use_path_style" {
type = bool type = bool
default = true default = true
} }
variable "use_lockfile" { variable "use_lockfile" {
type = bool type = bool
default = true default = true
} }
variable "vm_name" { variable "vm_name" {

View File

@@ -1,126 +0,0 @@
locals {
selected_instance = one([
for cfg in var.instance_configs :
cfg if cfg.crispy_name == var.node_name
])
}
resource "proxmox_virtual_environment_file" "cloud_config" {
content_type = "snippets"
datastore_id = "local"
node_name = "pop"
source_raw {
file_name = "vm.cloud-config.yaml" # The name of the snippet file
data = <<-EOF
#cloud-config
hostname: ${var.vm_name}
package_update: true
package_upgrade: true
system_info:
default_user:
groups: [ docker ]
users:
- default
- name: cloud
groups:
- sudo
- docker
shell: /bin/bash
ssh-authorized-keys:
- "${var.vm_user_sshkey}" # Inject user's SSH key
sudo: ALL=(ALL) NOPASSWD:ALL
packages:
- qemu-guest-agent
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- unattended-upgrades
runcmd:
- systemctl enable qemu-guest-agent
- mkdir -p /etc/apt/keyrings
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
- apt-get update
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
- systemctl enable docker
- systemctl start docker
- reboot
EOF
}
}
resource "proxmox_virtual_environment_vm" "ubuntu_22_minimal_template" {
name = var.vm_name # VM name
node_name = var.node_name # Proxmox node to deploy the VM
tags = var.vm_tags # Optional VM tags for categorization
agent {
enabled = true # Enable the QEMU guest agent
}
stop_on_destroy = true # Ensure VM is stopped gracefully when destroyed
disk {
datastore_id = var.node_datastore # Datastore to hold the disk
interface = "scsi0" # Primary disk interface
size = var.vm_size
file_id = "local:iso/ubuntu-22.04-minimal-cloudimg-amd64.img"
}
efi_disk {
datastore_id = var.node_datastore
type = "4m"
}
bios = var.vm_bios # BIOS type (e.g., seabios or ovmf)
machine = var.vm_machine # Machine type (e.g., q35)
cpu {
cores = var.vm_cpu # Number of CPU cores
type = "host" # Use host CPU type for best compatibility/performance
}
memory {
dedicated = var.vm_ram # RAM in MB
}
initialization {
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id # Link the cloud-init file
datastore_id = var.node_datastore
interface = "scsi1" # Separate interface for cloud-init
ip_config {
ipv4 {
address = "dhcp" # Get IP via DHCP
}
}
}
network_device {
bridge = var.bridge # Use the default bridge
}
operating_system {
type = "l26" # Linux 2.6+ kernel
}
lifecycle {
ignore_changes = [ # Ignore initialization section after first depoloyment for idempotency
initialization
]
}
}

View File

@@ -1,3 +0,0 @@
output "vm_ipv4_address" {
value = proxmox_virtual_environment_vm.ubuntu_22_minimal_template.ipv4_addresses[1][0]
}

View File

@@ -1,13 +0,0 @@
terraform {
required_providers {
proxmox = {
source = "bpg/proxmox"
version = "0.106.0"
#url = https://registry.terraform.io/providers/bpg/proxmox/latest/docs/guides/clone-vm
}
aws = {
source = "hashicorp/aws"
version = "6.38.0"
}
}
}

View File

@@ -1,137 +0,0 @@
variable "pm_api_url" { default = "https://192.168.10.201:8006/api2/json" }
variable "pm_api_token" { default = "terraform@pve!provider=760580c4-5c1f-462b-986a-dd244c6c95f2" }
variable "storage" { default = "hlst" }
variable "bridge" { default = "vmbr0" }
variable "instance_configs" {
type = list(object({
crispy_name = string
vmid = string
}))
default = [
{ crispy_name = "snap", vmid = "9002" },
{ crispy_name = "crackle", vmid = "9000" },
{ crispy_name = "pop", vmid = "9001" }
]
}
variable "clone_count" {
type = number
default = 1
}
variable "vm_count" { default = 1 }
variable "name_prefix" { default = "dev" }
variable "vm_ram" { default = 2048 }
variable "vm_cpu" { default = 1 }
variable "vm_size" { default = 10 }
variable "vm_bios" {
description = "Type of BIOS used for the VM"
type = string
default = "ovmf"
}
variable "vm_machine" {
description = "Type of machine used for the VM"
type = string
default = "q35"
}
variable "vm_tags" {
description = "Tags for the VM"
type = list(any)
default = ["test", "terraform"]
}
variable "ipconfig0" { default = "ip=dhcp" }
variable "access_key" {
type = string
default = "GK242d456c0692a9d4cc102206"
}
variable "secret_key" {
type = string
default = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09"
}
variable "region" {
type = string
default = "garage"
}
variable "endpoints_s3" {
type = string
description = "S3 endpoint"
default = "http://192.168.10.109:3909"
}
variable "skip_credentials_validation" {
type = bool
default = true
}
variable "skip_requesting_account_id" {
type = bool
default = true
}
variable "skip_metadata_api_check" {
type = bool
default = true
}
variable "skip_region_validation" {
type = bool
default = true
}
variable "use_path_style" {
type = bool
default = true
}
variable "use_lockfile" {
type = bool
default = true
}
variable "vm_name" {
description = "Hostname of the VM"
type = string
default = "Lab"
}
variable "vm_user_sshkey" {
description = "Admin user SSH key of the VM"
type = string
default = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRlWaLBt/qmWY01Cd6jN/YxLnlT+6lg+evEdN/dIajirdTj1rCbAdlG3WYvo+4BpN17HK3/eGQpGUMbgI/8MVd8YPODcD34gaNX0w2v66BwHx+S6BZUpz5T2IoQT0JtSv/TtFICoff5gXdNRpfd4eWsmTioEqLA6oToJLE4dn3jvAzFi9y7fyLqvuoQMmPidYYJjGT30eiULtXNspoEP+GmuWmVEu+znzMWaKDWKdOsii4Cv1aWCRKSDDRzDBrZI2mP+Vm4HDQBdgDYRw4ehumMDtfaSjyJCnrk691bIM+wxzICuIEecg5kq5HcUPvo2mFyWPAEXb5xlXnuopYEBd7 Generated By NeoServer"
}
variable "node_name" {
description = "Proxmox host for the VM"
type = string
default = "pop"
}
variable "node_datastore" {
description = "Datastore used for VM storage"
type = string
default = "hlst"
}
variable "vm_template" {
description = "Template of the VM"
type = string
default = "ubuntu-cloud"
}
variable "vm_user" {
description = "Admin user of the VM"
type = string
default = "cloud"
}

View File

@@ -3,7 +3,7 @@ terraform {
proxmox = { proxmox = {
source = "bpg/proxmox" source = "bpg/proxmox"
version = "0.106.0" version = "0.106.0"
#url = https://registry.terraform.io/providers/bpg/proxmox/latest/docs/guides/clone-vm #url = https://registry.terraform.io/providers/bpg/proxmox/latest/docs/guides/clone-vm
} }
aws = { aws = {
source = "hashicorp/aws" source = "hashicorp/aws"
@@ -15,7 +15,7 @@ terraform {
provider "proxmox" { provider "proxmox" {
endpoint = var.pm_api_url endpoint = var.pm_api_url
api_token = var.pm_api_token api_token = var.pm_api_token
insecure = true insecure = true
# === FIX THIS === # === FIX THIS ===
ssh { ssh {
@@ -29,5 +29,5 @@ provider "aws" {
region = "garage" region = "garage"
access_key = "GK242d456c0692a9d4cc102206" access_key = "GK242d456c0692a9d4cc102206"
secret_key = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09" secret_key = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09"
# shared_credentials_files = ["$HOME/.aws/credentials"] # shared_credentials_files = ["$HOME/.aws/credentials"]
} }

View File

@@ -1,8 +1,8 @@
instance_mode = "single" instance_mode = "single"
instance = { instance = {
service_name = "droppedneedle" service_name = "music"
vm_name = "droppedneedle-01" vm_name = "music-01"
node_name = "snap" node_name = "snap"
app_port = 8688 app_port = 8688
app_image = "ghcr.io/habirabbu/droppedneedle:latest" app_image = "ghcr.io/habirabbu/droppedneedle:latest"

View File

@@ -18,8 +18,8 @@ variable "instance" {
vm_size = optional(string) vm_size = optional(string)
vm_tags = optional(list(string)) vm_tags = optional(list(string))
}) })
default = null default = null
nullable = true nullable = true
description = "Used only when instance_mode = single." description = "Used only when instance_mode = single."
} }