在资源管理器模板中设置资源位置Set resource location in Resource Manager template

部署模板时,必须提供每个资源的位置。When deploying a template, you must provide a location for each resource. 该位置不必与资源组位置相同。The location doesn't need to be the same location as the resource group location.

获取可用位置Get available locations

不同的位置中支持不同的资源类型。Different resource types are supported in different locations. 若要获取资源类型支持的位置,请使用 Azure PowerShell 或 Azure CLI。To get the supported locations for a resource type, use Azure PowerShell or Azure CLI.

((Get-AzResourceProvider -ProviderNamespace Microsoft.Batch).ResourceTypes `
  | Where-Object ResourceTypeName -eq batchAccounts).Locations

使用 location 参数Use location parameter

若要在部署模板时实现灵活性,请使用参数指定资源的位置。To allow for flexibility when deploying your template, use a parameter to specify the location for resources. 将参数的默认值设置为 resourceGroup().locationSet the default value of the parameter to resourceGroup().location.

以下示例显示了部署到作为参数指定的位置的存储帐户:The following example shows a storage account that is deployed to a location specified as a parameter:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "storageAccountName": "[concat('storage', uniquestring(resourceGroup().id))]"
  },
  "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')]"
    }
  }
}

后续步骤Next steps