在 ARM 範本中設定延伸模組資源的範圍
延伸模組資源是可修改其他資源的資源。 例如,您可以將角色指派給資源。 角色指派是一種延伸模組資源類型。
如需延伸模組資源類型的完整清單,請參閱能擴充其他資源功能的資源類型。
本文示範如何在使用 Azure Resource Manager 範本 (ARM 範本) 部署時,設定延伸模組資源類型的範圍。 其描述在套用到資源時,適用於延伸模組資源的範圍屬性。
在部署範圍內套用
若要在目標部署範圍內套用延伸模組資源類型,可將資源新增至範本,就像使用任何資源類型一樣。 可用的範圍包括資源群組、訂用帳戶、管理群組和租用戶。 部署範圍必須支援資源類型。
下列範本會部署鎖定。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
},
"resources": [
{
"type": "Microsoft.Authorization/locks",
"apiVersion": "2016-09-01",
"name": "rgLock",
"properties": {
"level": "CanNotDelete",
"notes": "Resource Group should not be deleted."
}
}
]
}
部署至資源群組時,會鎖定資源群組。
az deployment group create \
--resource-group ExampleGroup \
--template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/scope/locktargetscope.json"
下一個範例會指派角色。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
"principalId": {
"type": "string",
"metadata": {
"description": "The principal to assign the role to"
}
},
"builtInRoleType": {
"type": "string",
"allowedValues": [
"Owner",
"Contributor",
"Reader"
],
"metadata": {
"description": "Built-in role to assign"
}
},
"roleNameGuid": {
"type": "string",
"metadata": {
"description": "The role assignment name"
}
}
},
"variables": {
"Owner": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635')]",
"Contributor": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'b24988ac-6180-42a0-ab88-20f7382dd24c')]",
"Reader": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')]"
},
"resources": [
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2020-10-01-preview",
"name": "[parameters('roleNameGuid')]",
"properties": {
"roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
"principalId": "[parameters('principalId')]"
}
}
],
"outputs": {}
}
部署至訂用帳戶時,會將角色指派給訂用帳戶。
az deployment sub create \
--name demoSubDeployment \
--location centralus \
--template-uri "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/scope/roletargetscope.json"
套用到資源
若要將延伸模組資源套用到資源,請使用 scope
屬性。 將範圍屬性設定為您要新增延伸模組的資源名稱。 範圍屬性是延伸模組資源類型的根屬性。
下列範例會建立儲存體帳戶,並將角色套用到該帳戶。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"principalId": {
"type": "string",
"metadata": {
"description": "The principal to assign the role to"
}
},
"builtInRoleType": {
"type": "string",
"allowedValues": [
"Owner",
"Contributor",
"Reader"
],
"metadata": {
"description": "Built-in role to assign"
}
},
"roleNameGuid": {
"type": "string",
"defaultValue": "[newGuid()]",
"metadata": {
"description": "A new GUID used to identify the role assignment"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"variables": {
"Owner": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635')]",
"Contributor": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'b24988ac-6180-42a0-ab88-20f7382dd24c')]",
"Reader": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')]",
"storageName": "[concat('storage', uniqueString(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[variables('storageName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
},
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2020-10-01-preview",
"name": "[parameters('roleNameGuid')]",
"scope": "[concat('Microsoft.Storage/storageAccounts', '/', variables('storageName'))]",
"dependsOn": [
"[variables('storageName')]"
],
"properties": {
"roleDefinitionId": "[variables(parameters('builtInRoleType'))]",
"principalId": "[parameters('principalId')]"
}
}
]
}
resourceGroup 和訂用帳戶屬性僅允許用於巢狀或連結的部署上。 個別資源上不允許這些屬性。 如果您想要使用範圍設定將延伸模組資源部署至不同資源群組中的資源,請使用巢狀或連結的部署。
下一步
- 若要瞭解如何在您的範本中定義參數,請參閱瞭解 ARM 範本的結構和語法。
- 如需解決常見部署錯誤的秘訣,請參閱使用 Azure Resource Manager 針對常見的 Azure 部署錯誤進行疑難排解。
- 如需了解如何部署需要 SAS 權杖的範本,請參閱使用 SAS 權杖部署私人 ARM 範本。