您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 Resource Manager 模板和 Azure CLI 部署资源Deploy resources with Resource Manager templates and Azure CLI

本文介绍如何将 Azure CLI 与资源管理器模板配合使用将资源部署到 Azure。This article explains how to use Azure CLI with Resource Manager templates to deploy your resources to Azure. 如果不熟悉部署和管理 Azure 解决方案的概念,请参阅 Azure 资源管理器概述If you aren't familiar with the concepts of deploying and managing your Azure solutions, see Azure Resource Manager overview.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有安装 Azure CLI,则可使用 Cloud ShellIf you don't have Azure CLI installed, you can use the Cloud Shell.

部署范围Deployment scope

可将部署目标设定为 Azure 订阅或订阅中的资源组。You can target your deployment to either an Azure subscription or a resource group within a subscription. 大多数情况下,我们会将以资源组指定为部署目标。In most cases, you'll target deployment to a resource group. 可以使用订阅部署在整个订阅中应用策略和角色分配。Use subscription deployments to apply policies and role assignments across the subscription. 还可以使用订阅部署创建资源组并向其部署资源。You also use subscription deployments to create a resource group and deploy resources to it. 你将根据部署范围使用不同的命令。Depending on the scope of the deployment, you use different commands.

若要部署到资源组,请使用 az group deployment createTo deploy to a resource group, use az group deployment create:

az group deployment create --resource-group <resource-group-name> --template-file <path-to-template>

若要部署到订阅,请使用 az deployment createTo deploy to a subscription, use az deployment create:

az deployment create --location <location> --template-file <path-to-template>

有关订阅级别部署的详细信息,请参阅在订阅级别创建资源组和资源For more information about subscription level deployments, see Create resource groups and resources at the subscription level.

目前,仅通过 REST API 支持管理组部署。Currently, management group deployments are only supported through the REST API. 有关管理组级别部署的详细信息,请参阅在管理组级别创建资源For more information about management group level deployments, see Create resources at the management group level.

本文中的示例使用资源组部署。The examples in this article use resource group deployments.

部署本地模板Deploy local template

将资源部署到 Azure 时,执行以下操作:When deploying resources to Azure, you:

  1. 登录到 Azure 帐户Sign in to your Azure account
  2. 创建用作已部署资源的容器的资源组。Create a resource group that serves as the container for the deployed resources. 资源组名称只能包含字母数字字符、句点、下划线、连字符和括号。The name of the resource group can only include alphanumeric characters, periods, underscores, hyphens, and parenthesis. 它最多可以包含 90 个字符。It can be up to 90 characters. 它不能以句点结尾。It can't end in a period.
  3. 将定义了要创建的资源的模板部署到资源组Deploy to the resource group the template that defines the resources to create

模板可以包括可用于自定义部署的参数。A template can include parameters that enable you to customize the deployment. 例如,可以提供为特定环境(如开发环境、测试环境和生产环境)定制的值。For example, you can provide values that are tailored for a particular environment (such as dev, test, and production). 示例模板定义了存储帐户 SKU 的参数。The sample template defines a parameter for the storage account SKU.

以下示例将创建一个资源组,并从本地计算机部署模板:The following example creates a resource group, and deploys a template from your local machine:

az group create --name ExampleGroup --location "Central US"
az group deployment create \
  --name ExampleDeployment \
  --resource-group ExampleGroup \
  --template-file storage.json \
  --parameters storageAccountType=Standard_GRS

部署可能需要几分钟才能完成。The deployment can take a few minutes to complete. 完成之后,会看到一条包含以下结果的消息:When it finishes, you see a message that includes the result:

"provisioningState": "Succeeded",

部署远程模板Deploy remote template

可能更愿意将 Resource Manager 模板存储在外部位置,而不是将它们存储在本地计算机上。Instead of storing Resource Manager templates on your local machine, you may prefer to store them in an external location. 可以将模板存储在源控件存储库(例如 GitHub)中。You can store templates in a source control repository (such as GitHub). 另外,还可以将其存储在 Azure 存储帐户中,以便在组织中共享访问。Or, you can store them in an Azure storage account for shared access in your organization.

若要部署外部模板,请使用 template-uri 参数。To deploy an external template, use the template-uri parameter. 使用示例中的 URI 从 GitHub 部署示例模板。Use the URI in the example to deploy the sample template from GitHub.

az group create --name ExampleGroup --location "Central US"
az group deployment create \
  --name ExampleDeployment \
  --resource-group ExampleGroup \
  --template-uri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json" \
  --parameters storageAccountType=Standard_GRS

前面的示例要求模板的 URI 可公开访问,它适用于大多数情况,因为模板应该不会包含敏感数据。The preceding example requires a publicly accessible URI for the template, which works for most scenarios because your template shouldn't include sensitive data. 如果需要指定敏感数据(如管理员密码),请以安全参数的形式传递该值。If you need to specify sensitive data (like an admin password), pass that value as a secure parameter. 但是,如果不希望模板可公开访问,可以通过将其存储在专用存储容器中来保护它。However, if you don't want your template to be publicly accessible, you can protect it by storing it in a private storage container. 有关部署需要共享访问签名 (SAS) 令牌的模板的信息,请参阅部署具有 SAS 令牌的专用模板For information about deploying a template that requires a shared access signature (SAS) token, see Deploy private template with SAS token.

从 Cloud Shell 部署模板Deploy template from Cloud Shell

可以使用 Cloud Shell 来部署模板。You can use Cloud Shell to deploy your template. 要部署外部模板,请提供与用于任何外部部署的完全相同的模板 URI。To deploy an external template, provide the URI of the template exactly as you would for any external deployment. 要部署本地模板,必须先将模板加载到 Cloud Shell 的存储帐户。To deploy a local template, you must first load your template into the storage account for your Cloud Shell. 本节介绍如何将模板加载到 Cloud Shell 帐户,并将其部署为本地文件。This section describes how to load the template to your cloud shell account, and deploy it as a local file. 如果尚未使用过 Cloud Shell,请参阅 Azure Cloud Shell 概述,了解如何设置它。If you haven't used Cloud Shell, see Overview of Azure Cloud Shell for information about setting it up.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 选择 Cloud Shell 资源组。Select your Cloud Shell resource group. 名称模式为 cloud-shell-storage-<region>The name pattern is cloud-shell-storage-<region>.


  3. 选择适用于 Cloud Shell 的存储帐户。Select the storage account for your Cloud Shell.


  4. 选择“Blob”。Select Blobs.

    选择 Blob

  5. 选择“+ 容器”。Select + Container.


  6. 为容器指定名称和访问级别。Give your container a name and an access level. 本文中的示例模板未包含任何敏感信息,因此允许进行匿名读取访问。The sample template in this article contains no sensitive information, so allow anonymous read access. 选择“确定”。Select OK.


  7. 选择你创建的容器。Select the container you created.


  8. 选择“上传”。Select Upload.

    上传 blob

  9. 找到并上传模板。Find and upload your template.


  10. 在它上传后,选择模板。After it has uploaded, select the template.


  11. 复制 URL。Copy the URL.

    复制 URL

  12. 打开提示符。Open the prompt.

    打开 Cloud Shell

在 Cloud Shell 中使用以下命令:In the Cloud Shell, use the following commands:

az group create --name examplegroup --location "South Central US"
az group deployment create --resource-group examplegroup \
  --template-uri <copied URL> \
  --parameters storageAccountType=Standard_GRS


若要传递参数值,可以使用内联参数或参数文件。To pass parameter values, you can use either inline parameters or a parameter file.

内联参数Inline parameters

若要传递内联参数,请在 parameters 中提供值。To pass inline parameters, provide the values in parameters. 例如,若要在 Bash shell 中将字符串和数组传递给模板,请使用:For example, to pass a string and array to a template is a Bash shell, use:

az group deployment create \
  --resource-group testgroup \
  --template-file demotemplate.json \
  --parameters exampleString='inline string' exampleArray='("value1", "value2")'

如果要将 Azure CLI 与 Windows 命令提示符 (CMD) 或 PowerShell 配合使用,请以以下格式传递数组:exampleArray="['value1','value2']"If you're using Azure CLI with Windows Command Prompt (CMD) or PowerShell, pass the array in the format: exampleArray="['value1','value2']".

还可以获取文件的内容并将该内容作为内联参数提供。You can also get the contents of file and provide that content as an inline parameter.

az group deployment create \
  --resource-group testgroup \
  --template-file demotemplate.json \
  --parameters exampleString=@stringContent.txt exampleArray=@arrayContent.json

当需要提供配置值时,从文件中获取参数值非常有用。Getting a parameter value from a file is helpful when you need to provide configuration values. 例如,可以为 Linux 虚拟机提供 cloud-init 值For example, you can provide cloud-init values for a Linux virtual machine.

arrayContent.json 格式为:The arrayContent.json format is:


参数文件Parameter files

与在脚本中以内联值的形式传递参数相比,可能会发现使用包含参数值的 JSON 文件更为容易。Rather than passing parameters as inline values in your script, you may find it easier to use a JSON file that contains the parameter values. 参数文件必须是本地文件。The parameter file must be a local file. Azure CLI 不支持外部参数文件。External parameter files aren't supported with Azure CLI.

有关参数文件的详细信息,请参阅创建资源管理器参数文件For more information about the parameter file, see Create Resource Manager parameter file.

若要传递本地参数文件,请使用 @ 指定名为 storage.parameters.json 的本地文件。To pass a local parameter file, use @ to specify a local file named storage.parameters.json.

az group deployment create \
  --name ExampleDeployment \
  --resource-group ExampleGroup \
  --template-file storage.json \
  --parameters @storage.parameters.json

处理扩展 JSON 格式Handle extended JSON format

若要部署包含多行字符串或注释的模板,必须使用 --handle-extended-json-format 开关。To deploy a template with multi-line strings or comments, you must use the --handle-extended-json-format switch. 例如:For example:

  "type": "Microsoft.Compute/virtualMachines",
  "name": "[variables('vmName')]", // to customize name, change it in variables
  "location": "[
    ]", //defaults to resource group location
  "apiVersion": "2018-10-01",
    storage account and network interface
    must be deployed first
  "dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
    "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"

测试模板部署Test a template deployment

若要测试模板和参数值而不实际部署任何资源,请使用 az group deployment validateTo test your template and parameter values without actually deploying any resources, use az group deployment validate.

az group deployment validate \
  --resource-group ExampleGroup \
  --template-file storage.json \
  --parameters @storage.parameters.json

如果未检测到错误,则该命令将返回有关测试部署的信息。If no errors are detected, the command returns information about the test deployment. 需要特别注意的是,error 值为 null。In particular, notice that the error value is null.

  "error": null,
  "properties": {

如果检测到错误,则该命令将返回一条错误消息。If an error is detected, the command returns an error message. 例如,如果为存储帐户 SKU 传递不正确的值,将返回以下错误:For example, passing an incorrect value for the storage account SKU, returns the following error:

  "error": {
    "code": "InvalidTemplate",
    "details": null,
    "message": "Deployment template validation failed: 'The provided value 'badSKU' for the template parameter
      'storageAccountType' at line '13' and column '20' is not valid. The parameter value is not part of the allowed
      value(s): 'Standard_LRS,Standard_ZRS,Standard_GRS,Standard_RAGRS,Premium_LRS'.'.",
    "target": null
  "properties": null

如果模板有语法错误,该命令将返回一个错误,指示它无法分析该模板。If your template has a syntax error, the command returns an error indicating it couldn't parse the template. 该消息会指出分析错误的行号和位置。The message indicates the line number and position of the parsing error.

  "error": {
    "code": "InvalidTemplate",
    "details": null,
    "message": "Deployment template parse failed: 'After parsing a value an unexpected character was encountered:
      \". Path 'variables', line 31, position 3.'.",
    "target": null
  "properties": null

后续步骤Next steps