ARM 模板错误 Bad JSON content found in the request

ARM template error Bad JSON content found in the request

我正在尝试使用 Azure DevOps 发布管道部署 ARM 模板。 Azure KeyVault 是模板中的资源之一。当我使用 Powershell 脚本时部署成功。但是,当使用 Azure DevOps Release 管道时,部署失败并出现错误“Bad JSON content found in the request”

Key Vault 资源定义如下。

{
  "type": "Microsoft.KeyVault/vaults",
  "apiVersion": "2018-02-14",
  "name": "[parameters('keyVaultName')]",
  "location": "[parameters('location')]",
  "tags": {
    "displayName": "KeyVault"
  },
  "properties": {
    "enabledForDeployment": "[parameters('enabledForDeployment')]",
    "enabledForTemplateDeployment": "[parameters('enabledForTemplateDeployment')]",
    "enabledForDiskEncryption": "[parameters('enabledForDiskEncryption')]",
    "tenantId": "[parameters('tenantId')]",
    "accessPolicies": [],
    "sku": {
      "name": "[parameters('skuName')]",
      "family": "A"
    }
  }
}

更新:我怀疑可能是因为租户id,硬编码了租户id来测试。但是还是不行。

根据日志,您正在任务中指定覆盖参数。这就是为什么您使用我提供的 ARM 模板,但仍然面临 Bad request 错误。因为在任务逻辑中,ARM文件中的脚本是API. And we use this API to create a resource you specified in azure. For detailed task logic described, you can refer my .

的request body

ARM模板中的参数定义是正确的,但是现在,由于指定的override参数导致的错误:

更具体地说,错误是因为您的参数覆盖定义中的 subscription().tenantId

你可以尝试使用Write-Host subscription().tenantId获取它的值并使用Azure powershell task打印出来。你会看到它无法得到任何东西。一句话,这个只能在Json文件中使用,不能在任务中使用。

所以现在,由于没有从该表达式中获取任何值,您还覆盖了 JSON 文件中定义的先前值。当任务要创建带有API.

的Azure资源时,它将缺少请求正文中的关键参数值(tenantId

有2个方案可以解决。

1.不要试图覆盖其值使用表达式的参数。

这里我指的是与Azure订阅相关的参数。大多数表达式无法在 Azure ARM 部署任务中编译。

2。如果你还想用任务中的特殊表达式覆盖这些特殊参数。

如果是这样,您必须先添加一个任务,才能从中获取 tenantId。然后将其传递到 ARM 部署任务中。

您可以使用以下示例脚本添加 Azure Powershell 任务:

Write-Output "Getting tenantId using Get-AzureRmSubscription..."

$subscription = (Get-AzureRmSubscription -SubscriptionId $azureSubscriptionId)

Write-Output "Requested subscription: $azureSubscriptionId"

$subscriptionId = $subscription.Id
$subscriptionName = $subscription.Name
$tenantId = $subscription.tenantId

Write-Output "Subscription Id: $subscriptionId"
Write-Output "Subscription Name: $subscriptionName"
Write-Output "Tenant Id: $tenantId"
Write-Host  "##vso[task.setvariable variable=TenantID;]$$tenantId"

那么在接下来的任务中,您可以使用$(TenantID)来获取它的值。

这里可以参考这两个优秀的博客:Blog1 and Blog2


我仍然建议您使用第一种解决方案,因为如果选择第二种解决方案,管道的体积会增加并变得复杂。