使用 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.

可以在请求正文中包含模板或链接到文件。You can either include your template in the request body or link to a file. 使用文件时,它可以是本地文件,也可以是通过 URI 提供的外部文件。When using a file, it can be a local file or an external file that is available through a URI. 如果模板位于存储帐户中,可以限制对该模板的访问,并在部署过程中提供共享访问签名 (SAS) 令牌。When your template is in a storage account, you can restrict access to the template and provide a shared access signature (SAS) token during deployment.

部署范围Deployment scope

可将部署目标设定为管理组、Azure 订阅或资源组。You can target your deployment to a management group, an Azure subscription, or a resource group. 大多数情况下,我们会将部署目标设定为资源组。In most cases, you'll target deployments to a resource group. 使用管理组或订阅部署在指定范围内应用策略和角色分配。Use management group or subscription deployments to apply policies and role assignments across the specified scope. 还可以使用订阅部署创建资源组并向其部署资源。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.

若要部署到资源组,请使用“部署 - 创建”。To deploy to a resource group, use Deployments - Create. 请求将发送到:The request is sent to:

PUT https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Resources/deployments/{deploymentName}?api-version=2019-05-01

若要部署到订阅,请使用“部署 - 在订阅范围创建”。To deploy to a subscription, use Deployments - Create At Subscription Scope. 请求将发送到:The request is sent to:

PUT https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/providers/Microsoft.Resources/deployments/{deploymentName}?api-version=2019-05-01

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

若要部署到管理组,请使用部署 - 在管理组范围内创建To deploy to a management group, use Deployments - Create At Management Group Scope. 请求将发送到:The request is sent to:

PUT https://management.chinacloudapi.cn/providers/Microsoft.Management/managementGroups/{groupId}/providers/Microsoft.Resources/deployments/{deploymentName}?api-version=2019-05-01

有关管理组级部署的详细信息,请参阅在管理组级别创建资源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.

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

  1. 设置 常见参数和标头,包括身份验证令牌。Set common parameters and headers, including authentication tokens.

  2. 如果目前没有资源组,请创建资源组。If you don't 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.chinacloudapi.cn/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>?api-version=2019-05-01
    

    使用如下所示请求正文:With a request body like:

    {
    "location": "China North",
    "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. 若要部署模板,请在请求 URI 中提供订阅 ID、资源组名称和部署名称。To deploy a template, provide your subscription ID, the name of the resource group, the name of the deployment in the request URI.

    PUT https://management.chinacloudapi.cn/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>/providers/Microsoft.Resources/deployments/<YourDeploymentName>?api-version=2019-05-01
    

    在请求正文中,提供指向模板和参数文件的链接。In the request body, provide a link to your template and parameter file. 有关参数文件的详细信息,请参阅创建资源管理器参数文件For more information about the parameter file, see Create Resource Manager parameter file.

    请注意,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 aren't in your template.

    {
    "properties": {
      "templateLink": {
        "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/template.json",
        "contentVersion": "1.0.0.0"
      },
      "parametersLink": {
        "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/parameters.json",
        "contentVersion": "1.0.0.0"
      },
      "mode": "Incremental"
    }
    }
    

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

    {
    "properties": {
      "templateLink": {
        "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/template.json",
        "contentVersion": "1.0.0.0"
      },
      "parametersLink": {
        "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/parameters.json",
        "contentVersion": "1.0.0.0"
      },
      "mode": "Incremental",
      "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.

    如果需要为参数(如密码)提供敏感值,请将该值添加到密钥保管库。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.

  5. 可以将模板和参数包含在请求正文中,而不是链接到模板和参数的文件。Instead of linking to files for the template and parameters, you can include them in the request body. 以下示例显示了内联模板和参数的请求正文:The following example shows the request body with the template and parameter inline:

    {
      "properties": {
      "mode": "Incremental",
      "template": {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-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(uniquestring(resourceGroup().id), 'standardsa')]"
        },
        "resources": [
          {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2018-02-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')]"
          }
        }
      },
      "parameters": {
        "location": {
          "value": "chinaeast2"
        }
      }
     }
    }
    
  6. 若要获取模板部署的状态,请使用部署 - 获取To get the status of the template deployment, use Deployments - Get.

    GET https://management.chinacloudapi.cn/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>/providers/Microsoft.Resources/deployments/<YourDeploymentName>?api-version=2018-05-01
    

后续步骤Next steps