Terraform 如何根据if else条件从本地筛选数据?

Terraform 如何根据if else条件从本地筛选数据?,terraform,terraform-provider-azure,azure-rm,terraform-template-file,hashicorp,Terraform,Terraform Provider Azure,Azure Rm,Terraform Template File,Hashicorp,Main.tf locals { location_mapping = [ { "url": "L1.tfe.com" "location": "L1" "resource_group_name": "R1" "name_log_workspace_name": "W1

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"
}
我想根据本地数据块中的
位置和url
条件动态填充名称资源组e

范例


如果我通过
location value L2
url value L2.tfe.com
,那么我将得到
name=W2
resource\u group\u 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
}

你好@Marcin我需要匹配两个条件
位置
url
@NaveenKumar如果,您只需将
添加到
中即可。我更新了答案。如果条件不匹配,那么我需要一些默认值来获取我已尝试使用else,但获取错误。@NaveenKumar我再次更新了答案。但下一次,请在提问时提供所有这些细节。@NaveenKumar问得怎么样?