本快速入门介绍如何将 Azure 资源管理器模板(ARM 模板)打包为模板规格。然后部署该模板规格。模板规格包含用于部署存储帐户的 ARM 模板。This quickstart shows you how to package an Azure Resource Manager template (ARM template) into a template spec. Then, you deploy that template spec. Your template spec contains an ARM template that deploys a storage account.
可以根据本地模板创建模板规格。You create a template spec from a local template.复制以下模板并将其本地保存到名为 azuredeploy.json 的文件中。Copy the following template and save it locally to a file named azuredeploy.json.本快速入门假设你已将模板保存到路径 c:\Templates\azuredeploy.json,不过你可以使用任何路径。This quickstart assumes you've saved to a path c:\Templates\azuredeploy.json but you can use any path.
模板规格是名为 Microsoft.Resources/templateSpecs 的资源类型。The template spec is a resource type named Microsoft.Resources/templateSpecs.若要创建模板规格,可以使用 PowerShell、Azure CLI、门户或 ARM 模板。To create a template spec, use PowerShell, Azure CLI, the portal, or an ARM template.
建议不使用 ARM 模板,而是使用 PowerShell 或 CLI 来创建模板规格。这些工具会自动将链接的模板转换为连接到主模板的项目。Instead of using an ARM template, we recommend that you use PowerShell or CLI to create your template spec. Those tools automatically convert linked templates to artifacts connected to your main template.使用 ARM 模板创建模板规格时,必须手动将这些链接的模板添加为项目,这可能会很复杂。When you use an ARM template to create the template spec, you must manually add those linked templates as artifacts, which can be complicated.
使用 ARM 模板创建模板规格时,该模板将嵌入资源定义。When you use an ARM template to create the template spec, the template is embedded in the resource definition.你需对本地模板进行一些更改。There are some changes you need to make to your local template.复制以下模板并将其本地保存为 azuredeploy.json。Copy the following template and save it locally as azuredeploy.json.
备注
在嵌入的模板中,必须使用第二个左括号对所有模板表达式进行转义。In the embedded template, all template expressions must be escaped with a second left bracket.请使用 "[[,而不是 "[。Use "[[ instead of "[.JSON 数组仍使用单个左括号。JSON arrays still use a single left bracket.
az deployment group create \
--name templateSpecRG \
--template-file "c:\Templates\azuredeploy.json"
部署模板规格Deploy template spec
若要部署模板规格,请使用部署模板所用的部署命令。To deploy a template spec, use the same deployment commands as you would use to deploy a template.传入模板规格的资源 ID 以进行部署。Pass in the resource ID of the template spec to deploy.
提供的参数与 ARM 模板的完全一样。You provide parameters exactly as you would for an ARM template.使用存储帐户类型的参数重新部署模板规格。Redeploy the template spec with a parameter for the storage account type.
获取模板规格 ID 并将其分配到 Windows PowerShell 中的变量时存在一个已知问题。There is a known issue with getting a template spec ID and assigning it to a variable in Windows PowerShell.
部署模板规格。Deploy the template spec.
az deployment group create \
--resource-group storageRG \
--template-spec $id
提供的参数与 ARM 模板的完全一样。You provide parameters exactly as you would for an ARM template.使用存储帐户类型的参数重新部署模板规格。Redeploy the template spec with a parameter for the storage account type.
az deployment group create \
--resource-group storageRG \
--template-spec $id \
--parameters storageAccountType='Standard_GRS'
复制以下模板并将其本地保存到名为 storage.json 的文件中。Copy the following template and save it locally to a file named storage.json.
az deployment group create \
--name storageRG \
--template-file "c:\Templates\storage.json"
授予访问权限Grant access
如果要让组织中的其他用户部署模板规格,你需要向他们授予读取权限。If you want to let other users in your organization deploy your template spec, you need to grant them read access.对于包含要共享的模板规格的资源组,你可以将读取者角色分配给相应的 Azure AD 组。You can assign the Reader role to an Azure AD group for the resource group that contains template specs you want to share.有关详细信息,请参阅教程:使用 Azure PowerShell 授予组对 Azure 资源的访问权限。For more information, see Tutorial: Grant a group access to Azure resources using Azure PowerShell.
更新模板Update template
假设你已在模板规格中标识了要对模板进行的更改。以下模板类似于之前的模板,不同之处在于其存储帐户名称增加了前缀。Let's suppose you've identified a change you want to make to the template in your template spec. The following template is similar to your earlier template except it adds a prefix for the storage account name.复制以下模板并更新 azuredeploy.json 文件。Copy the following template and update your azuredeploy.json file.
在现有模板规格中添加名为 2.0 的新版本,而不是为修改后的模板创建新的模板规格。用户可以选择任一版本进行部署。Rather than creating a new template spec for the revised template, add a new version named 2.0 to the existing template spec. Users can choose either version to deploy.
部署该版本。Deploy that version.为存储帐户名称提供一个前缀。Provide a prefix for the storage account name.
az deployment group create \
--resource-group storageRG \
--template-spec $id \
--parameters namePrefix='demoaccount'
同样,必须对本地模板进行一些更改,确保其在模板规格下正常工作。Again, you must make some changes to your local template to make it work with template specs.复制以下模板并将其本地保存为 azuredeploy.json。Copy the following template and save it locally as azuredeploy.json.