azure terraform如果local.environment不等于"123",请执行此操作,否则什么也不执行

l7mqbcuq  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(89)

我试图获得一些IP限制我所有的Azure webapps除了1,我使用terraform编码环境,但不知道如何编码下面的语句“如果本地环境不等于”123”做这个,否则什么也不做”
有什么建议吗?我一直在寻找一段时间,现在找不到一个例子,像我试图做的事情。
先谢谢你了
我尝试过的东西都没用
我在这个区块中尝试的所有以下行:

resource "azurerm_windows_web_app" "myApp" {

#stuff here

site_config {

#stuff here

ip_restriction { 
        
        name       = "Testname"
        ip_address = "8.8.8.8/32"
        action     = "Allow"
        priority   = "796"
      }
}
}

count = local.environment == "123" ? 1 : 0

for_each = local.environment ? [] : ["123"]

for_each = local.environment ? [] : ["1"]

value = [for x in local.environment: x.id if x.id == "123"]
wj8zmpe1

wj8zmpe11#

terraform如果local.environment不等于“123”,则执行此操作,否则不执行任何操作:

  • 为了实现这一点,我修改了你的代码如下,它为我工作的预期。*
provider "azurerm" {
  features {}
}
variable "environment"{}
resource "azurerm_resource_group" "example" {
  name     = "resourcegroup"
  location = "xxxxx"
}

resource "azurerm_service_plan" "example" {
  name                = "example"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  sku_name            = "P1v2"
  os_type             = "Windows"
}

resource "azurerm_windows_web_app" "myApp" {
  count              = var.environment != "123" ? 1 : 0
  name                = "example"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_service_plan.example.location
  service_plan_id     = azurerm_service_plan.example.id

  site_config {
    
      ip_restriction { 
      name       = "Testname"
      ip_address = "8.8.8.8/32"
      action     = "Allow"
      priority   = "796"
    }

}
}
  • 已成功验证配置:*

terraform plan

有关Web应用程序部署,请参考terraform registry模板。

相关问题