你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
随着组织的成熟,可以部署 Azure 资源管理器模板(ARM 模板),在管理组级别创建资源。 例如,可能需要为管理组定义和分配 策略 或 Azure 基于角色的访问控制(Azure RBAC )。 使用管理组级模板,可以声明性地应用策略并在管理组级别分配角色。
支持的资源
并非所有资源类型都可以部署到管理组级别。 本部分列出了支持的资源类型。
对于 Azure 蓝图,请使用:
对于 Azure Policy,请使用:
对于访问控制,请使用:
对于部署到订阅或资源组的嵌套模板,请使用:
若要管理资源,请使用:
管理组是租户级资源。 但是你可以将新管理组的范围设置为租户,从而在管理组部署中创建管理组。 请参阅管理组。
架构
用于管理组部署的架构不同于资源组部署的架构。
对于模板,请使用:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
...
}
在所有部署范围内,参数文件的架构均相同。 对于参数文件,请使用:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
...
}
部署命令
若要部署到管理组,请使用管理组部署命令。
对于 Azure CLI,请使用 az deployment mg create:
az deployment mg create \
--name demoMGDeployment \
--location WestUS \
--management-group-id myMG \
--template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/management-level-deployment/azuredeploy.json"
有关部署命令和部署 ARM 模板的选项的更多详细信息,请参阅:
- 使用 ARM 模板和 Azure 门户部署资源
- 使用 ARM 模板和 Azure CLI 部署资源
- 使用 ARM 模板和 Azure PowerShell 部署资源
- 使用 ARM 模板和 Azure 资源管理器 REST API 部署资源
- 使用部署按钮从 GitHub 存储库部署模板
- 从 Cloud Shell 部署 ARM 模板
部署位置和名称
对于管理组级部署,必须提供部署的位置。 部署位置与你部署的资源的位置不同。 部署位置指定的是存储部署数据的位置。 订阅和租户部署也需要位置。 对于资源组部署,资源组的位置用于存储部署数据。
可为部署提供名称,也可使用默认的部署名称。 默认名称就是模板文件的名称。 例如,部署一个名为 azuredeploy.json 的模板将创建默认部署名称 azuredeploy。
每个部署名称对应的位置必须相同。 当某个位置中已有某个部署时,无法在另一位置创建同名的部署。 例如,如果在 centralus 创建名为 deployment1 的管理组部署,则以后无法创建位于 westus 且名称为 deployment1 的另一个部署。 如果出现错误代码 InvalidDeploymentLocation,请使用与该名称的先前部署不同的名称或相同的位置。
部署范围
部署到管理组时,可以将资源部署到:
- 操作的目标管理组
- 租户中的另一个管理组
- 管理组中的订阅
- 管理组中的资源组
- 资源组的租户
唯一禁止的范围转换是从资源组到管理组,或从订阅到管理组。
扩展资源的范围可以限定为不同于部署目标的目标。
部署模板的用户必须有权访问指定的作用域。
本部分演示如何指定不同范围。 可以在单个模板中组合这些不同范围。
将范围设为目标管理组
模板 的资源部分 内定义的资源将应用于部署命令中的管理组:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
management-group-resources-default
],
"outputs": {}
}
将范围设为另一个管理组
若要以另一个管理组为目标,请添加嵌套部署并指定 scope 属性。 将 scope 属性设置为 Microsoft.Management/managementGroups/<mg-name> 格式的值。
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"mgName": {
"type": "string"
}
},
"variables": {
"mgId": "[format('Microsoft.Management/managementGroups/{0}', parameters('mgName'))]"
},
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "nestedDeployment",
"scope": "[variables('mgId')]",
"location": "eastus",
"properties": {
"mode": "Incremental",
"template": {
management-group-resources-non-default
}
}
}
],
"outputs": {}
}
订阅的范围
还可以将管理组中的订阅作为目标。 部署模板的用户必须有权访问指定的作用域。
若要以管理组中的订阅为目标,请使用嵌套部署和 subscriptionId 属性:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-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
}
]
}
}
}
]
}
将范围限定于资源组
还可以将管理组中的资源组作为目标。 部署模板的用户必须有权访问指定的作用域。
若要以管理组中的资源组为目标,请使用嵌套部署。 设置 subscriptionId 和 resourceGroup 属性。 不要为嵌套部署设置位置,因为它部署在资源组的位置:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-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
}
]
}
}
}
]
}
若要使用管理组部署在订阅中创建资源组,并将存储帐户部署到该资源组,请参阅 “部署到订阅和资源组”。
将范围设定为租户
若要在租户中创建资源,请将 scope 设置为 /。 部署模板的用户必须具有在租户中进行部署所需的访问权限。
若要使用嵌套部署,请设置 scope 和 location:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "nestedDeployment",
"location": "centralus",
"scope": "/",
"properties": {
"mode": "Incremental",
"template": {
tenant-resources
}
}
}
],
"outputs": {}
}
或者,您可以将某些资源类型,例如管理组的范围设置为 /。 下一部分将介绍如何创建新的管理组。
管理组
若要在管理组部署中创建管理组,必须将管理组的范围设置为 /。
以下示例在根管理组中创建新的管理组:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"mgName": {
"type": "string",
"defaultValue": "[concat('mg-', uniqueString(newGuid()))]"
}
},
"resources": [
{
"type": "Microsoft.Management/managementGroups",
"apiVersion": "2024-02-01-preview",
"name": "[parameters('mgName')]",
"scope": "/",
"location": "eastus",
"properties": {}
}
],
"outputs": {
"output": {
"type": "string",
"value": "[parameters('mgName')]"
}
}
}
下一个示例将在指定为父级的管理组中创建一个新管理组。 请注意,范围设置为 /。
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"mgName": {
"type": "string",
"defaultValue": "[concat('mg-', uniqueString(newGuid()))]"
},
"parentMG": {
"type": "string"
}
},
"resources": [
{
"name": "[parameters('mgName')]",
"type": "Microsoft.Management/managementGroups",
"apiVersion": "2024-02-01-preview",
"scope": "/",
"location": "eastus",
"properties": {
"details": {
"parent": {
"id": "[tenantResourceId('Microsoft.Management/managementGroups', parameters('parentMG'))]"
}
}
}
}
],
"outputs": {
"output": {
"type": "string",
"value": "[parameters('mgName')]"
}
}
}
订阅
若要使用 ARM 模板在管理组中创建新的 Azure 订阅,请参阅:
若要部署一个会将现有 Azure 订阅移至新管理组的模板,请参阅在 ARM 模板中移动订阅
Azure Policy
部署到管理组的自定义策略定义是管理组的扩展。 若要获取自定义策略定义的 ID,请使用函数 extensionResourceId() 。 内置策略定义是租户级资源。 若要获取内置策略定义的 ID,请使用函数 tenantResourceId() 。
以下示例演示如何在管理组级别 定义 策略并将其分配:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"targetMG": {
"type": "string",
"metadata": {
"description": "Target Management Group"
}
},
"allowedLocations": {
"type": "array",
"defaultValue": [
"australiaeast",
"australiasoutheast",
"australiacentral"
],
"metadata": {
"description": "An array of the allowed locations, all other locations will be denied by the created policy."
}
}
},
"variables": {
"mgScope": "[tenantResourceId('Microsoft.Management/managementGroups', parameters('targetMG'))]",
"policyDefinition": "LocationRestriction"
},
"resources": [
{
"type": "Microsoft.Authorization/policyDefinitions",
"name": "[variables('policyDefinition')]",
"apiVersion": "2025-03-01",
"properties": {
"policyType": "Custom",
"mode": "All",
"parameters": {
},
"policyRule": {
"if": {
"not": {
"field": "location",
"in": "[parameters('allowedLocations')]"
}
},
"then": {
"effect": "deny"
}
}
}
},
{
"type": "Microsoft.Authorization/policyAssignments",
"name": "location-lock",
"apiVersion": "2025-03-01",
"dependsOn": [
"[variables('policyDefinition')]"
],
"properties": {
"scope": "[variables('mgScope')]",
"policyDefinitionId": "[extensionResourceId(variables('mgScope'), 'Microsoft.Authorization/policyDefinitions', variables('policyDefinition'))]"
}
}
]
}
部署到订阅和资源组
在管理组级别的部署中,可以以管理组中的订阅为目标。 以下示例在订阅中创建资源组,并将存储帐户部署到该资源组:
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"nestedsubId": {
"type": "string"
},
"nestedRG": {
"type": "string"
},
"storageAccountName": {
"type": "string"
},
"nestedLocation": {
"type": "string"
}
},
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "nestedSub",
"location": "[parameters('nestedLocation')]",
"subscriptionId": "[parameters('nestedSubId')]",
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
},
"variables": {
},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2025-04-01",
"name": "[parameters('nestedRG')]",
"location": "[parameters('nestedLocation')]"
}
]
}
}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "nestedRG",
"subscriptionId": "[parameters('nestedSubId')]",
"resourceGroup": "[parameters('nestedRG')]",
"dependsOn": [
"nestedSub"
],
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2025-06-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('nestedLocation')]",
"kind": "StorageV2",
"sku": {
"name": "Standard_LRS"
}
}
]
}
}
}
]
}
后续步骤
- 若要了解如何分配角色,请参阅使用 Azure 资源管理器模板分配 Azure 角色。
- 有关为 Microsoft Defender for Cloud 部署工作区设置的示例,请参阅 deployASCwithWorkspaceSettings.json。
- 还可以在 订阅级别 和 租户级别部署模板。