共用方式為


部署範圍中的 ARM 範本函數

您可以使用 Azure Resource Manager 範本 (ARM 範本),部署至資源群組、訂用帳戶、管理群組或租用戶。 一般而言,ARM 範本函數在所有範圍中的運作方式都相同。 本文說明部分函數因範圍而存在的差異。

支援的函數

部署至不同的範圍時,有一些重要的考量事項:

  • 資源群組部署支援resourceGroup() 函數。

  • 資源群組和訂用帳戶部署支援subscription() 函數。

  • 所有範圍皆支援reference()list() 函數。

  • 使用 resourceId() 取得部署在資源群組的資源之識別碼。

    "subnet": {
      "id": "[resourceId(parameters('virtualNetworkResourceGroup'), 'Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnet1Name'))]"
    }
    
  • 使用 subscriptionResourceId() 函數取得部署在訂用帳戶的資源之識別碼。

    例如,若要取得部署至訂用帳戶的原則定義之資源識別碼,請使用:

    "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')]"
    
  • extensionResourceId() 函數可用於實作為管理群組延伸模組的資源。 部署至管理群組的自訂原則定義是管理群組的延伸。

    若要在管理群組層級取得自訂原則定義的資源識別碼,請使用:

    "policyDefinitionId": "[extensionResourceId(variables('mgScope'), 'Microsoft.Authorization/policyDefinitions', parameters('policyDefinitionID'))]"
    
  • 使用 tenantResourceId() 函數取得部署在租用戶的資源識別碼。 內建原則定義是租用戶層級的資源。 在管理群組層級指派內建原則時,請使用 tenantResourceId 函數。

    若要取得內建原則定義的資源識別碼,請使用:

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

範圍中的函數解析

部署至一個以上的範圍時,resourceGroup()subscription() 函數會根據您指定範本的方式進行不同的解析。 當您連結至外部範本時,函式一律會解析為該範本的範圍。 當您將範本巢狀嵌入父代範本中時,請使用 expressionEvaluationOptions 屬性來指定函式要解析為父代範本或巢狀範本的資源群組和訂用帳戶。 屬性設定為 inner,會解析成巢狀範本的範圍。 屬性設定為 outer,會解析成父代範本的範圍。

下表顯示函式會解析成父代或內嵌的資源群組和訂用帳戶。

範本類型 範圍 解決方法
巢狀 外部 (預設值) 父代資源群組
巢狀 inner 子資源群組
連結 N/A 子資源群組

以下範例範本示範:

  • 具有預設 (外部) 範圍的巢狀範本
  • 具有內部範圍的巢狀範本
  • 連結的範本
{
  "$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 southcentralus
New-AzResourceGroup -Name inlineGroup -Location southcentralus
New-AzResourceGroup -Name linkedGroup -Location southcentralus

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

下一步