I'm constructing some objects to store the required information in Terraform I just defined a variable and its value as below
vnetsettings = {
HUBVNET = {
VNET_Name = "co-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "CoreInfra"
VNET_Address_Space = ["10.1.0.0/16","10.2.0.0/16"]
VNET_Tags = {
env = "prod"
application = "hub"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
MGMTVNET = {
VNET_Name = "mgmt-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "MGMT"
VNET_Address_Space = ["10.3.0.0/16","10.4.0.0/16"]
VNET_Tags = {
env = "prod"
application = "MGMT"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
}
my question is how can i bulk replace some of the attributes in the object, like VNET_Resource_Group
below is the result i want, everything same as the one above, except for the VNET_Resource_Group
vnetsettings = {
HUBVNET = {
VNET_Name = "co-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "replacedvalue"
VNET_Address_Space = ["10.1.0.0/16","10.2.0.0/16"]
VNET_Tags = {
env = "prod"
application = "hub"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
MGMTVNET = {
VNET_Name = "mgmt-vnet-01"
VNET_Location = "eastasia"
VNET_Resource_Group = "replacedvalue"
VNET_Address_Space = ["10.3.0.0/16","10.4.0.0/16"]
VNET_Tags = {
env = "prod"
application = "MGMT"
}
VNET_DNS_Servers = ["10.1.0.4","10.2.0.4"]
}
}
CodePudding user response:
What you can do is to create a local
variable which is essentially a copy of the original object. Also, while making the copy you can replace an attribute from the original objects using the merge
function.
locals {
vnetsettings_updated = {
for key, value in var.vnetsettings : key => merge(value, { VNET_Resource_Group = "replacedvalue" })
}
}
# Example usage of the updated object
output "vnetsettings" {
description = "VNET settings with updated VNET_Resource_Group"
value = local.vnetsettings_updated
}