Compare commits

..

8 Commits

Author SHA1 Message Date
CC
992ccea2b7 changing back ram and disk defaults 2026-09-15 15:55:36 +01:00
CC
6565fabbde rc 3 2026-09-12 17:46:41 +01:00
CC
1c13e8be4f rc 2 2026-09-12 17:44:50 +01:00
CC
6e67a1f7b3 rc 2 2026-09-12 17:35:38 +01:00
CC
496870455b rc 2 2026-09-12 17:34:45 +01:00
CC
cb6afe0932 rc 1 2026-09-12 17:18:23 +01:00
CC
ab79b9c0fa working main.tf 2026-09-12 17:08:24 +01:00
CC
17bc648be3 comment out ansible inventory 2026-09-12 15:42:19 +01:00
11 changed files with 96 additions and 116 deletions

View File

@@ -16,7 +16,7 @@ on:
jobs: jobs:
terraform-ansible-deploy: terraform-ansible-deploy:
runs-on: frente runs-on: ubuntu-latest
env: env:
PROXMOX_VE_API_TOKEN: https://api.staging.example.com PROXMOX_VE_API_TOKEN: https://api.staging.example.com
PROXMOX_VE_ENDPOINT: true PROXMOX_VE_ENDPOINT: true

View File

@@ -1,4 +1,4 @@
# VM Service Template # VM Service Template - CHANGE BACKEND FILE PATH
This repository is a starter template for deploying one or more service VMs in Proxmox using Terraform, then configuring and launching applications with Ansible and Docker Compose. This repository is a starter template for deploying one or more service VMs in Proxmox using Terraform, then configuring and launching applications with Ansible and Docker Compose.

19
terraform/locals.tf Normal file
View File

@@ -0,0 +1,19 @@
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.proxmox_ubuntu_cloudinit_clone[k].vm_ipv4_address
vm_tags = concat(
try(v.vm_tags, []),
["terraform", "docker", v.service_name, "ip-${replace(module.proxmox_ubuntu_cloudinit_clone[k].vm_ipv4_address, ".", "-")}"]
)
}
}
}

View File

@@ -1,28 +1,9 @@
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.proxmox_ubuntu_cloudinit_clone[k].vm_ipv4_address
vm_tags = concat(
try(v.vm_tags, []),
["terraform", "docker", v.service_name, "ip-${replace(module.proxmox_ubuntu_cloudinit_clone[k].vm_ipv4_address, ".", "-")}"]
)
}
}
}
module "proxmox_ubuntu_cloudinit_clone" { module "proxmox_ubuntu_cloudinit_clone" {
for_each = local.instance_map for_each = local.instance_map
source = "./modules/proxmox_ubuntu_cloudinit_clone" source = "./modules/proxmox_ubuntu_cloudinit_clone"
vm_name = each.value.vm_name vm_name = each.value.vm_name
service_name = each.value.service_name
node_name = each.value.node_name node_name = each.value.node_name
node_datastore = var.vm_defaults.node_datastore node_datastore = var.vm_defaults.node_datastore
bridge = var.vm_defaults.bridge bridge = var.vm_defaults.bridge
@@ -36,16 +17,6 @@ module "proxmox_ubuntu_cloudinit_clone" {
["terraform", "docker", each.value.service_name] ["terraform", "docker", each.value.service_name]
) )
vm_user_sshkey = var.vm_defaults.vm_user_sshkey vm_user_sshkey = var.vm_defaults.vm_user_sshkey
} compose_file_content = filebase64("${path.root}/../compose/docker-compose.yml")
env_file_content = filebase64("${path.root}/../compose/.env")
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
} }

View File

@@ -1,26 +0,0 @@
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)
}

View File

@@ -1,7 +0,0 @@
output "filename" {
value = local_file.inventory.filename
}
output "content" {
value = local_file.inventory.content
}

View File

@@ -1,13 +0,0 @@
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
}))
}

View File

@@ -4,7 +4,7 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
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"
data = <<-EOF data = <<-EOF
#cloud-config #cloud-config
hostname: ${var.vm_name} hostname: ${var.vm_name}
@@ -14,7 +14,7 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
system_info: system_info:
default_user: default_user:
groups: [ docker ] groups: [ cloud ]
users: users:
- default - default
@@ -24,7 +24,7 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
- docker - docker
shell: /bin/bash shell: /bin/bash
ssh-authorized-keys: ssh-authorized-keys:
- "${var.vm_user_sshkey}" # +++ Needs to be put into a variable injected by git actions secrets - "${var.vm_user_sshkey}"
sudo: ALL=(ALL) NOPASSWD:ALL sudo: ALL=(ALL) NOPASSWD:ALL
packages: packages:
@@ -36,16 +36,38 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
- lsb-release - lsb-release
- unattended-upgrades - unattended-upgrades
- rsync - rsync
- nano
bootcmd:
- mkdir -p /home/cloud/${var.service_name}
- chown cloud:cloud /home/cloud/${var.service_name}
write_files:
- path: /home/cloud/${var.service_name}/docker-compose.yml
owner: cloud:cloud
permissions: '0644'
encoding: b64
content: ${var.compose_file_content}
- path: /home/cloud/${var.service_name}/.env
owner: cloud:cloud
permissions: '0600'
encoding: b64
content: ${var.env_file_content}
runcmd: runcmd:
- systemctl enable qemu-guest-agent - systemctl enable qemu-guest-agent
- mkdir -p /etc/apt/keyrings - mkdir -p /etc/apt/keyrings
- apt-add-repository ppa:fish-shell/release-3
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg - 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 - 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 update
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin - apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin fish
- systemctl enable docker - systemctl enable docker
- systemctl start docker - systemctl start docker
- chown -R cloud:cloud /home/cloud/${var.service_name}
- cd /home/cloud/${var.service_name}
- sudo -u cloud docker compose up -d
- reboot - reboot
EOF EOF
} }
@@ -102,16 +124,9 @@ resource "proxmox_virtual_environment_vm" "ubuntu_22_minimal_clone" {
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

@@ -4,6 +4,9 @@ variable "pm_api_token" { default = "terraform@pve!provider=760580c4-5c1f-462b-9
variable "storage" { default = "hlst" } variable "storage" { default = "hlst" }
variable "bridge" { default = "vmbr0" } variable "bridge" { default = "vmbr0" }
variable "os_type" {
default = "alpine"
}
variable "instance_configs" { variable "instance_configs" {
type = list(object({ type = list(object({
@@ -104,9 +107,16 @@ variable "vm_name" {
default = "Lab" default = "Lab"
} }
variable "service_name" {
description = "Folder name of the services"
type = string
default = "Lab"
}
variable "vm_user_sshkey" { variable "vm_user_sshkey" {
description = "Admin user SSH key of the VM" description = "Admin user SSH key of the VM"
type = string type = string
default = ""
} }
@@ -134,3 +144,14 @@ variable "vm_user" {
type = string type = string
default = "cloud" default = "cloud"
} }
variable "compose_file_content" {
description = "Rendered contents of docker-compose.yml to write to the VM"
type = string
}
variable "env_file_content" {
description = "Rendered contents of .env to write to the VM"
type = string
sensitive = true
}

View File

@@ -1,7 +1,7 @@
output "vm_ipv4_addresses" { output "vm_ipv4_addresses" {
description = "Map of instance key to VM IPv4 address" description = "Map of instance key to VM IPv4 address"
value = { value = {
for k, m in module.vm-n8n : k => m.vm_ipv4_address for k, m in module.proxmox_ubuntu_cloudinit_clone : k => m.vm_ipv4_address
} }
} }
@@ -30,12 +30,12 @@ output "vm_tag_data" {
} }
} }
output "inventory_file" { # output "inventory_file" {
description = "Path to the generated Ansible inventory" # description = "Path to the generated Ansible inventory"
value = module.inventory.filename # value = module.inventory.filename
} # }
output "vm_data_file" { # output "vm_data_file" {
description = "Path to the generated VM data file" # description = "Path to the generated VM data file"
value = module.vm_data.filename # value = module.vm_data.filename
} # }

View File

@@ -54,11 +54,11 @@ variable "vm_defaults" {
node_datastore = "hlst" node_datastore = "hlst"
vm_bios = "ovmf" vm_bios = "ovmf"
vm_machine = "q35" vm_machine = "q35"
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" bridge = "vmbr0"
vm_cpu = 2 vm_cpu = 2
vm_ram = 4096 vm_ram = 2048
vm_size = 20 vm_size = 12
vm_user_sshkey = ""
} }
} }