快速入门:创建和部署模板规格(预览)Quickstart: Create and deploy template spec (Preview)

本快速入门介绍如何将 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.

先决条件Prerequisites

具有活动订阅的 Azure 帐户。An Azure account with an active subscription. 创建试用版订阅Create a trial subscription.

备注

模板规格当前提供预览版。Template Specs is currently in preview. 若要将其与 Azure PowerShell 一起使用,必须安装版本 5.0.0 或更高版本To use it with Azure PowerShell, you must install version 5.0.0 or later. 若要将其与 Azure CLI 一起使用,请使用版本 2.14.2 或更高版本To use it with Azure CLI, use version 2.14.2 or later.

创建模板Create template

可以根据本地模板创建模板规格。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.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    
    "storageAccountName": "[concat('store', uniquestring(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountEndPoint": "https://core.chinacloudapi.cn/",
    "storageAccountName": {
      "type": "string",
      "value": "[variables('storageAccountName')]"
    }
  }
}

创建模板规格Create template spec

模板规格是名为 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.

  1. 创建新的资源组以包含模板规格。Create a new resource group to contain the template spec.

    New-AzResourceGroup `
      -Name templateSpecRG `
      -Location chinanorth2
    
  2. 在该资源组中创建模板规格。Create the template spec in that resource group. 将新的模板规格命名为 storageSpec。Give the new template spec the name storageSpec.

    New-AzTemplateSpec `
      -Name storageSpec `
      -Version "1.0" `
      -ResourceGroupName templateSpecRG `
      -Location chinanorth2 `
      -TemplateFile "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.

  1. 创建资源组以包含新的存储帐户。Create a resource group to contain the new storage account.

    New-AzResourceGroup `
      -Name storageRG `
      -Location chinanorth2
    
  2. 获取模板规格的资源 ID。Get the resource ID of the template spec.

    $id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "1.0").Versions.Id
    
  3. 部署模板规格。Deploy the template spec.

    New-AzResourceGroupDeployment `
      -TemplateSpecId $id `
      -ResourceGroupName storageRG
    
  4. 提供的参数与 ARM 模板的完全一样。You provide parameters exactly as you would for an ARM template. 使用存储帐户类型的参数重新部署模板规格。Redeploy the template spec with a parameter for the storage account type.

    New-AzResourceGroupDeployment `
      -TemplateSpecId $id `
      -ResourceGroupName storageRG `
      -storageAccountType Standard_GRS
    

授予访问权限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.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "namePrefix": {
        "type": "string",
        "maxLength": 11,
        "defaultValue": "store",
        "metadata": {
            "description": "Prefix for storage account name"
        }
    }
  },
  "variables": {
    "storageAccountEndPoint": "https://core.chinacloudapi.cn/",
    "storageAccountName": "[concat(parameters('namePrefix'), uniquestring(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountEndPoint": "https://core.chinacloudapi.cn/",
    "storageAccountName": {
      "type": "string",
      "value": "[variables('storageAccountName')]"
    }
  }
}

更新模板规格版本Update template spec version

在现有模板规格中添加名为 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.

  1. 创建新的模板规格版本。Create a new version for the template spec.

    New-AzTemplateSpec `
     -Name storageSpec `
     -Version "2.0" `
     -ResourceGroupName templateSpecRG `
     -Location chinanorth2 `
     -TemplateFile "c:\Templates\azuredeploy.json"
    
  2. 若要部署新版本,请获取 2.0 版本的资源 ID。To deploy the new version, get the resource ID for the 2.0 version.

    $id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name storageSpec -Version "2.0").Versions.Id
    
  3. 部署该版本。Deploy that version. 为存储帐户名称提供一个前缀。Provide a prefix for the storage account name.

    New-AzResourceGroupDeployment `
     -TemplateSpecId $id `
     -ResourceGroupName storageRG `
     -namePrefix "demoaccount"
    

清理资源Clean up resources

若要清理本快速入门中部署的资源,请删除创建的两个资源组。To clean up the resource you deployed in this quickstart, delete both resource groups that you created.

  1. 在 Azure 门户上的左侧菜单中选择“资源组”。From the Azure portal, select Resource group from the left menu.

  2. 在“按名称筛选”字段中输入资源组名称(templateSpecRG 和 storageRG)。Enter the resource group name (templateSpecRG and storageRG) in the Filter by name field.

  3. 选择资源组名称。Select the resource group name.

  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.

后续步骤Next steps

若要了解有关创建包含关联模板的模板规格的信息,请参阅创建关联模板的模板规格To learn about creating a template spec that includes linked templates, see Create a template spec of a linked template.