mirror of
https://gitea.fenix-dev.com/fenix-gitea-admin/iac-opentofu-private.git
synced 2025-10-27 15:53:06 +00:00
Merge pull request 'fenix-admin' (#174) from fenix-admin into main
Reviewed-on: fenix-gitea-admin/iac-teste#174
This commit is contained in:
35
merge_yaml.py
Normal file
35
merge_yaml.py
Normal file
@ -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()
|
||||
56
proxmox.tf
56
proxmox.tf
@ -35,6 +35,40 @@ resource "proxmox_virtual_environment_file" "cloud_init_yaml" {
|
||||
}
|
||||
|
||||
|
||||
locals {
|
||||
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 = "${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" {
|
||||
@ -43,30 +77,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[each.key].result["merged_yaml"]
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user