1

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"
}

ローカルからのデータ ブロックの条件に基づいて、nameresource_group_nameを動的に入力したいと考えています。location and url

私が合格location value L2したらurl value L2.tfe.com、私は取得name=W2しますresource_group_name=R2

4

1 に答える 1

2

一致が1つしかないと仮定すると、次のようなものが適していると思います。

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
}
于 2021-03-04T07:33:51.607 に答える