tested n8n template
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
terraform {
|
||||
backend "s3" {
|
||||
bucket = "terraform"
|
||||
key = "template/terraform.tfstate"
|
||||
key = "n8n/terraform.tfstate"
|
||||
access_key = "GK242d456c0692a9d4cc102206"
|
||||
secret_key = "1d7e22b7a8892cb11b569017659aa511b37b53287c4d1699c310d9f8ac76df09"
|
||||
region = "garage"
|
||||
|
||||
@@ -2,11 +2,25 @@ 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-n8n[k].vm_ipv4_address
|
||||
vm_tags = concat(
|
||||
try(v.vm_tags, []),
|
||||
["terraform", "docker", v.service_name, "ip-${replace(module.vm-n8n[k].vm_ipv4_address, ".", "-")}"]
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module "vm" {
|
||||
module "vm-n8n" {
|
||||
for_each = local.instance_map
|
||||
source = "git::https://tea.charcarservices.uk/CC/TerraformModules.git//proxmox_ubuntu_cloudinit_template?ref=main"
|
||||
source = "./modules/proxmox_ubuntu_cloudinit_clone"
|
||||
|
||||
vm_name = each.value.vm_name
|
||||
node_name = each.value.node_name
|
||||
@@ -23,3 +37,15 @@ module "vm" {
|
||||
)
|
||||
vm_user_sshkey = var.vm_defaults.vm_user_sshkey
|
||||
}
|
||||
|
||||
module "inventory" {
|
||||
source = "./modules/proxmox_ansible_inventory"
|
||||
filename = "${abspath("${path.root}/..")}/ansible/inventory/inventory.yml"
|
||||
instances = local.vm_created
|
||||
}
|
||||
|
||||
module "vm_data" {
|
||||
source = "./modules/proxmox_vm_data"
|
||||
filename = "${abspath("${path.root}/..")}/terraform/vm_data.yml"
|
||||
instances = local.vm_created
|
||||
}
|
||||
|
||||
26
terraform/modules/proxmox_ansible_inventory/main.tf
Normal file
26
terraform/modules/proxmox_ansible_inventory/main.tf
Normal file
@@ -0,0 +1,26 @@
|
||||
locals {
|
||||
inventory = {
|
||||
all = {
|
||||
vars = {
|
||||
ansible_user = "cloud"
|
||||
}
|
||||
children = {
|
||||
for svc in distinct([for k, v in var.instances : v.service_name]) :
|
||||
svc => {
|
||||
hosts = {
|
||||
for k, v in var.instances :
|
||||
v.vm_name => {
|
||||
ansible_host = v.ipv4_address
|
||||
}
|
||||
if v.service_name == svc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "local_file" "inventory" {
|
||||
filename = var.filename
|
||||
content = yamlencode(local.inventory)
|
||||
}
|
||||
7
terraform/modules/proxmox_ansible_inventory/outputs.tf
Normal file
7
terraform/modules/proxmox_ansible_inventory/outputs.tf
Normal file
@@ -0,0 +1,7 @@
|
||||
output "filename" {
|
||||
value = local_file.inventory.filename
|
||||
}
|
||||
|
||||
output "content" {
|
||||
value = local_file.inventory.content
|
||||
}
|
||||
13
terraform/modules/proxmox_ansible_inventory/variables.tf
Normal file
13
terraform/modules/proxmox_ansible_inventory/variables.tf
Normal file
@@ -0,0 +1,13 @@
|
||||
variable "filename" {
|
||||
description = "Path to write the inventory.yml file"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
description = "Normalized instance map keyed by instance key"
|
||||
type = map(object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
ipv4_address = string
|
||||
}))
|
||||
}
|
||||
123
terraform/modules/proxmox_ubuntu_cloudinit_clone/main.tf
Normal file
123
terraform/modules/proxmox_ubuntu_cloudinit_clone/main.tf
Normal file
@@ -0,0 +1,123 @@
|
||||
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_clone" {
|
||||
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
|
||||
|
||||
|
||||
clone {
|
||||
vm_id = local.selected_instance.vmid # ID 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)
|
||||
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
|
||||
}
|
||||
|
||||
disk {
|
||||
datastore_id = var.node_datastore # Datastore to hold the disk
|
||||
interface = "scsi0" # Primary disk interface
|
||||
size = var.vm_size
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
lifecycle {
|
||||
ignore_changes = [ # Ignore initialization section after first depoloyment for idempotency
|
||||
initialization
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
output "vm_ipv4_address" {
|
||||
value = proxmox_virtual_environment_vm.ubuntu_22_minimal_clone.ipv4_addresses[1][0]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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"
|
||||
}
|
||||
}
|
||||
}
|
||||
140
terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf
Normal file
140
terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf
Normal file
@@ -0,0 +1,140 @@
|
||||
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 "os_type" {
|
||||
default = "alpine"
|
||||
}
|
||||
|
||||
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 = "900" }
|
||||
]
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
126
terraform/modules/proxmox_ubuntu_cloudinit_template/main.tf
Normal file
126
terraform/modules/proxmox_ubuntu_cloudinit_template/main.tf
Normal file
@@ -0,0 +1,126 @@
|
||||
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
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
output "vm_ipv4_address" {
|
||||
value = proxmox_virtual_environment_vm.ubuntu_22_minimal_template.ipv4_addresses[1][0]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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"
|
||||
}
|
||||
}
|
||||
}
|
||||
137
terraform/modules/proxmox_ubuntu_cloudinit_template/variables.tf
Normal file
137
terraform/modules/proxmox_ubuntu_cloudinit_template/variables.tf
Normal file
@@ -0,0 +1,137 @@
|
||||
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"
|
||||
}
|
||||
17
terraform/modules/proxmox_vm_data/main.tf
Normal file
17
terraform/modules/proxmox_vm_data/main.tf
Normal file
@@ -0,0 +1,17 @@
|
||||
locals {
|
||||
vm_data = {
|
||||
vm_tag_data = {
|
||||
for k, v in var.instances :
|
||||
k => {
|
||||
node_name = v.node_name
|
||||
vm_name = v.vm_name
|
||||
tags = v.vm_tags
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "local_file" "vm_data" {
|
||||
filename = var.filename
|
||||
content = yamlencode(local.vm_data)
|
||||
}
|
||||
7
terraform/modules/proxmox_vm_data/outputs.tf
Normal file
7
terraform/modules/proxmox_vm_data/outputs.tf
Normal file
@@ -0,0 +1,7 @@
|
||||
output "filename" {
|
||||
value = local_file.vm_data.filename
|
||||
}
|
||||
|
||||
output "content" {
|
||||
value = local_file.vm_data.content
|
||||
}
|
||||
15
terraform/modules/proxmox_vm_data/variables.tf
Normal file
15
terraform/modules/proxmox_vm_data/variables.tf
Normal file
@@ -0,0 +1,15 @@
|
||||
variable "filename" {
|
||||
description = "Path to write the vm_data.yml file"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
description = "Normalized instance map keyed by instance key"
|
||||
type = map(object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
ipv4_address = string
|
||||
vm_tags = list(string)
|
||||
}))
|
||||
}
|
||||
@@ -1,20 +1,41 @@
|
||||
output "vm_ipv4_addresses" {
|
||||
description = "Map of instance key to VM IPv4 address"
|
||||
value = {
|
||||
for k, m in module.vm : k => m.vm_ipv4_address
|
||||
}
|
||||
}
|
||||
|
||||
output "vm_tags" {
|
||||
value = {
|
||||
for k, m in module.vm : k => concat(
|
||||
try(local.instance_map[k].vm_tags, []),
|
||||
["terraform", "docker", local.instance_map[k].service_name, "ip-${replace(m.vm_ipv4_address, ".", "-")}"]
|
||||
)
|
||||
for k, m in module.vm-n8n : k => m.vm_ipv4_address
|
||||
}
|
||||
}
|
||||
|
||||
output "service_names" {
|
||||
description = "Map of instance key to service name"
|
||||
value = {
|
||||
for k, cfg in local.instance_map : k => cfg.service_name
|
||||
for k, v in local.instance_map : k => v.service_name
|
||||
}
|
||||
}
|
||||
|
||||
output "vm_names" {
|
||||
description = "Map of instance key to VM name"
|
||||
value = {
|
||||
for k, v in local.instance_map : k => v.vm_name
|
||||
}
|
||||
}
|
||||
|
||||
output "vm_tag_data" {
|
||||
description = "Map of instance key to node, VM name, and final tags"
|
||||
value = {
|
||||
for k, v in local.vm_created : k => {
|
||||
node_name = v.node_name
|
||||
vm_name = v.vm_name
|
||||
tags = v.vm_tags
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output "inventory_file" {
|
||||
description = "Path to the generated Ansible inventory"
|
||||
value = module.inventory.filename
|
||||
}
|
||||
|
||||
output "vm_data_file" {
|
||||
description = "Path to the generated VM data file"
|
||||
value = module.vm_data.filename
|
||||
}
|
||||
|
||||
@@ -1,21 +1,11 @@
|
||||
instance_mode = "single"
|
||||
|
||||
instance = {
|
||||
service_name = "grafana"
|
||||
vm_name = "grafana-01"
|
||||
service_name = "n8n"
|
||||
vm_name = "n8n-01"
|
||||
node_name = "pop"
|
||||
app_port = 3000
|
||||
app_image = "grafana/grafana:latest"
|
||||
vm_tags = ["monitoring"]
|
||||
app_port = 5678
|
||||
app_image = "docker.n8n.io/n8nio/n8n"
|
||||
vm_tags = ["agentic"]
|
||||
}
|
||||
|
||||
vm_defaults = {
|
||||
node_datastore = "hlst"
|
||||
vm_bios = "ovmf"
|
||||
vm_machine = "q35"
|
||||
vm_user_sshkey = "ssh-ed25519 AAAA..."
|
||||
bridge = "vmbr0"
|
||||
vm_cpu = 2
|
||||
vm_ram = 4096
|
||||
vm_size = "20G"
|
||||
}
|
||||
|
||||
@@ -1,40 +1,44 @@
|
||||
variable "instance_mode" {
|
||||
type = string
|
||||
default = "single"
|
||||
type = string
|
||||
description = "single or multiple"
|
||||
|
||||
validation {
|
||||
condition = contains(["single", "multiple"], var.instance_mode)
|
||||
error_message = "instance_mode must be either single or multiple."
|
||||
}
|
||||
}
|
||||
|
||||
variable "instance" {
|
||||
description = "Single instance definition"
|
||||
type = object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
app_port = number
|
||||
app_image = string
|
||||
vm_tags = optional(list(string))
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
vm_tags = optional(list(string))
|
||||
})
|
||||
default = null
|
||||
default = null
|
||||
nullable = true
|
||||
description = "Used only when instance_mode = single."
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
description = "Multiple instance definitions"
|
||||
type = map(object({
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
app_port = number
|
||||
app_image = string
|
||||
vm_tags = optional(list(string))
|
||||
service_name = string
|
||||
vm_name = string
|
||||
node_name = string
|
||||
vm_cpu = optional(number)
|
||||
vm_ram = optional(number)
|
||||
vm_size = optional(string)
|
||||
vm_tags = optional(list(string))
|
||||
}))
|
||||
default = {}
|
||||
default = {}
|
||||
nullable = false
|
||||
description = "Used only when instance_mode = multiple."
|
||||
}
|
||||
|
||||
|
||||
variable "vm_defaults" {
|
||||
type = object({
|
||||
node_datastore = string
|
||||
@@ -44,16 +48,19 @@ variable "vm_defaults" {
|
||||
bridge = string
|
||||
vm_cpu = number
|
||||
vm_ram = number
|
||||
vm_size = string
|
||||
vm_size = number
|
||||
})
|
||||
default = {
|
||||
node_datastore = "hlst"
|
||||
vm_bios = "ovmf"
|
||||
vm_machine = "q35"
|
||||
vm_user_sshkey = ""
|
||||
vm_user_sshkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRlWaLBt/qmWY01Cd6jN/YxLnlT+6lg+evEdN/dIajirdTj1rCbAdlG3WYvo+4BpN17HK3/eGQpGUMbgI/8MVd8YPODcD34gaNX0w2v66BwHx+S6BZUpz5T2IoQT0JtSv/TtFICoff5gXdNRpfd4eWsmTioEqLA6oToJLE4dn3jvAzFi9y7fyLqvuoQMmPidYYJjGT30eiULtXNspoEP+GmuWmVEu+znzMWaKDWKdOsii4Cv1aWCRKSDDRzDBrZI2mP+Vm4HDQBdgDYRw4ehumMDtfaSjyJCnrk691bIM+wxzICuIEecg5kq5HcUPvo2mFyWPAEXb5xlXnuopYEBd7 Generated By NeoServer"
|
||||
bridge = "vmbr0"
|
||||
vm_cpu = 1
|
||||
vm_ram = 2048
|
||||
vm_size = "20G"
|
||||
vm_cpu = 2
|
||||
vm_ram = 4096
|
||||
vm_size = 20
|
||||
}
|
||||
}
|
||||
|
||||
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" }
|
||||
|
||||
Reference in New Issue
Block a user