diff --git a/terraform/locals.tf b/terraform/locals.tf new file mode 100644 index 0000000..3afdef9 --- /dev/null +++ b/terraform/locals.tf @@ -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, ".", "-")}"] + ) + } + } +} \ No newline at end of file diff --git a/terraform/main.tf b/terraform/main.tf index cbc682f..1d86fce 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -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" { for_each = local.instance_map source = "./modules/proxmox_ubuntu_cloudinit_clone" vm_name = each.value.vm_name + service_name = each.value.service_name node_name = each.value.node_name node_datastore = var.vm_defaults.node_datastore bridge = var.vm_defaults.bridge @@ -35,17 +16,7 @@ module "proxmox_ubuntu_cloudinit_clone" { try(each.value.vm_tags, []), ["terraform", "docker", each.value.service_name] ) - 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 -# } + 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") +} \ No newline at end of file diff --git a/terraform/modules/proxmox_ansible_inventory/main.tf b/terraform/modules/proxmox_ansible_inventory/main.tf deleted file mode 100644 index 6c262d8..0000000 --- a/terraform/modules/proxmox_ansible_inventory/main.tf +++ /dev/null @@ -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) -} diff --git a/terraform/modules/proxmox_ansible_inventory/outputs.tf b/terraform/modules/proxmox_ansible_inventory/outputs.tf deleted file mode 100644 index 0ab7d6c..0000000 --- a/terraform/modules/proxmox_ansible_inventory/outputs.tf +++ /dev/null @@ -1,7 +0,0 @@ -output "filename" { - value = local_file.inventory.filename -} - -output "content" { - value = local_file.inventory.content -} diff --git a/terraform/modules/proxmox_ansible_inventory/variables.tf b/terraform/modules/proxmox_ansible_inventory/variables.tf deleted file mode 100644 index ea3837f..0000000 --- a/terraform/modules/proxmox_ansible_inventory/variables.tf +++ /dev/null @@ -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 - })) -} diff --git a/terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf b/terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf index 8dc08f2..9dae83d 100644 --- a/terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf +++ b/terraform/modules/proxmox_ubuntu_cloudinit_clone/variables.tf @@ -4,6 +4,9 @@ variable "pm_api_token" { default = "terraform@pve!provider=760580c4-5c1f-462b-9 variable "storage" { default = "hlst" } variable "bridge" { default = "vmbr0" } +variable "os_type" { + default = "alpine" +} variable "instance_configs" { type = list(object({ @@ -104,9 +107,16 @@ variable "vm_name" { default = "Lab" } +variable "service_name" { + description = "Folder name of the services" + type = string + default = "Lab" +} + variable "vm_user_sshkey" { description = "Admin user SSH key of the VM" type = string + default = "" } @@ -133,4 +143,15 @@ variable "vm_user" { description = "Admin user of the VM" 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 } \ No newline at end of file diff --git a/terraform/variables.tf b/terraform/variables.tf index 6d07b7b..87b6b73 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -54,11 +54,11 @@ variable "vm_defaults" { node_datastore = "hlst" vm_bios = "ovmf" 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" vm_cpu = 2 - vm_ram = 4096 - vm_size = 20 - vm_user_sshkey = "" + vm_ram = 8192 + vm_size = 100 } }