Create new map from an existing list

Create new map from an existing list

hi, please , I migrate some terraform variables from terraform file [1.] to vault [2.], and for this reason,
right now I have variables reached from vault in different format [2.].

1.)

variable “vm_configuration” {
description = “Default vm configuration”
type = map
default = {
“192.168.1.10” = {
hostname = “vm01”
ip = “192.168.1.10”
vcpu = “2”
vram = “4294967296”
},
“192.168.1.11” = {
hostname = “vm02”
ip = “192.168.1.11”
vcpu = “2”
vram = “4294967296”
}
}
}

2.) vault secrets :

vm1_hostname = “vm01”
vm1_ip = “192.168.1.10”
vm1_vcpu = “2”
vm1_vram = “4294967296”
vm2_hostname = “vm02”
vm2_ip = “192.168.1.11”
vm2_vcpu = “2”
vm2_vram = “4294967296”

I try to convert reached variable from vault [2.], by using vault_generic_secret [3.], to the required format [1.], by extracting keys and values to separate lists [4.],
and then I try to use zipmap, but this way did not work.
Or I missed something.

3.)

data “vault_generic_secret” “vm_configs” {
path = “some/path/vm”
}

4.)

vms_keys = }
for key, value in “${data.vault_generic_secret.vm_configs.data}” : substr(key,0,3) => key…
}

vms_values = {
for key, value in “${data.vault_generic_secret.vm_configs.data}” : substr(key,0,3) => value…
}

Another problem is, that right now i have string “vm1” and “vm2” as key and not ip address “192.168.1.10” and “192.168.1.11”.

Thank you

please any suggestion/kick

Update #1
with this little update [1.] I can have a little bit different output [2.]

1.)

vms = {
for key, value in var.vm_configuration : trimprefix(key,substr(key,0,4)) => value…
}

2.)

output = [
“hostname” [ “vm1”, “vm2” ],
“ip” [ “192.168.0.1”, “192.168.0.2” ],
“vcpu” [ “2”, “2” ],
“vram” [ “4294967296”, “4294967296” ]
]

please any suggestion ?

Update #2 (final)

vms_keys = {
for key, value in var.vm_configuration : trimprefix(key,substr(key,0,4)) => value…
}

ips = { for k, v in local.vms_keys.ip : k => v }

vms = { for k, v in local.ips : v =>
{ for key, value in local.vms_keys : key => value[k] }
}

Now “for_each = local.vms”, do what I expect.
Maybe this will be helpful for someone.