Azure 模板我们如何根据资源组位置更改资源名称?

Azure template how do we change the resource name based on resource group location?

我正在开发一个通用模板,它在每个区域使用不同的 vnet。我们如何根据位置更改模板,设置 vnet 名称。

我们可以使用 if (resourcegroup.location) 吗?

我想我知道你发生了什么,你没有与有效值“EastUs”进行比较,它不是 resourceGroup().location,在本例中是“eastus”。您可以进行测试部署并查看输出以检查此类值。

我已经修改了您在评论中传递的模板以执行您想要的操作。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "storageAccountName": "[if(equals(resourceGroup().location,'eastus'),'eastusstorageaccount','noteastusstorageaccount')]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2018-07-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountName": {
      "type": "string",
      "value": "[variables('storageAccountName')]"
    },
    "location": {
      "type": "string",
      "value": "[resourceGroup().location]"
    }
  }
}

为了以动态方式做你想做的事,我会尝试对 vbnet 的名称进行编码。像这样:

  1. eastusvbnet
  2. westeuropevbnet
  3. uksouthvbnet

在变量中我会把这个:

"variables": {
    "storageAccountName": "[concat(resourceGroup().location,'vbnet')]"
  }