Hi @parthibansg20,
I’m afraid I don’t understand your goal. I’d like to learn more about it to try to answer your question.
I think what you’ve shown here is a value for a variable declared like this:
variable "serverlist" {
type = list(object({
name = string
value = string
}))
}
I also think that you want to make a dynamic decision based on the value of the name
attribute of each element of this list.
However, I don’t yet understand what result you intend to produce. In particular, I don’t understand what “a1”, “a2”, “a3”, and “a4” represent in your example.
As a partial answer to your question, I can share a pattern for selecting a value based on a string. You can achieve that by declaring a local value that is a mapping to be used as a lookup table:
locals {
server_name_something = {
"PhysicalVM" = "result-1"
"CloudVM" = "result-2"
}
servers_with_something = tolist([
for s in var.serverlist : {
name = s.name
value = s.value
something = local.server_name_something[s.name]
}
])
}
The server_name_something
local value gives a different result for each possible name
value. servers_with_something
then builds a new list based on the given list which adds the additional attribute something
which has the result of consulting the lookup table.
With the input value you showed in your question, local.servers_with_something
would have the following value:
[
{"name": "PhysicalVM", "value": "Server1", "something": "result-1"},
{"name": "CloudVM", "value": "CServer1", "something": "result-2"},
{"name": "PhysicalVM", "value": "Server2", "something": "result-1"},
{"name": "PhysicalVM", "value": "Server3", "something": "result-1"},
]
Hopefully this example gives you some ideas for what to try next. If this isn’t a sufficient answer then please tell me more about your goal and I will try again.
Thanks!