new vars pass docker compose in cloud init

This commit is contained in:
CC
2026-09-12 15:33:40 +01:00
parent fbb2a53e23
commit 5fb9824444
6 changed files with 62 additions and 22 deletions

View File

@@ -9,10 +9,10 @@ locals {
service_name = v.service_name
vm_name = v.vm_name
node_name = v.node_name
ipv4_address = module.vm-music[k].vm_ipv4_address
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.vm-music[k].vm_ipv4_address, ".", "-")}"]
["terraform", "docker", v.service_name, "ip-${replace(module.proxmox_ubuntu_cloudinit_clone[k].vm_ipv4_address, ".", "-")}"]
)
}
}

View File

@@ -1,4 +1,4 @@
module "vm-music" {
module "proxmox_ubuntu_cloudinit_clone" {
for_each = local.instance_map
source = "./modules/proxmox_ubuntu_cloudinit_clone"
@@ -16,16 +16,18 @@ module "vm-music" {
["terraform", "docker", each.value.service_name]
)
vm_user_sshkey = var.vm_defaults.vm_user_sshkey
compose_file_content = file("${path.root}/../compose/docker-compose.yml")
env_file_content = file("${path.root}/../compose/.env")
}
module "inventory" {
source = "./modules/proxmox_ansible_inventory"
filename = "${abspath("${path.root}/..")}/ansible/inventory/inventory.yml"
instances = local.vm_created
}
# 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
}
# module "vm_data" {
# source = "./modules/proxmox_vm_data"
# filename = "${abspath("${path.root}/..")}/terraform/vm_data.yml"
# instances = local.vm_created
# }

View File

@@ -4,7 +4,7 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
node_name = var.node_name
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
#cloud-config
hostname: ${var.vm_name}
@@ -24,7 +24,7 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
- docker
shell: /bin/bash
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
packages:
@@ -37,6 +37,19 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
- unattended-upgrades
- rsync
write_files:
- path: /home/cloud/docker-compose.yml
owner: cloud:cloud
permissions: '0644'
content: |
${indent(10, var.compose_file_content)}
- path: /home/cloud/.env
owner: cloud:cloud
permissions: '0600'
content: |
${indent(10, var.env_file_content)}
runcmd:
- systemctl enable qemu-guest-agent
- mkdir -p /etc/apt/keyrings
@@ -46,7 +59,8 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
- systemctl enable docker
- systemctl start docker
- reboot
- chown -R cloud:cloud /home/cloud
- [ bash, -c, "cd /home/cloud && docker compose up -d" ]
EOF
}
}

View File

@@ -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"
}
}
}

View File

@@ -110,7 +110,7 @@ variable "vm_name" {
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"
default = ""
}
@@ -138,3 +138,14 @@ variable "vm_user" {
type = string
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" {
description = "Map of instance key to VM IPv4 address"
value = {
for k, m in module.vm-music : k => m.vm_ipv4_address
for k, m in module.proxmox_ubuntu_cloudinit_clone : k => m.vm_ipv4_address
}
}