您现在访问的是微软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.

提示

有关在部署过程中调试错误的帮助,请参阅:For help with debugging an error during deployment, see:

模板可以是本地文件或是可通过 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.

使用 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
    {
     "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
    {
     "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
    

部署失败时,重新部署Redeploy when deployment fails

对于失败的部署,可以指定从部署历史记录自动重新部署以前的部署。For deployments that fail, you can specify that an earlier deployment from your deployment history is automatically redeployed. 若要使用此选项,部署必须具有唯一的名称,以便可以在历史记录中标识它们。To use this option, your deployments must have unique names so they can be identified in the history. 如果没有唯一名称,则当前失败的部署可能会覆盖历史记录中以前成功的部署。If you don't have unique names, the current failed deployment might overwrite the previously successful deployment in the history. 只能将此选项用于根级别部署。You can only use this option with root level deployments. 从嵌套模板进行的部署不可用于重新部署。Deployments from a nested template aren't available for redeployment.

若要在当前部署失败的情况下,重新部署上一个成功部署,请使用:To redeploy the last successful deployment if the current deployment fails, use:

"onErrorDeployment": {
  "type": "LastSuccessful",
},

若要在当前部署失败的情况下,重新部署特定部署,请使用:To redeploy a specific deployment if the current deployment fails, use:

"onErrorDeployment": {
  "type": "SpecificDeployment",
  "deploymentName": "<deploymentname>"
}

指定的部署必须已成功。The specified deployment must have succeeded.

参数文件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 can't 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