ARM 範本的範圍函式

Resource Manager 提供下列函式,在您的 Azure Resource Manager 範本 (ARM 範本) 中取得部署範圍值:

若要從參數、變數或目前的部署中取得值,請參閱 部署值函式

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱範圍函式。

managementGroup

managementGroup()

傳回具有目前部署管理群組屬性的物件。

在 Bicep 中,使用 managementGroup 範圍函式。

備註

managementGroup() 僅可用於管理群組部署。 此函式會傳回部署作業的目前管理群組。 用來取得目前管理群組的屬性。

傳回值

具有目前管理群組屬性的物件。

管理群組範例

下列範例會傳回目前管理群組的屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "mgInfo": "[managementGroup()]"
  },
  "resources": [],
  "outputs": {
    "mgResult": {
      "type": "object",
      "value": "[variables('mgInfo')]"
    }
  }
}

傳回的會是:

"mgResult": {
  "type": "Object",
  "value": {
    "id": "/providers/Microsoft.Management/managementGroups/examplemg1",
    "name": "examplemg1",
    "properties": {
      "details": {
        "parent": {
          "displayName": "Tenant Root Group",
          "id": "/providers/Microsoft.Management/managementGroups/00000000-0000-0000-0000-000000000000",
          "name": "00000000-0000-0000-0000-000000000000"
        },
        "updatedBy": "00000000-0000-0000-0000-000000000000",
        "updatedTime": "2020-07-23T21:05:52.661306Z",
        "version": "1"
      },
      "displayName": "Example MG 1",
      "tenantId": "00000000-0000-0000-0000-000000000000"
    },
    "type": "/providers/Microsoft.Management/managementGroups"
  }
}

下一個範例會建立新的管理群組,並使用此函式來設定父管理群組。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "mgName": {
      "type": "string",
      "defaultValue": "[format('mg-{0}', uniqueString(newGuid()))]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Management/managementGroups",
      "apiVersion": "2020-05-01",
      "scope": "/",
      "name": "[parameters('mgName')]",
      "properties": {
        "details": {
          "parent": {
            "id": "[managementGroup().id]"
          }
        }
      }
    }
  ],
  "outputs": {
    "newManagementGroup": {
      "type": "string",
      "value": "[parameters('mgName')]"
    }
  }
}

resourceGroup

resourceGroup()

傳回代表目前資源群組的物件。

在 Bicep 中,使用 resourceGroup 範圍函式。

傳回值

傳回的物件會使用下列格式:

{
  "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}",
  "name": "{resourceGroupName}",
  "type":"Microsoft.Resources/resourceGroups",
  "location": "{resourceGroupLocation}",
  "managedBy": "{identifier-of-managing-resource}",
  "tags": {
  },
  "properties": {
    "provisioningState": "{status}"
  }
}

所含的資源由其他服務管理的資源群組,才會傳回 managedBy 屬性。 就受控應用程式、Databricks 和 AKS 而言,該屬性的值將是管理端資源的資源識別碼。

備註

resourceGroup() 函式不能用於部署在訂用帳戶層級中的範本, 只能用於部署到資源群組中的範本。 您可以在以資源群組為目標的連結或巢狀範本 (具有內部範圍) 中使用 resourceGroup() 函式,即使已將父範本部署至訂用帳戶亦然。 在此情況下,連結或巢狀範本會在資源群組層級部署。 如需關於在訂用帳戶層級部署中將資源群組設為目標的詳細資訊,請參閱將 Azure 資源部署至多個訂用帳戶和資源群組

resourceGroup 函式的常見用法是在和資源群組相同的位置中建立資源。 下列範例會使用資源群組位置作為預設參數值。

"parameters": {
  "location": {
    "type": "string",
    "defaultValue": "[resourceGroup().location]"
  }
}

您也可以使用 resourceGroup 函式,將資源群組的標記套用至資源。 如需詳細資訊,請參閱從資源群組套用標籤

使用巢狀範本部署至多個資源群組時,您可以指定評估 resourceGroup 函式的範圍。 如需詳細資訊,請參閱將 Azure 資源部署至多個訂用帳戶或資源群組

資源群組範例

下列範例會傳回資源群組的屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "resourceGroupOutput": {
      "type": "object",
      "value": "[resourceGroup()]"
    }
  }
}

上述範例會以下列格式傳回物件︰

{
  "id": "/subscriptions/{subscription-id}/resourceGroups/examplegroup",
  "name": "examplegroup",
  "type":"Microsoft.Resources/resourceGroups",
  "location": "southcentralus",
  "properties": {
    "provisioningState": "Succeeded"
  }
}

訂用帳戶

subscription()

傳回目前部署的訂用帳戶詳細資料。

在 Bicep 中,使用 subscription 範圍函式。

傳回值

此函式會傳回下列格式︰

{
  "id": "/subscriptions/{subscription-id}",
  "subscriptionId": "{subscription-id}",
  "tenantId": "{tenant-id}",
  "displayName": "{name-of-subscription}"
}

備註

使用巢狀範本部署至多個訂用帳戶時,您可以指定評估訂用帳戶函式的範圍。 如需詳細資訊,請參閱將 Azure 資源部署至多個訂用帳戶或資源群組

訂用帳戶範例

下列範例顯示在 outputs 區段中所呼叫的 subscription 函式。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "subscriptionOutput": {
      "type": "object",
      "value": "[subscription()]"
    }
  }
}

tenant

tenant()

傳回使用者的租用戶。

在 Bicep 中,使用 tenant 範圍函式。

備註

tenant() 可搭配任何部署範圍使用。 此函式一律傳回目前的租用戶。 使用此函式來取得目前租用戶的屬性。

設定連結範本或延伸模組資源的範圍時,請使用下列語法:"scope": "/"

傳回值

具有目前租用戶相關屬性的物件。

租用戶範例

下列範例會傳回租用戶的屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "tenantInfo": "[tenant()]"
  },
  "resources": [],
  "outputs": {
    "tenantResult": {
      "type": "object",
      "value": "[variables('tenantInfo')]"
    }
  }
}

傳回的會是:

"tenantResult": {
  "type": "Object",
  "value": {
    "countryCode": "US",
    "displayName": "Contoso",
    "id": "/tenants/00000000-0000-0000-0000-000000000000",
    "tenantId": "00000000-0000-0000-0000-000000000000"
  }
}

下一步