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

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

本文介绍如何将 Resource Manager REST API 与 Resource Manager 模板配合使用向 Azure 部署资源。This article explains how to use the Resource Manager REST API with Resource Manager templates to deploy your resources to Azure.

提示

有关在部署过程中调试错误的帮助,请参阅:

模板可以是本地文件或是可通过 URI 访问的外部文件。Your template can be either a local file or an external file that is available through a URI. 如果模板驻留在存储帐户中,可以限制对该模板的访问,并在部署过程中提供共享访问签名 (SAS) 令牌。When your template resides in a storage account, you can restrict access to the template and provide a shared access signature (SAS) token during deployment.

增量部署和完整部署Incremental and complete deployments

部署资源时,可以指定部署为增量更新还是完整更新。When deploying your resources, you specify that the deployment is either an incremental update or a complete update. 这两种模式的主要区别是 Resource Manager 如何处理资源组中不在模板中的现有资源:The primary difference between these two modes is how Resource Manager handles existing resources in the resource group that are not in the template:

  • 在完整模式中,Resource Manager 删除资源组中已存在但尚未在模板中指定的资源。In complete mode, Resource Manager deletes resources that exist in the resource group but are not specified in the template.
  • 在增量模式中,Resource Manager 保留资源组中已存在但尚未在模板中指定的资源。In incremental mode, Resource Manager leaves unchanged resources that exist in the resource group but are not specified in the template.

对于这两种模式,Resource Manager 都会尝试预配在模板中指定的所有资源。For both modes, Resource Manager attempts to provision all resources specified in the template. 如果资源已存在于资源组中且其设置未更改,该操作不会导致任何更改。If the resource already exists in the resource group and its settings are unchanged, the operation results in no change. 如果更改某个资源的设置,则会使用这些新设置预配资源。If you change the settings for a resource, the resource is provisioned with those new settings. 如果尝试更新现有资源的位置或类型,则部署会失败并出现错误。If you attempt to update the location or type of an existing resource, the deployment fails with an error. 请改用所需的位置或类型部署新资源。Instead, deploy a new resource with the location or type that you need.

Resource Manager 默认使用增量模式。By default, Resource Manager uses the incremental mode.

为了说明增量模式和完整模式的差异,请考虑以下方案。To illustrate the difference between incremental and complete modes, consider the following scenario.

现有资源组包含:Existing Resource Group contains:

  • 资源 AResource A
  • 资源 BResource B
  • 资源 CResource C

模板定义:Template defines:

  • 资源 AResource A
  • 资源 BResource B
  • 资源 DResource D

在“增量”模式下部署时,资源组包含:When deployed in incremental mode, the resource group contains:

  • 资源 AResource A
  • 资源 BResource B
  • 资源 CResource C
  • 资源 DResource D

在“完整”模式下部署时,会删除资源 C。When deployed in complete mode, Resource C is deleted. 该资源组包含:The resource group contains:

  • 资源 AResource A
  • 资源 BResource B
  • 资源 DResource D

使用 REST API 进行部署Deploy with the REST API

  1. 设置常见参数和标头,包括身份验证令牌。Set common parameters and headers, including authentication tokens.
  2. 如果目前没有资源组,请创建资源组。If you do not have an existing resource group, create a resource group. 提供订阅 ID、新资源组的名称,以及解决方案所需的位置。Provide your subscription ID, the name of the new resource group, and location that you need for your solution. 有关详细信息,请参阅创建资源组For more information, see Create a resource group.

     PUT https://management.azure.com/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>?api-version=2015-01-01
       <common headers>
       {
         "location": "West US",
         "tags": {
            "tagname1": "tagvalue1"
         }
       }
    
  3. 在执行部署之前,通过运行验证模板部署操作来验证部署。Validate your deployment before executing it by running the Validate a template deployment operation. 测试部署时,请提供与执行部署时所提供的完全相同的参数(如下一步中所示)。When testing the deployment, provide parameters exactly as you would when executing the deployment (shown in the next step).
  4. 创建部署。Create a deployment. 提供订阅 ID、资源组的名称、部署的名称以及模板的链接。Provide your subscription ID, the name of the resource group, the name of the deployment, and a link to your template. 有关模板文件的信息,请参阅参数文件For information about the template file, see Parameter file. 有关使用 REST API 创建资源组的详细信息,请参阅创建模板部署For more information about the REST API to create a resource group, see Create a template deployment. 请注意,mode 设置为 IncrementalNotice the mode is set to Incremental. 要运行完整部署,请将 mode 设置为 CompleteTo run a complete deployment, set mode to Complete. 使用完整模式时要小心,因为可能会无意中删除不在模板中的资源。Be careful when using the complete mode as you can inadvertently delete resources that are not in your template.

     PUT https://management.azure.com/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>/providers/Microsoft.Resources/deployments/<YourDeploymentName>?api-version=2015-01-01
       <common headers>
       {
         "properties": {
           "templateLink": {
             "uri": "http://mystorageaccount.blob.core.windows.net/templates/template.json",
             "contentVersion": "1.0.0.0"
           },
           "mode": "Incremental",
           "parametersLink": {
             "uri": "http://mystorageaccount.blob.core.windows.net/templates/parameters.json",
             "contentVersion": "1.0.0.0"
           }
         }
       }
    

    如果想要记录响应内容或/和请求内容,请在请求中包括 debugSettingIf you want to log response content, request content, or both, include debugSetting in the request.

     "debugSetting": {
       "detailLevel": "requestContent, responseContent"
     }
    

    可以将存储帐户设置为使用共享访问签名 (SAS) 令牌。You can set up your storage account to use a shared access signature (SAS) token. 有关详细信息,请参阅使用共享访问签名委托访问权限For more information, see Delegating Access with a Shared Access Signature.

  5. 获取模板部署的状态。Get the status of the template deployment. 有关详细信息,请参阅获取有关模板部署的信息For more information, see Get information about a template deployment.

       GET https://management.azure.com/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>/providers/Microsoft.Resources/deployments/<YourDeploymentName>?api-version=2015-01-01
        <common headers>
    

参数文件Parameter file

如果要在部署期间使用参数文件传递参数值,需要使用类似于以下示例的格式创建一个 JSON 文件:If you use a parameter file to pass parameter values during deployment, you need to create a JSON file with a format similar to the following example:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "webSiteName": {
            "value": "ExampleSite"
        },
        "webSiteHostingPlanName": {
            "value": "DefaultPlan"
        },
        "webSiteLocation": {
            "value": "West US"
        },
        "adminPassword": {
            "reference": {
               "keyVault": {
                  "id": "/subscriptions/{guid}/resourceGroups/{group-name}/providers/Microsoft.KeyVault/vaults/{vault-name}"
               }, 
               "secretName": "sqlAdminPassword" 
            }   
        }
   }
}

参数文件的大小不能超过 64 KB。The size of the parameter file cannot be more than 64 KB.

如果需要为参数(如密码)提供敏感值,请将该值添加到密钥保管库。If you need to provide a sensitive value for a parameter (such as a password), add that value to a key vault. 在部署过程中检索密钥保管库,如前面的示例所示。Retrieve the key vault during deployment as shown in the previous example. 有关详细信息,请参阅在部署期间传递安全值For more information, see Pass secure values during deployment.

后续步骤Next steps