使用 ARM 範本進行租用戶部署

隨著您的組織逐漸成熟,您可能需要定義和指派 Microsoft Entra 租用戶中的原則Azure 角色型存取控制 (Azure RBAC)。 您可以使用租用戶層級範本,以宣告方式套用原則,並在全域層級指派角色。

提示

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

支援的資源

並非所有的資源類型都可部署至租用戶等級。 本節將列出支援的資源類型。

針對 Azure 角色型存取控制 (Azure RBAC),請使用:

若為部署至管理群組、訂閱或資源群組的巢狀範本,請使用:

若為建立管理群組,請使用:

若為建立訂閱,請使用:

若要管理成本,請使用:

若為設定入口網站,請使用:

內建原則定義是租用戶等級的資源,但您無法在租用戶部署自訂原則定義。 如需將內建原則定義指派給資源的範例,請參閱 tenantResourceId 範例

結構描述

您用於租用戶部署的結構描述與用於資源群組部署的結構描述不同。

針對範本,請使用:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
  ...
}

所有部署範圍的參數檔案結構描述都相同。 針對參數檔案,請使用:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  ...
}

必要的存取權

部署範本的主體必須具備在租用戶範圍建立資源的權限。 主體必須擁有執行部署動作 (Microsoft.Resources/deployments/*) 並建立在範本中定義之資源的權限。 例如,若要建立管理群組,主體必須具備租用戶範圍的「參與者」權限。 若要建立角色指派,主體必須具備「擁有者」權限。

Microsoft Entra ID 的全域管理員不會自動具備指派角色的權限。 若要在租用戶範圍啟用範本部署,全域管理員必須執行下列步驟:

  1. 提升帳戶存取權,讓全域管理員可以指派角色。 如需詳細資訊,請參閱提高存取權以管理所有 Azure 訂用帳戶和管理群組

  2. 將「擁有者」或「參與者」指派給需要部署範本的主體。

    New-AzRoleAssignment -SignInName "[userId]" -Scope "/" -RoleDefinitionName "Owner"
    
    az role assignment create --assignee "[userId]" --scope "/" --role "Owner"
    

主體現在具備部署範本所需的權限。

部署命令

用於租用戶部署的命令與用於資源群組部署的命令不同。

針對 Azure CLI,使用 az deployment tenant create

az deployment tenant create \
  --name demoTenantDeployment \
  --location WestUS \
  --template-uri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/tenant-deployments/new-mg/azuredeploy.json"

有關用於部署 ARM 範本的部署命令和選項,如需詳細資訊,請參閱:

部署位置和名稱

針對租用戶層級部署,您必須提供部署的位置。 部署的位置與您部署的資源位置不同。 部署位置會指定部署資料的儲存位置。 訂閱管理群組部署也需要位置。 針對資源群組 (部分機器翻譯) 部署,資源群組的位置會用來儲存部署資料。

您可以提供部署的名稱,或使用預設的部署名稱。 預設名稱是範本檔案的名稱。 例如,部署名為 azuredeploy.json 的範本會建立預設的部署名稱 azuredeploy

對於每個部署名稱而言,此位置是不可變的。 當某個位置已經有名稱相同的現有部署時,您無法在其他位置建立部署。 例如,如果您在 centralus 中建立名稱為 deployment1 的租用戶部署,稍後就無法再使用名稱 deployment1 建立另一個部署,而只能在 westus 的位置建立另一個部署。 如果您收到錯誤代碼 InvalidDeploymentLocation,請使用不同的名稱或與先前該名稱部署相同的位置。

部署範圍

部署至租用戶時,您可以將資源部署至:

  • 租用戶
  • 租用戶中的管理群組
  • 訂用帳戶
  • resource groups

延伸模組資源的範圍可以設為與部署目標不同的目標。

部署範本的使用者必須能夠存取指定的範圍。

本節說明如何指定不同的範圍。 您可以將這些不同範圍結合在單一範本中。

將範圍設為租用戶

在範本的資源區段中定義的資源會套用至租用戶。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    tenant-resources
  ],
  "outputs": {}
}

將範圍設為管理群組

若要鎖定租用戶內的管理群組,請新增巢狀部署並指定 scope 屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "mgName": {
      "type": "string"
    }
  },
  "variables": {
    "mgId": "[concat('Microsoft.Management/managementGroups/', parameters('mgName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "nestedMG",
      "scope": "[variables('mgId')]",
      "location": "eastus",
      "properties": {
        "mode": "Incremental",
        "template": {
          management-group-resources
        }
      }
    }
  ],
  "outputs": {}
}

將範圍設為訂閱

您也可以將租用戶中的訂用帳戶設為目標。 部署範本的使用者必須有指定範圍的存取權。

若要將租用戶中的訂用帳戶設定為目標,請使用巢狀部署和 subscriptionId 屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "nestedSub",
      "location": "westus2",
      "subscriptionId": "00000000-0000-0000-0000-000000000000",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              subscription-resources
            }
          ]
        }
      }
    }
  ]
}

將範圍設為資源群組

您也可以將租用戶內的資源群組設為目標。 部署範本的使用者必須有指定範圍的存取權。

若要將租用戶內的資源群組設為目標,請使用巢狀部署。 設定 subscriptionIdresourceGroup 屬性。 請勿設定巢狀部署的位置,因其是部署在資源群組的位置。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "nestedRGDeploy",
      "subscriptionId": "00000000-0000-0000-0000-000000000000",
      "resourceGroup": "demoResourceGroup",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              resource-group-resources
            }
          ]
        }
      }
    }
  ]
}

建立管理群組

下列範本會建立管理群組。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "mgName": {
        "type": "string",
        "defaultValue": "[concat('mg-', uniqueString(newGuid()))]"
      }
    },
    "resources": [
      {
        "type": "Microsoft.Management/managementGroups",
        "apiVersion": "2020-02-01",
        "name": "[parameters('mgName')]",
        "properties": {
        }
      }
    ]
  }

如果您的帳戶沒有部署至租用戶的權限,您仍然可以藉由部署至另一個範圍,來建立管理群組。 如需詳細資訊,請參閱管理群組

指派角色

下列範本會在租用戶範圍內指派角色。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.5.6.12127",
      "templateHash": "17107802581699825924"
    }
  },
  "parameters": {
    "principalId": {
      "type": "string",
      "metadata": {
        "description": "principalId if the user that will be given contributor access to the tenant"
      }
    },
    "roleDefinitionId": {
      "type": "string",
      "defaultValue": "8e3af657-a8ff-443c-a75c-2fe8c4bcb635",
      "metadata": {
        "description": "roleDefinition for the assignment - default is owner"
      }
    }
  },
  "variables": {
    "roleAssignmentName": "[guid('/', parameters('principalId'), parameters('roleDefinitionId'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2020-03-01-preview",
      "name": "[variables('roleAssignmentName')]",
      "properties": {
        "roleDefinitionId": "[tenantResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId'))]",
        "principalId": "[parameters('principalId')]"
      }
    }
  ]
}

下一步