From 95b9e4490b7159baac83bde92db48fa34e686d07 Mon Sep 17 00:00:00 2001 From: fenix-gitea-admin Date: Wed, 27 Aug 2025 10:11:31 +0000 Subject: [PATCH 1/5] Add merge_yaml.py --- merge_yaml.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 merge_yaml.py diff --git a/merge_yaml.py b/merge_yaml.py new file mode 100644 index 0000000..e9cff7e --- /dev/null +++ b/merge_yaml.py @@ -0,0 +1,35 @@ + +#!/usr/bin/env python3 +import yaml +import sys +import json +from collections.abc import Mapping + +def deep_merge_yaml(dict1, dict2): + result = dict1.copy() + for key, value in dict2.items(): + if key in result: + if isinstance(result[key], list) and isinstance(value, list): + result[key] = result[key] + value + elif isinstance(result[key], Mapping) and isinstance(value, Mapping): + result[key] = deep_merge_yaml(result[key], value) + else: + result[key] = value + else: + result[key] = value + return result + +def main(): + input_data = json.load(sys.stdin) + file1 = input_data["file1"] + file2 = input_data["file2"] + + with open(file1, "r") as f1, open(file2, "r") as f2: + yaml1 = yaml.safe_load(f1) + yaml2 = yaml.safe_load(f2) + + merged = deep_merge_yaml(yaml1, yaml2) + print(json.dumps({"merged_yaml": yaml.dump(merged, sort_keys=False)})) + +if __name__ == "__main__": + main() From 017865126d8bd65f89e8b5522a414d05bf6fcc90 Mon Sep 17 00:00:00 2001 From: fenix-gitea-admin Date: Wed, 27 Aug 2025 10:14:14 +0000 Subject: [PATCH 2/5] python --- proxmox.tf | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/proxmox.tf b/proxmox.tf index 1d3476b..a5ef41c 100644 --- a/proxmox.tf +++ b/proxmox.tf @@ -35,6 +35,15 @@ resource "proxmox_virtual_environment_file" "cloud_init_yaml" { } +data "external" "merged_yaml" { + program = ["python3", "${path.module}/merge_yaml.py"] + + query = { + file1 = "${path.module}/cloud-init-base.yaml" + file2 = "${path.module}/cloud-init-extra.yaml" + } +} + # Gerar um snippet cloud-init por VM resource "proxmox_virtual_environment_file" "vm_user_data" { @@ -43,30 +52,10 @@ resource "proxmox_virtual_environment_file" "vm_user_data" { node_name = "fenix" datastore_id = "local-snippets" content_type = "snippets" - source_raw { file_name = "cloud-init-iac-k8s-${each.key}.yaml" - data = yamlencode( - merge( - yamldecode(file("${path.module}/cloud-init-base.yaml")), - { - packages = concat( - try(yamldecode(file("${path.module}/cloud-init-base.yaml")).packages, []), - try(each.value.extra_packages, []) - ), - users = concat( - try(yamldecode(file("${path.module}/cloud-init-base.yaml")).users, []), - try(each.value.extra_users, []) - ), - runcmd = concat( - try(yamldecode(file("${path.module}/cloud-init-base.yaml")).runcmd, []), - try(each.value.extra_runcmd, []) - ) - } - ) - ) + data = data.external.merged_yaml.result["merged_yaml"] } - } From c49e9e6f5580708de38fd2d212521bbbf5849b90 Mon Sep 17 00:00:00 2001 From: fenix-gitea-admin Date: Wed, 27 Aug 2025 10:17:06 +0000 Subject: [PATCH 3/5] Update proxmox.tf --- proxmox.tf | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/proxmox.tf b/proxmox.tf index a5ef41c..4d906ad 100644 --- a/proxmox.tf +++ b/proxmox.tf @@ -35,12 +35,21 @@ resource "proxmox_virtual_environment_file" "cloud_init_yaml" { } +locals { + rendered_yaml = templatefile("${path.module}/cloud-init-extra.yaml.tftpl", { + # Aqui colocas as variáveis que o template precisa + hostname = "vm-k8s" + fqdn = "vm-k8s.local" + # Adiciona mais conforme necessário + }) +} + data "external" "merged_yaml" { program = ["python3", "${path.module}/merge_yaml.py"] query = { file1 = "${path.module}/cloud-init-base.yaml" - file2 = "${path.module}/cloud-init-extra.yaml" + file2 = local.rendered_yaml } } From 08f2896c8564b1ae14d4358ea07ec50f155ed6fc Mon Sep 17 00:00:00 2001 From: fenix-gitea-admin Date: Wed, 27 Aug 2025 10:17:40 +0000 Subject: [PATCH 4/5] Update proxmox.tf --- proxmox.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxmox.tf b/proxmox.tf index 4d906ad..a3ddd71 100644 --- a/proxmox.tf +++ b/proxmox.tf @@ -36,7 +36,7 @@ resource "proxmox_virtual_environment_file" "cloud_init_yaml" { locals { - rendered_yaml = templatefile("${path.module}/cloud-init-extra.yaml.tftpl", { + rendered_yaml = templatefile("${path.module}/cloud-init-vm.yaml.tftpl", { # Aqui colocas as variáveis que o template precisa hostname = "vm-k8s" fqdn = "vm-k8s.local" From 9fd88af475a8c3a51b1bf21667ca60d12fcb909d Mon Sep 17 00:00:00 2001 From: fenix-gitea-admin Date: Wed, 27 Aug 2025 10:25:15 +0000 Subject: [PATCH 5/5] Update proxmox.tf --- proxmox.tf | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/proxmox.tf b/proxmox.tf index a3ddd71..5c7a7c5 100644 --- a/proxmox.tf +++ b/proxmox.tf @@ -36,24 +36,40 @@ resource "proxmox_virtual_environment_file" "cloud_init_yaml" { locals { - rendered_yaml = templatefile("${path.module}/cloud-init-vm.yaml.tftpl", { - # Aqui colocas as variáveis que o template precisa - hostname = "vm-k8s" - fqdn = "vm-k8s.local" - # Adiciona mais conforme necessário - }) + rendered_yaml_per_vm ={ + for vm in var.proxmox_k8s_vms : vm.name => + templatefile("${path.module}/cloud-init-vm.yaml.tftpl", { + # Aqui colocas as variáveis que o template precisa + hostname = "vm-k8s" + fqdn = "vm-k8s.local" + # Adiciona mais conforme necessário + }) + } } + data "external" "merged_yaml" { + for_each = { for vm in var.proxmox_k8s_vms : vm.name => vm } + + depends_on = [local_file.rendered_yaml_file] + program = ["python3", "${path.module}/merge_yaml.py"] query = { file1 = "${path.module}/cloud-init-base.yaml" - file2 = local.rendered_yaml + file2 = "${path.module}/cloud-init-extra-${each.key}.yaml" } } +resource "local_file" "rendered_yaml_file" { + for_each = { for vm in var.proxmox_k8s_vms : vm.name => vm } + + content = local.rendered_yaml_per_vm[each.key] + filename = "${path.module}/cloud-init-extra-${each.key}.yaml" +} + + # Gerar um snippet cloud-init por VM resource "proxmox_virtual_environment_file" "vm_user_data" { for_each = { for vm in var.proxmox_k8s_vms : vm.name => vm } @@ -63,7 +79,7 @@ resource "proxmox_virtual_environment_file" "vm_user_data" { content_type = "snippets" source_raw { file_name = "cloud-init-iac-k8s-${each.key}.yaml" - data = data.external.merged_yaml.result["merged_yaml"] + data = data.external.merged_yaml[each.key].result["merged_yaml"] } }