你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 ARM 模板进行订阅部署
若要简化资源管理,可以使用 Azure 资源管理器模板(ARM 模板)在 Azure 订阅级别部署资源。 例如,可以将策略和 Azure 基于角色的访问控制 (Azure RBAC) 部署到你的订阅中,从而将它们应用于整个订阅。 还可以在订阅中创建资源组,然后将资源部署到订阅中的资源组。
注意
可在订阅级别部署中部署到 800 个不同的资源组。
若要在订阅级别部署模板,请使用 Azure CLI、PowerShell、REST API 或门户。
支持的资源
并非所有资源类型都可以部署到订阅级别。 本部分列出了支持的资源类型。
对于 Azure 蓝图,请使用:
对于 Azure 策略,请使用:
对于访问控制,请使用:
- accessReviewScheduleDefinitions
- accessReviewScheduleSettings
- roleAssignments
- roleAssignmentScheduleRequests
- roleDefinitions
- roleEligibilityScheduleRequests
- roleManagementPolicyAssignments
对于部署到资源组的嵌套模板,请使用:
若要创建新的资源组,请使用:
若要管理订阅,请使用:
对于监视,请使用:
对于安全性,请使用:
- advancedThreatProtectionSettings
- alertsSuppressionRules
- assessmentMetadata
- assessments
- autoProvisioningSettings
- connectors
- deviceSecurityGroups
- ingestionSettings
- pricings
- securityContacts
- 设置
- workspaceSettings
其他支持的类型包括:
架构
用于订阅级别部署的架构不同于资源组部署的架构。
对于模板,请使用:
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
...
}
对于所有部署范围,参数文件的架构都相同。 对于参数文件,请使用:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
...
}
部署命令
若要部署到订阅,请使用订阅级别部署命名。
对于 Azure CLI,请使用 az deployment sub create。 以下示例会部署一个模板来创建资源组:
az deployment sub create \
--name demoSubDeployment \
--location centralus \
--template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/emptyrg.json" \
--parameters rgName=demoResourceGroup rgLocation=centralus
有关部署命令和部署 ARM 模板的选项的更多详细信息,请参阅:
- 使用 ARM 模板和 Azure 门户部署资源
- 使用 ARM 模板和 Azure CLI 部署资源
- 使用 ARM 模板和 Azure PowerShell 部署资源
- 使用 ARM 模板和 Azure 资源管理器 REST API 部署资源
- 使用部署按钮从 GitHub 存储库部署模板
- 从 Cloud Shell 部署 ARM 模板
部署位置和名称
对于订阅级别部署,必须为部署提供位置。 部署位置独立于部署的资源的位置。 部署位置指定何处存储部署数据。 管理组和租户部署也需要位置。 对于资源组部署,资源组的位置用于存储部署数据。
可以为部署提供一个名称,也可以使用默认部署名称。 默认名称是模板文件的名称。 例如,部署一个名为 azuredeploy.json 的模板将创建默认部署名称 azuredeploy。
每个部署名称的位置不可变。 当某个位置中已有某个部署时,无法在另一位置创建同名的部署。 例如,如果在 centralus中创建名为 “deployment1” 的订阅部署,则以后不能创建另一个名为 “deployment1” 但位置为 “westus” 的部署。 如果出现错误代码 InvalidDeploymentLocation
,请使用其他名称或使用与该名称的以前部署相同的位置。
部署范围
部署到订阅时,可以将资源部署到:
- 操作中的目标订阅
- 租户中的任何订阅
- 该订阅或其他订阅中的资源组
- 订阅的租户
唯一禁止的范围转换发生在从资源组到管理组或从订阅到管理组的时候。
可以将扩展资源的范围设置为与部署目标不同的范围。
部署模板的用户必须有权访问指定的作用域。
本部分演示如何指定不同范围。 可以在单个模板中组合这些不同范围。
将范围限制为目标订阅
若要将资源部署到目标订阅,请将这些资源添加到模板的资源部分。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
subscription-level-resources
],
"outputs": {}
}
将范围限制为其他订阅
若要将资源部署到与操作中的订阅不同的订阅,请添加嵌套部署。 将 subscriptionId
属性设置为要部署到的订阅的 ID。 为嵌套部署设置 location
属性。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2021-04-01",
"name": "nestedDeployment",
"subscriptionId": "00000000-0000-0000-0000-000000000000",
"location": "westus",
"properties": {
"mode": "Incremental",
"template": {
subscription-resources
}
}
}
],
"outputs": {}
}
将范围限定于资源组
若要将资源部署到订阅中的资源组,请添加嵌套部署并包括 resourceGroup
属性。 在以下示例中,嵌套部署以名为 demoResourceGroup
的资源组为目标。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2021-04-01",
"name": "nestedDeployment",
"resourceGroup": "demoResourceGroup",
"properties": {
"mode": "Incremental",
"template": {
resource-group-resources
}
}
}
],
"outputs": {}
}
有关部署到资源组的示例,请参阅创建资源组和资源。
将范围设定为租户
若要在租户中创建资源,请将 scope
设置为 /
。 部署模板的用户必须具有在租户中进行部署所需的访问权限。
若要使用嵌套部署,请设置 scope
和 location
。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2021-04-01",
"name": "nestedDeployment",
"location": "centralus",
"scope": "/",
"properties": {
"mode": "Incremental",
"template": {
tenant-resources
}
}
}
],
"outputs": {}
}
或者,可将某些资源类型(如管理组)的范围设置为 /
。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"mgName": {
"type": "string",
"defaultValue": "[concat('mg-', uniqueString(newGuid()))]"
}
},
"resources": [
{
"type": "Microsoft.Management/managementGroups",
"apiVersion": "2021-04-01",
"name": "[parameters('mgName')]",
"scope": "/",
"location": "eastus",
"properties": {}
}
],
"outputs": {
"output": {
"type": "string",
"value": "[parameters('mgName')]"
}
}
}
有关详细信息,请参阅管理组。
资源组
创建资源组
若要在 ARM 模板中创建资源组,请为该资源组定义包含名称和位置的 Microsoft.Resources/resourceGroups 资源。
以下模板创建空资源组。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"rgName": {
"type": "string"
},
"rgLocation": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2022-09-01",
"name": "[parameters('rgName')]",
"location": "[parameters('rgLocation')]",
"properties": {}
}
],
"outputs": {}
}
结合使用 copy 元素与资源组来创建多个资源组。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"rgNamePrefix": {
"type": "string"
},
"rgLocation": {
"type": "string"
},
"instanceCount": {
"type": "int"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2022-09-01",
"location": "[parameters('rgLocation')]",
"name": "[concat(parameters('rgNamePrefix'), copyIndex())]",
"copy": {
"name": "rgCopy",
"count": "[parameters('instanceCount')]"
},
"properties": {}
}
],
"outputs": {}
}
有关资源迭代的信息,请参阅 ARM 模板中的资源迭代和教程:使用 ARM 模板创建多个资源实例。
创建资源组和资源
若要创建资源组并向其部署资源,请使用嵌套模板。 嵌套模板定义要部署到资源组的资源。 将嵌套模板设置为依赖于资源组,确保资源组存在,然后再部署资源。 最多可部署到 800 个资源组。
以下示例将创建一个资源组,并向该资源组部署存储帐户。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"rgName": {
"type": "string"
},
"rgLocation": {
"type": "string"
},
"storagePrefix": {
"type": "string",
"maxLength": 11
}
},
"variables": {
"storageName": "[format('{0}{1}', parameters('storagePrefix'), uniqueString(subscription().id, parameters('rgName')))]"
},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2022-09-01",
"name": "[parameters('rgName')]",
"location": "[parameters('rgLocation')]",
"properties": {}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2022-09-01",
"name": "storageDeployment",
"resourceGroup": "[parameters('rgName')]",
"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": "2022-09-01",
"name": "[variables('storageName')]",
"location": "[parameters('rgLocation')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2"
}
]
}
},
"dependsOn": [
"[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
]
}
]
}
Azure Policy
分配策略定义
以下示例将现有的策略定义分配到订阅。 如果策略定义使用参数,请将参数作为对象提供。 如果策略定义不使用参数,请使用默认的空对象。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"policyDefinitionID": {
"type": "string"
},
"policyName": {
"type": "string"
},
"policyParameters": {
"type": "object",
"defaultValue": {}
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Authorization/policyAssignments",
"apiVersion": "2020-03-01",
"name": "[parameters('policyName')]",
"properties": {
"scope": "[subscription().id]",
"policyDefinitionId": "[parameters('policyDefinitionID')]",
"parameters": "[parameters('policyParameters')]"
}
}
]
}
若要使用 Azure CLI 部署此模板,请使用:
# Built-in policy definition that accepts parameters
definition=$(az policy definition list --query "[?displayName=='Allowed locations'].id" --output tsv)
az deployment sub create \
--name demoDeployment \
--location centralus \
--template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/policyassign.json" \
--parameters policyDefinitionID=$definition policyName=setLocation policyParameters="{'listOfAllowedLocations': {'value': ['westus']} }"
若要使用 PowerShell 部署此模板,请使用:
$definition = Get-AzPolicyDefinition | Where-Object { $_.Properties.DisplayName -eq 'Allowed locations' }
$locations = @("westus", "westus2")
$policyParams =@{listOfAllowedLocations = @{ value = $locations}}
New-AzSubscriptionDeployment `
-Name policyassign `
-Location centralus `
-TemplateUri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/policyassign.json" `
-policyDefinitionID $definition.PolicyDefinitionId `
-policyName setLocation `
-policyParameters $policyParams
创建和分配策略定义
可在同一模板中定义和分配策略定义。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [
{
"type": "Microsoft.Authorization/policyDefinitions",
"apiVersion": "2020-03-01",
"name": "locationpolicy",
"properties": {
"policyType": "Custom",
"parameters": {},
"policyRule": {
"if": {
"field": "location",
"equals": "northeurope"
},
"then": {
"effect": "deny"
}
}
}
},
{
"type": "Microsoft.Authorization/policyAssignments",
"apiVersion": "2020-03-01",
"name": "location-lock",
"dependsOn": [
"locationpolicy"
],
"properties": {
"scope": "[subscription().id]",
"policyDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/policyDefinitions', 'locationpolicy')]"
}
}
]
}
若要在订阅中创建策略定义,然后将其分配到订阅,请使用以下 CLI 命令:
az deployment sub create \
--name demoDeployment \
--location centralus \
--template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/policydefineandassign.json"
若要使用 PowerShell 部署此模板,请使用:
New-AzSubscriptionDeployment `
-Name definePolicy `
-Location centralus `
-TemplateUri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/policydefineandassign.json"
Azure 蓝图
创建蓝图定义
可通过模板创建蓝图定义。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"blueprintName": {
"defaultValue": "sample-blueprint",
"type": "String",
"metadata": {
"description": "The name of the blueprint definition."
}
}
},
"resources": [
{
"type": "Microsoft.Blueprint/blueprints",
"apiVersion": "2018-11-01-preview",
"name": "[parameters('blueprintName')]",
"properties": {
"targetScope": "subscription",
"description": "Blueprint with a policy assignment artifact.",
"resourceGroups": {
"sampleRg": {
"description": "Resource group to add the assignment to."
}
},
"parameters": {
"listOfResourceTypesNotAllowed": {
"type": "array",
"metadata": {
"displayName": "Resource types to pass to the policy assignment artifact."
},
"defaultValue": [
"Citrix.Cloud/accounts"
]
}
}
}
},
{
"type": "Microsoft.Blueprint/blueprints/artifacts",
"apiVersion": "2018-11-01-preview",
"name": "[concat(parameters('blueprintName'), '/policyArtifact')]",
"kind": "policyAssignment",
"dependsOn": [
"[parameters('blueprintName')]"
],
"properties": {
"displayName": "Blocked Resource Types policy definition",
"description": "Block certain resource types",
"policyDefinitionId": "[tenantResourceId('Microsoft.Authorization/policyDefinitions', '6c112d4e-5bc7-47ae-a041-ea2d9dccd749')]",
"resourceGroup": "sampleRg",
"parameters": {
"listOfResourceTypesNotAllowed": {
"value": "[[parameters('listOfResourceTypesNotAllowed')]"
}
}
}
}
]
}
若要在订阅中创建蓝图定义,请使用以下 CLI 命令:
az deployment sub create \
--name demoDeployment \
--location centralus \
--template-uri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/subscription-deployments/blueprints-new-blueprint/azuredeploy.json"
若要使用 PowerShell 部署此模板,请使用:
New-AzSubscriptionDeployment `
-Name demoDeployment `
-Location centralus `
-TemplateUri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/subscription-deployments/blueprints-new-blueprint/azuredeploy.json"
访问控制
若要了解如何分配角色,请参阅使用 Azure 资源管理器模板分配 Azure 角色。
以下示例创建一个资源组,对其应用锁定,并为主体分配一个角色。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.5.6.12127",
"templateHash": "16815708176905569328"
}
},
"parameters": {
"rgName": {
"type": "string",
"metadata": {
"description": "Name of the resourceGroup to create"
}
},
"rgLocation": {
"type": "string",
"metadata": {
"description": "Location for the resourceGroup"
}
},
"principalId": {
"type": "string",
"metadata": {
"description": "principalId of the user that will be given contributor access to the resourceGroup"
}
},
"roleDefinitionId": {
"type": "string",
"defaultValue": "b24988ac-6180-42a0-ab88-20f7382dd24c",
"metadata": {
"description": "roleDefinition to apply to the resourceGroup - default is contributor"
}
},
"roleAssignmentName": {
"type": "string",
"defaultValue": "[guid(parameters('principalId'), parameters('roleDefinitionId'), parameters('rgName'))]",
"metadata": {
"description": "Unique name for the roleAssignment in the format of a guid"
}
}
},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2019-10-01",
"name": "[parameters('rgName')]",
"location": "[parameters('rgLocation')]",
"tags": {
"Note": "subscription level deployment"
},
"properties": {}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2020-10-01",
"name": "applyLock",
"resourceGroup": "[parameters('rgName')]",
"properties": {
"expressionEvaluationOptions": {
"scope": "inner"
},
"mode": "Incremental",
"parameters": {
"principalId": {
"value": "[parameters('principalId')]"
},
"roleDefinitionId": {
"value": "[parameters('roleDefinitionId')]"
},
"roleAssignmentName": {
"value": "[parameters('roleAssignmentName')]"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.5.6.12127",
"templateHash": "6034226420560042393"
}
},
"parameters": {
"principalId": {
"type": "string",
"metadata": {
"description": "principalId of the user that will be given contributor access to the resourceGroup"
}
},
"roleDefinitionId": {
"type": "string",
"metadata": {
"description": "roleDefinition to apply to the resourceGroup - default is contributor"
}
},
"roleAssignmentName": {
"type": "string",
"metadata": {
"description": "Unique name for the roleAssignment in the format of a guid"
}
}
},
"resources": [
{
"type": "Microsoft.Authorization/locks",
"apiVersion": "2016-09-01",
"name": "DontDelete",
"properties": {
"level": "CanNotDelete",
"notes": "Prevent deletion of the resourceGroup"
}
},
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2020-04-01-preview",
"name": "[guid(parameters('roleAssignmentName'))]",
"properties": {
"roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId'))]",
"principalId": "[parameters('principalId')]"
}
}
]
}
},
"dependsOn": [
"[subscriptionResourceId('Microsoft.Resources/resourceGroups', parameters('rgName'))]"
]
}
]
}
后续步骤
- 若要通过示例来了解如何为 Microsoft Defender for Cloud 部署工作区设置,请参阅 deployASCwithWorkspaceSettings.json。
- 示例模板可在 GitHub 找到。
- 还可在管理组级别和租户级别部署模板。