部署范围内的 ARM 模板函数

利用 Azure 资源管理器模板(ARM 模板),可以部署到资源组、订阅、管理组或租户。 通常,ARM 模板函数在所有范围内的使用方法都相同。 本文介绍一些函数因范围而存在的差异。

支持的函数

部署到不同范围时,有一些重要的注意事项:

  • 资源组部署支持 resourceGroup() 函数。

  • 资源组部署和订阅部署支持 subscription() 函数。

  • 所有范围均支持 reference()list() 函数。

  • 使用 resourceId() 获取在资源组中部署的资源的 ID。

    "subnet": {
      "id": "[resourceId(parameters('virtualNetworkResourceGroup'), 'Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnet1Name'))]"
    }
    
  • 使用 subscriptionResourceId() 函数获取在订阅中部署的资源的 ID。

    例如,若要获取部署到订阅的策略定义的资源 ID,请使用:

    "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')]"
    
  • 对于作为管理组的扩展实现的资源,请使用 extensionResourceId() 函数。 部署到管理组的自定义策略定义是管理组的扩展。

    若要获取管理组级别的自定义策略定义的资源 ID,请使用:

    "policyDefinitionId": "[extensionResourceId(variables('mgScope'), 'Microsoft.Authorization/policyDefinitions', parameters('policyDefinitionID'))]"
    
  • 使用 tenantResourceId() 函数获取在租户处部署的资源的 ID。 内置策略定义是租户级别资源。 在管理组级别分配内置策略时,请使用 tenantResourceId 函数。

    若要获取内置策略定义的资源 ID,请使用:

    "policyDefinitionId": "[tenantResourceId('Microsoft.Authorization/policyDefinitions', parameters('policyDefinitionID'))]"
    

范围内的函数解析

当部署到多个范围时,根据指定模板的方式不同,resourceGroup()subscription() 函数解析的方式也有所不同。 链接到外部模板时,函数始终解析为该模板的作用域。 在父模板中嵌套模板时,请使用 expressionEvaluationOptions 属性指定函数是否解析为父模板或嵌套模板的资源组和订阅。 将属性设置为 inner,以便解析为嵌套模板的范围。 将属性设置为 outer,以便解析为父模板的范围。

下表显示了函数是解析为父资源组,还是解析为嵌入资源组和订阅。

模板类型 范围 解决方法
嵌套 外部(默认值) 父资源组
嵌套 内部 子资源组
链接 空值 子资源组

以下示例模板演示:

  • 具有默认(外部)范围的嵌套模板
  • 具有内部范围的嵌套模板
  • 链接的模板
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "defaultScopeTemplate",
      "resourceGroup": "inlineGroup",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {},
          "variables": {},
          "resources": [
          ],
          "outputs": {
            "resourceGroupOutput": {
              "type": "string",
              "value": "[resourceGroup().name]"
            }
          }
        },
        "parameters": {}
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "innerScopeTemplate",
      "resourceGroup": "inlineGroup",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "inner"
        },
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {},
          "variables": {},
          "resources": [
          ],
          "outputs": {
            "resourceGroupOutput": {
              "type": "string",
              "value": "[resourceGroup().name]"
            }
          }
        },
        "parameters": {}
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "linkedTemplate",
      "resourceGroup": "linkedGroup",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "contentVersion": "1.0.0.0",
          "uri": "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/resourcegroupname.json"
        },
        "parameters": {}
      }
    }
  ],
  "outputs": {
    "parentRG": {
      "type": "string",
      "value": "[concat('Parent resource group is ', resourceGroup().name)]"
    },
    "defaultScopeRG": {
      "type": "string",
      "value": "[concat('Default scope resource group is ', reference('defaultScopeTemplate').outputs.resourceGroupOutput.value)]"
    },
    "innerScopeRG": {
      "type": "string",
      "value": "[concat('Inner scope resource group is ', reference('innerScopeTemplate').outputs.resourceGroupOutput.value)]"
    },
    "linkedRG": {
      "type": "string",
      "value": "[concat('Linked resource group is ', reference('linkedTemplate').outputs.resourceGroupOutput.value)]"
    }
  }
}

若要测试上述模板并查看结果,请使用 PowerShell 或 Azure CLI。

New-AzResourceGroup -Name parentGroup -Location chinaeast
New-AzResourceGroup -Name inlineGroup -Location chinaeast
New-AzResourceGroup -Name linkedGroup -Location chinaeast

New-AzResourceGroupDeployment `
  -ResourceGroupName parentGroup `
  -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/crossresourcegroupproperties.json

前述示例的输出为:

 Name             Type                       Value
 ===============  =========================  ==========
 parentRG         String                     Parent resource group is parentGroup
 defaultScopeRG   String                     Default scope resource group is parentGroup
 innerScopeRG     String                     Inner scope resource group is inlineGroup
 linkedRG         String                     Linked resource group is linkedgroup

后续步骤