diff --git a/proxmox.tf b/proxmox.tf index fcb542a..f07f863 100644 --- a/proxmox.tf +++ b/proxmox.tf @@ -51,6 +51,56 @@ resource "proxmox_virtual_environment_file" "cloud_init_yaml" { } + +# Gerar um snippet cloud-init por VM +resource "proxmox_virtual_environment_file" "vm_user_data" { + for_each = { for vm in var.k8s_vms : vm.name => vm } + + node_name = "fenix" + datastore_id = "local-snippets" + content_type = "snippets" + + source_raw { + file_name = "cloud-init-iac-k8s-${each.value.name}.yaml" + source_raw { + < 0 ~} + + %{ for u in each.value.extra_users ~} + - name: ${u.name} + groups: [${join(", ", u.groups)}] + shell: /bin/bash + sudo: ALL=(ALL) NOPASSWD:ALL + %{ endfor ~} + + chpasswd: + list: | + %{ for u in each.value.extra_users ~} + ${u.name}:${u.password} + %{ endfor ~} + expire: false + + %{ endif ~} + runcmd: + %{ if length(each.value.extra_runcmd) > 0 ~} + %{ for cmd in each.value.extra_runcmd ~} + - ${cmd} + %{ endfor ~} + %{ endif ~} + EOF + } + } +} + + resource "proxmox_virtual_environment_vm" "proxmox-kubernetes-VM-template" { depends_on = [proxmox_virtual_environment_download_file.latest_ubunto_cloud_img, proxmox_virtual_environment_file.cloud_init_yaml] @@ -141,39 +191,7 @@ resource "proxmox_virtual_environment_vm" "k8s_vms" { } } - user_data = < 0 ~} - - %{ for u in each.value.extra_users ~} - - name: ${u.name} - groups: [${join(", ", u.groups)}] - shell: /bin/bash - sudo: ALL=(ALL) NOPASSWD:ALL - %{ endfor ~} - - chpasswd: - list: | - %{ for u in each.value.extra_users ~} - ${u.name}:${u.password} - %{ endfor ~} - expire: false - - %{ endif ~} - runcmd: - %{ if length(each.value.extra_runcmd) > 0 ~} - %{ for cmd in each.value.extra_runcmd ~} - - ${cmd} - %{ endfor ~} - %{ endif ~} - EOF + user_data_file_id = proxmox_virtual_environment_file.vm_user_data[each.key].id }