Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
本文介绍如何在部署到管理组时使用 Bicep 来设置范围。
随着组织的不断发展,可以部署 Bicep 文件来创建管理组级别的资源。 例如,你可能需要为管理组定义和分配策略或 Azure 基于角色的访问控制 (Azure RBAC)。 使用管理组级别的模板,可以声明方式在管理组级别应用策略和分配角色。 有关详细信息,请参阅了解范围。
并非所有资源类型都可以部署到管理组级别。 本部分列出了支持的资源类型。
对于 Azure 蓝图,请使用:
- 项目
- 蓝图
- blueprintAssignments
- 版本
对于 Azure Policy,请使用:
- 策略分配
- 策略定义
- 策略集定义
- 整改措施
对于访问控制,请使用:
- privateLinkAssociations
- 角色分配
- 角色分配时间表请求
- 角色定义
- 角色资格计划请求
- 角色管理策略分配
对于部署到订阅或资源组的嵌套模板,请使用:
- 部署
若要管理资源,请使用:
- 诊断设置
- 标签
管理组是租户级资源。 但你可以将新管理组的范围设置为租户,从而在管理组部署中创建管理组。 请参阅管理组。
若要将范围设置为管理组,请使用:
targetScope = 'managementGroup'
若要部署到管理组,请使用管理组部署命令。
对于 Azure CLI,请使用 az deployment mg create:
az deployment mg create \
--name demoMGDeployment \
--location ChinaNorth \
--management-group-id myMG \
--template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/management-level-deployment/azuredeploy.json"
有关部署命令和部署 ARM 模板的选项的更多详细信息,请参阅:
对于管理组级别部署,必须为部署提供位置。 部署位置独立于部署的资源的位置。 部署位置指定何处存储部署数据。 订阅和租户部署也需要位置。 对于资源组部署,资源组的位置用于存储部署数据。
可以为部署提供一个名称,也可以使用默认部署名称。 默认名称是模板文件的名称。 例如,部署名为“main.bicep”的模板会创建默认部署名称 main。
每个部署名称的位置不可变。 当某个位置中已有某个部署时,无法在另一位置创建同名的部署。 例如,如果在 chinaeast 中创建名为“deployment1”的管理组部署,则以后不能创建另一个名为“deployment1”但位置为“chinanorth”的部署。 如果出现错误代码 InvalidDeploymentLocation
,请使用其他名称或使用与该名称的以前部署相同的位置。
在 Bicep 文件中,所有用 resource
关键字声明的资源都必须在与部署相同级别的范围内进行部署。 对于管理组部署,这意味着 Bicep 文件中的所有 resource
声明都必须部署到同一管理组,或部署为部署所在同一管理组中资源的子资源或扩展资源。
但此限制不适用于 existing
资源。 可以在与部署不同的范围内引用现有资源。
若要在单个部署中的多个范围内部署资源,可使用模块。 部署模块会触发“嵌套部署”,从而支持你面向不同的范围。 部署父 Bicep 文件的用户必须具有在这些范围内启动部署所需的权限。
你可以在以下范围部署管理组范围 Bicep 文件中的资源:
若要将资源部署到目标管理组,请添加那些具有关键字 resource
的资源。
targetScope = 'managementGroup'
// policy definition created in the management group
resource policyDefinition 'Microsoft.Authorization/policyDefinitions@2025-01-01' = {
...
}
若要以另一个管理组为目标,请添加模块。 使用 managementGroup 函数设置 scope
属性。 提供管理组名称。
targetScope = 'managementGroup'
param otherManagementGroupName string
// module deployed at management group level but in a different management group
module exampleModule 'module.bicep' = {
name: 'deployToDifferentMG'
scope: managementGroup(otherManagementGroupName)
}
还可以将管理组中的订阅作为目标。 部署模板的用户必须有权访问指定的作用域。
若要以管理组中的订阅为目标,请添加模块。 使用 subscription 函数设置 scope
属性。 提供订阅 ID。
targetScope = 'managementGroup'
param subscriptionID string
// module deployed to subscription in the management group
module exampleModule 'module.bicep' = {
name: 'deployToSub'
scope: subscription(subscriptionID)
}
还可以将管理组中的资源组作为目标。 部署模板的用户必须有权访问指定的作用域。
若要以管理组中的资源组为目标,请添加模块。 使用 resourceGroup 函数设置 scope
属性。 提供订阅 ID 和资源组名称。
targetScope = 'managementGroup'
param subscriptionID string
param resourceGroupName string
// module deployed to resource group in the management group
module exampleModule 'module.bicep' = {
name: 'deployToRG'
scope: resourceGroup(subscriptionID, resourceGroupName)
}
要在租户中创建资源,请添加一个模块。 使用 tenant 函数设置其 scope
属性。 部署模板的用户必须具有在租户中进行部署所需的访问权限。
targetScope = 'managementGroup'
// module deployed at tenant level
module exampleModule 'module.bicep' = {
name: 'deployToTenant'
scope: tenant()
}
或者,可将某些资源类型(如管理组)的范围设置为 /
。 下一部分将介绍如何创建新的管理组。
若要在管理组部署中创建管理组,则必须将管理组的范围设置为租户。
下面的示例在根管理组中创建了一个新的管理组。
targetScope = 'managementGroup'
param mgName string = 'mg-${uniqueString(newGuid())}'
resource newMG 'Microsoft.Management/managementGroups@2023-04-01' = {
scope: tenant()
name: mgName
properties: {}
}
output newManagementGroup string = mgName
下一个示例将在部署的目标管理组中创建一个新的管理组。 下一个示例使用管理组函数。
targetScope = 'managementGroup'
param mgName string = 'mg-${uniqueString(newGuid())}'
resource newMG 'Microsoft.Management/managementGroups@2023-04-01' = {
scope: tenant()
name: mgName
properties: {
details: {
parent: {
id: managementGroup().id
}
}
}
}
output newManagementGroup string = mgName
若要部署一个会将现有 Azure 订阅移至新管理组的模板,请参阅在 ARM 模板中移动订阅
部署到管理组的自定义策略定义是管理组的扩展。 若要获取自定义策略定义的 ID,请使用 extensionResourceId() 函数。 内置策略定义是租户级别资源。 若要获取内置策略定义的 ID,请使用 tenantResourceId 函数。
下面的示例演示如何在管理组级别定义策略,以及如何分配它。
targetScope = 'managementGroup'
@description('An array of the allowed locations, all other locations will be denied by the created policy.')
param allowedLocations array = [
'chinaeast2'
'chinaeast'
'chinanorth'
]
resource policyDefinition 'Microsoft.Authorization/policyDefinitions@2023-04-01' = {
name: 'locationRestriction'
properties: {
policyType: 'Custom'
mode: 'All'
parameters: {}
policyRule: {
if: {
not: {
field: 'location'
in: allowedLocations
}
}
then: {
effect: 'deny'
}
}
}
}
resource policyAssignment 'Microsoft.Authorization/policyAssignments@2024-04-01' = {
name: 'locationAssignment'
properties: {
policyDefinitionId: policyDefinition.id
}
}
若要了解其他范围,请参阅: