如何根据 if else 条件 terraform 过滤来自本地人的数据?
How to filter data from locals based on if else condition terraform?
Main.tf
locals {
location_mapping = [
{
"url": "L1.tfe.com"
"location": "L1"
"resource_group_name": "R1"
"name_log_workspace_name": "W1"
},
{
"url": "L2.tfe.com"
"location": "L2"
"resource_group_name": "R2"
"name_log_workspace_name": "W2"
},
{
"url": "L3.tfe.com"
"location": "L3"
"resource_group_name": "R3"
"name_log_workspace_name": "W3"
}
]
}
data "azurerm_log_analytics_workspace" "example" {
# Populate name and resource group based on var.location(L2) condition if location matches in locals
name = "W2"
resource_group_name = "R2"
}
我想根据数据块中的location and url
条件动态填充name和resource_group_name来自当地人。
例子
如果我通过 location value L2
和 url value L2.tfe.com
那么我将得到 name=W2
和 resource_group_name=R2
我认为应该符合以下条件,假设只能匹配一个:
variable "location" {
default = "L2"
}
variable "url" {
default = "L2.tfe.com"
}
locals {
location_mapping = [
{
"url": "L1.tfe.com"
"location": "L1"
"resource_group_name": "R1"
"name_log_workspace_name": "W1"
},
{
"url": "L2.tfe.com"
"location": "L2"
"resource_group_name": "R2"
"name_log_workspace_name": "W2"
},
{name_log_workspace_name
"url": "L3.tfe.com"
"location": "L3"
"resource_group_name": "R3"
"name_log_workspace_name": "W3"
}
]
selected_mapping = lookup({for val in local.location_mapping:
0 => val if val.location == var.location &&
val.url == var.url}, 0,
{
"url": "default"
"location": "default_L"
"resource_group_name": "default_R"
"name_log_workspace_name": "default_W"
})
}
data "azurerm_log_analytics_workspace" "example" {
name = local.selected_mapping.name_log_workspace_name
resource_group_name = local.selected_mapping.resource_group_name
}
Main.tf
locals {
location_mapping = [
{
"url": "L1.tfe.com"
"location": "L1"
"resource_group_name": "R1"
"name_log_workspace_name": "W1"
},
{
"url": "L2.tfe.com"
"location": "L2"
"resource_group_name": "R2"
"name_log_workspace_name": "W2"
},
{
"url": "L3.tfe.com"
"location": "L3"
"resource_group_name": "R3"
"name_log_workspace_name": "W3"
}
]
}
data "azurerm_log_analytics_workspace" "example" {
# Populate name and resource group based on var.location(L2) condition if location matches in locals
name = "W2"
resource_group_name = "R2"
}
我想根据数据块中的location and url
条件动态填充name和resource_group_name来自当地人。
例子
如果我通过 location value L2
和 url value L2.tfe.com
那么我将得到 name=W2
和 resource_group_name=R2
我认为应该符合以下条件,假设只能匹配一个:
variable "location" {
default = "L2"
}
variable "url" {
default = "L2.tfe.com"
}
locals {
location_mapping = [
{
"url": "L1.tfe.com"
"location": "L1"
"resource_group_name": "R1"
"name_log_workspace_name": "W1"
},
{
"url": "L2.tfe.com"
"location": "L2"
"resource_group_name": "R2"
"name_log_workspace_name": "W2"
},
{name_log_workspace_name
"url": "L3.tfe.com"
"location": "L3"
"resource_group_name": "R3"
"name_log_workspace_name": "W3"
}
]
selected_mapping = lookup({for val in local.location_mapping:
0 => val if val.location == var.location &&
val.url == var.url}, 0,
{
"url": "default"
"location": "default_L"
"resource_group_name": "default_R"
"name_log_workspace_name": "default_W"
})
}
data "azurerm_log_analytics_workspace" "example" {
name = local.selected_mapping.name_log_workspace_name
resource_group_name = local.selected_mapping.resource_group_name
}