部署范围内的 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,以解析至父模板的范围。

下表显示了函数是否指向父资源组或嵌入的资源组以及订阅。

模板类型 Scope 决议
嵌套 外部(default) 父资源组
嵌套 子资源组
链接 N/A 子资源组

以下 示例模板 显示了一个:

  • 具有默认 (outer) 范围的嵌套模板。
  • 具有作用域的 inner 嵌套模板。
  • 关联模板
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "defaultScopeTemplate",
      "resourceGroup": "inlineGroup",
      "properties": {
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [],
          "outputs": {
            "resourceGroupOutput": {
              "type": "string",
              "value": "[resourceGroup().name]"
            }
          }
        }
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "innerScopeTemplate",
      "resourceGroup": "inlineGroup",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "inner"
        },
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [],
          "outputs": {
            "resourceGroupOutput": {
              "type": "string",
              "value": "[resourceGroup().name]"
            }
          }
        }
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "linkedTemplate",
      "resourceGroup": "linkedGroup",
      "properties": {
        "mode": "Incremental",
        "parameters": {},
        "templateLink": {
          "contentVersion": "1.0.0.0",
          "uri": "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/resourcegroupname.json"
        }
      }
    }
  ],
  "outputs": {
    "parentRG": {
      "type": "string",
      "value": "[format('Parent resource group is {0}', resourceGroup().name)]"
    },
    "defaultScopeRG": {
      "type": "string",
      "value": "[format('Default scope resource group is {0}', reference('defaultScopeTemplate').outputs.resourceGroupOutput.value)]"
    },
    "innerScopeRG": {
      "type": "string",
      "value": "[format('Inner scope resource group is {0}', reference('innerScopeTemplate').outputs.resourceGroupOutput.value)]"
    },
    "linkedRG": {
      "type": "string",
      "value": "[format('Linked resource group is {0}', 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

后续步骤