本快速入门介绍如何将 Azure 资源管理器模板(ARM 模板)打包为模板规格。然后部署该模板规格。模板规格包含用于部署存储帐户的 ARM 模板。
Prerequisites
具有活动订阅的 Azure 帐户。
免费创建帐户。
创建模板
从 ARM 模板创建模板规格。 复制以下模板,并将其另存为 C:\Templates\createStorageV1.js。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"defaultValue": "[uniqueString(resourceGroup().id)]"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2025-01-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"accessTier": "Hot"
}
}
]
}
创建模板规格
模板规格是名为 Microsoft.Resources/templateSpecs
的资源类型。 若要创建模板规格,请使用 PowerShell、Azure CLI、Azure 门户或 ARM 模板。
创建新的资源组以包含模板规格。
New-AzResourceGroup `
-Name templateSpecRG `
-Location westus2
在该资源组中创建模板规格。 将新的模板规格命名为 storageSpec。
New-AzTemplateSpec `
-Name storageSpec `
-Version "1.0" `
-ResourceGroupName templateSpecRG `
-Location westus2 `
-TemplateFile "C:\Templates\createStorageV1.json"
创建新的资源组以包含模板规格。
az group create \
--name templateSpecRG \
--location westus2
在该资源组中创建模板规格。 将新的模板规格命名为 storageSpec。
az ts create \
--name storageSpec \
--version "1.0" \
--resource-group templateSpecRG \
--location "westus2" \
--template-file "C:\Templates\createStorageV1.json"
登录 Azure 门户。
搜索模板规格。 从可用选项中选择“模板规格”。
选择 “导入模板”,然后按照说明导入之前保存的 C:\Templates\createStorageV1.json。
提供以下值:
-
名称:输入模板规格的名称。例如 storageSpec。
-
订阅:选择用于创建模板规格的 Azure 订阅。
-
资源组:选择“新建”,然后输入新的资源组名称。 例如 templateSpecRG。
-
位置:选择资源组的位置。 例如美国西部 2。
-
版本:输入模板规格的版本。使用 1.0。
选择查看 + 创建,然后选择创建。
Note
建议使用 PowerShell 或 CLI 而不是 ARM 模板来创建模板规格。这些工具会自动将链接的模板转换为与主模板关联的项目。 如果使用 ARM 模板,则需要将链接模板手动添加为项目,这可能更为复杂。
使用 ARM 模板创建模板规格时,该模板将嵌入资源定义。 复制以下模板,并将其本地保存为 createTemplateSpec.json:
Note
在嵌入的模板中,必须使用第二个左括号对所有模板表达式进行转义。 请使用 "[[
,而不是 "[
。 JSON 数组仍使用单个左括号。 请参阅以下示例中的名称和位置属性:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/templateSpecs",
"apiVersion": "2022-02-01",
"name": "storageSpec",
"location": "westus2",
"properties": {
"displayName": "Storage template spec"
}
},
{
"type": "Microsoft.Resources/templateSpecs/versions",
"apiVersion": "2022-02-01",
"name": "[format('{0}/{1}', 'storageSpec', '1.0')]",
"location": "westus2",
"properties": {
"mainTemplate": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"defaultValue": "[uniqueString(resourceGroup().id)]"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2025-01-01",
"name": "[[parameters('storageAccountName')]",
"location": "[[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"accessTier": "Hot"
}
}
]
}
},
"dependsOn": [
"storageSpec"
]
}
]
}
使用 Azure CLI 或 PowerShell 创建新的资源组。
New-AzResourceGroup `
-Name templateSpecRG `
-Location westus2
az group create \
--name templateSpecRG \
--location westus2
使用 Azure CLI 或 PowerShell 部署模板。
New-AzResourceGroupDeployment `
-ResourceGroupName templateSpecRG `
-TemplateFile "C:\Templates\createTemplateSpec.json"
az deployment group create \
--resource-group templateSpecRG \
--template-file "C:\Templates\createTemplateSepc.json"
使用 Azure CLI 或 PowerShell 验证部署。
Get-AzTemplateSpec `
-ResourceGroupName templateSpecRG `
-Name storageSpec
az ts show \
--resource-group templateSpecRG \
--name storageSpec
部署模板规格
若要部署模板规格,请使用部署模板所用的部署命令。 传入模板规格的资源 ID 以进行部署。
创建资源组以包含新的存储帐户。
New-AzResourceGroup `
-Name storageRG `
-Location westus2
获取模板规格的资源 ID。
$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "1.0").Versions.Id
部署模板规格。
New-AzResourceGroupDeployment `
-TemplateSpecId $id `
-ResourceGroupName storageRG
提供与 ARM 模板完全一样的参数。 使用存储帐户类型的参数重新部署模板规格。
New-AzResourceGroupDeployment `
-TemplateSpecId $id `
-ResourceGroupName storageRG `
-storageAccountType Standard_GRS
创建资源组以包含新的存储帐户。
az group create \
--name storageRG \
--location westus2
获取模板规格的资源 ID。
id=$(az ts show --name storageSpec --resource-group templateSpecRG --version "1.0" --query "id")
Note
获取模板规格 ID 并将其分配到 Windows PowerShell 中的变量时存在一个已知问题。
部署模板规格。
az deployment group create \
--resource-group storageRG \
--template-spec $id
提供与 ARM 模板完全一样的参数。 使用存储帐户类型的参数重新部署模板规格。
az deployment group create \
--resource-group storageRG \
--template-spec $id \
--parameters storageAccountType='Standard_GRS'
选择已创建的模板规格。 使用搜索框查找模板规格(如果有)。
选择“部署”。
提供以下值:
-
订阅:选择用于创建资源的 Azure 订阅。
-
资源组:选择“新建”,然后输入“storageRG” 。
选择“查看 + 创建”,然后选择“创建”。
复制以下模板,并将其本地保存为 deployTemplateSpecV1.json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "demo",
"properties": {
"templateLink": {
"id": "[resourceId('templateSpecRG', 'Microsoft.Resources/templateSpecs/versions', 'storageSpec', '1.0')]"
},
"parameters": {
},
"mode": "Incremental"
}
}
]
}
在模板中, templateSpecRG 是包含模板规格的资源组的名称, storageSpec 是模板规格的名称, 1.0 是模板规格的版本。
使用 Azure CLI 或 PowerShell 为存储帐户创建新的资源组。
New-AzResourceGroup `
-Name storageRG `
-Location westus2
az group create \
--name storageRG \
--location westus2
使用 Azure CLI 或 PowerShell 部署模板。
New-AzResourceGroupDeployment `
-ResourceGroupName storageRG `
-TemplateFile "C:\Templates\deployTemplateSpecV1.json"
az deployment group create \
--resource-group storageRG \
--template-file "C:\Templates\deployTemplateSpecV1.json"
使用 Azure CLI 或 PowerShell 验证部署。
Get-AzResource `
-ResourceGroupName storageRG
az resource list \
--resource-group storageRG
授予访问权限
如果要让组织中的其他用户部署模板规格,请向其授予读取权限。 对于包含要共享的模板规格的资源组,请将读者角色分配给 Microsoft Entra 组。 有关详细信息,请参阅教程:使用 Azure PowerShell 授予组对 Azure 资源的访问权限。
更新模板
若要在模板规格中更改模板,请修改模板。 以下模板类似于先前的模板,只不过它为存储帐户名称添加前缀。 复制以下模板并将其另存为 createStorageV2.json 文件。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"defaultValue": "[format('store{0}', uniqueString(resourceGroup().id))]"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2025-01-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"accessTier": "Hot"
}
}
]
}
更新模板规格版本
添加一个名为 2.0
现有模板规格的新版本,而不是为修改后的模板创建新的模板规格。可以部署任一版本。
创建新的模板规格版本。
New-AzTemplateSpec `
-Name storageSpec `
-Version "2.0" `
-ResourceGroupName templateSpecRG `
-Location westus2 `
-TemplateFile "C:\Templates\createStorageV2.json"
若要部署新版本,请获取 2.0
版本的资源 ID。
$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "2.0").Versions.Id
部署该版本。 为存储帐户名称提供一个前缀。
New-AzResourceGroupDeployment `
-TemplateSpecId $id `
-ResourceGroupName storageRG `
-namePrefix "demoaccount"
创建新的模板规格版本。
az ts create \
--name storageSpec \
--version "2.0" \
--resource-group templateSpecRG \
--location "westus2" \
--template-file "C:\Templates\createStorageV2.json"
若要部署新版本,请获取 2.0
版本的资源 ID。
id=$(az ts show --name storageSpec --resource-group templateSpecRG --version "2.0" --query "id")
部署该版本。 为存储帐户名称提供一个前缀。
az deployment group create \
--resource-group storageRG \
--template-spec $id \
--parameters namePrefix='demoaccount'
打开模板规格 storageSpec,然后选择“ 创建新版本”。
选择 1.0 作为基本模板,然后选择“ 创建”。
将新版本命名为 2.0
,并且可以根据需要添加说明。 选择“编辑模板”。
将模板的内容替换为更新的模板。 选择 “查看 + 保存” 。
选择“保存更改”。
要部署新版本,请选择“版本”。
打开新版本,然后选择“ 部署”。
像部署早期版本时一样填写字段。
选择“查看 + 创建”,然后选择“创建”。
同样,必须对本地模板进行一些更改,确保其在模板规格下正常工作。 复制以下模板,并将其本地保存为 createTemplateSpec.json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/templateSpecs",
"apiVersion": "2022-02-01",
"name": "storageSpec",
"location": "westus2",
"properties": {
"displayName": "Storage template spec"
}
},
{
"type": "Microsoft.Resources/templateSpecs/versions",
"apiVersion": "2022-02-01",
"name": "[format('{0}/{1}', 'storageSpec', '2.0')]",
"location": "westus2",
"properties": {
"mainTemplate": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"defaultValue": "[format('store{0}', uniqueString(resourceGroup().id))]"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2025-01-01",
"name": "[[parameters('storageAccountName')]",
"location": "[[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"accessTier": "Hot"
}
}
]
}
},
"dependsOn": [
"storageSpec"
]
}
]
}
若要将新版本添加到模板规格,请使用 Azure CLI 或 PowerShell 部署模板。
New-AzResourceGroupDeployment `
-ResourceGroupName templateSpecRG `
-TemplateFile "C:\Templates\createTemplateSpec.json"
az deployment group create \
--resource-group templateSpecRG \
--template-file "C:\Templates\createTemplateSpec.json"
使用 Azure CLI 或 PowerShell 验证部署。
Get-AzTemplateSpec `
-ResourceGroupName templateSpecRG `
-Name storageSpec
az ts show \
--resource-group templateSpecRG \
--name storageSpec
应在版本列表中看到新版本 2.0
。
复制以下模板,并将其本地保存为 deployTemplateSpecV2.json:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2025-04-01",
"name": "demo",
"properties": {
"templateLink": {
"id": "[resourceId('templateSpecRG', 'Microsoft.Resources/templateSpecs/versions', 'storageSpec', '2.0')]"
},
"parameters": {
},
"mode": "Incremental"
}
}
]
}
使用 Azure CLI 或 PowerShell 部署模板。
New-AzResourceGroupDeployment `
-ResourceGroupName storageRG `
-TemplateFile "C:\Templates\deployTemplateSpecV2.json"
az deployment group create \
--resource-group storageRG \
--template-file "C:\Templates\deployTemplateSpecV2.json"
使用 Azure CLI 或 PowerShell 验证部署。
Get-AzResource `
-ResourceGroupName storageRG
az resource list \
--resource-group storageRG
您应该会看到一个新的存储帐户,其名称以 store
开头,并包含基于资源组 ID 的唯一字符串。
清理资源
若要清理本快速入门中部署的资源,请删除创建的两个资源组。
- 在 Azure 门户上的左侧菜单中选择“资源组”。
- 在“按名称筛选”字段中输入资源组名称(templateSpecRG 和 storageRG)。
- 选择资源组名称。
- 在顶部菜单中选择“删除资源组”。
后续步骤
若要了解如何创建包含链接模板的模板规格,请参阅如何 创建链接模板的模板规格。