ARM 模板错误请求中发现错误的 JSON 内容 [英] ARM template error Bad JSON content found in the request

查看:25
本文介绍了ARM 模板错误请求中发现错误的 JSON 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Azure DevOps 发布管道部署 ARM 模板.Azure KeyVault 是模板中的资源之一.当我使用 Powershell 脚本时,部署成功.但是,当使用 Azure DevOps Release 管道时,部署失败并显示错误请求中发现错误的 JSON 内容"

Key Vault 资源定义如下.

<代码>{类型":Microsoft.KeyVault/Vaults",apiVersion":2018-02-14",名称":[参数('keyVaultName')]",位置":[参数('位置')]",标签":{显示名称":KeyVault"},属性":{enabledForDeployment":[参数('enabledForDeployment')]",enabledForTemplateDeployment":[参数('enabledForTemplateDeployment')]",enabledForDiskEncryption":[参数('enabledForDiskEncryption')]",tenantId":[参数('tenantId')]",访问策略":[],sku":{名称":[参数('skuName')]",家庭":A"}}}

更新:我怀疑这可能是因为租户 ID 并硬编码租户 ID 进行测试.但仍然没有运气.

解决方案

根据日志,您在任务中指定了覆盖参数.这就是为什么您使用我提供的 ARM 模板,但仍然面临 Bad request 错误的原因.因为在任务逻辑中,ARM文件中的脚本是

更具体地说,错误是由于参数覆盖定义中的 subscription().tenantId 造成的.

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

所以现在,由于没有从这个表达式中得到值,你也覆盖了之前在 JSON 文件中定义的值.当任务要使用 API 创建 azure 资源时,它会缺少请求正文中的关键参数值(tenantId).

<小时>

有两种解决方案可以解决它.

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

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

2.如果您仍想使用任务中的特殊表达式覆盖这些特殊参数.

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

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

Write-Output 使用 Get-AzureRmSubscription 获取租户 ID..."$subscription = (Get-AzureRmSubscription -SubscriptionId $azureSubscriptionId)写入输出请求的订阅:$azureSubscriptionId"$subscriptionId = $subscription.Id$subscriptionName = $subscription.Name$tenantId = $subscription.tenantId写入输出订阅 ID:$subscriptionId"写输出订阅名称:$subscriptionName"写输出租户 ID:$tenantId"写主机##vso[task.setvariable variable=TenantID;]$$tenantId"

然后在下一个任务中,您可以使用 $(TenantID) 获取其值.

这里可以参考这两个优秀的博客:Blog1Blog2

<小时>

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

I am trying to deploy an ARM template using the Azure DevOps release pipeline. Azure KeyVault is one of the resources in the template. the deployment is successful when I use the Powershell script. however, when Azure DevOps Release pipeline is used, deployment fails with error "Bad JSON content found in the request"

The key vault resource definition is as below.

{
  "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"
    }
  }
}

Update: I suspected that it could be because of tenant id and hardcoded the tenant id to test. But still no luck.

解决方案

According to the log, you are specifying the override parameters in the task. That's why you are using the ARM template I provided, but still facing the Bad request error. Because in the task logic, the script which in ARM files is the request body of the API. And we use this API to create a resource you specified in azure. For detailed task logic described, you can refer my previous answer.

The parameter definition in the ARM template is correct, but now, the error caused by the override parameters specified:

More specifically, the error is because of the subscription().tenantId in your parameter override definition.

You can try to use Write-Host subscription().tenantId to get its value and print out by using Azure powershell task. You will see that it could not get any thing. One word, this can only used in Json file instead of used in task.

So now, because of no value get from this expression, also you have override the previous value which defined in the JSON file. It will lack the key parameter value(tenantId) in the request body when the task is going to create a azure resource with API.


There has 2 solution can solve it.

1. Do not try to override the parameters which its value is using expression.

Here I just mean the parameter that relevant with Azure subscription. Most of the expression could not be compiled in the Azure ARM deploy task.

2. If you still want to override these special parameters with the special expression in the task.

If this, you must add one task firstly, to get the tenantId from that. Then pass it into the ARM deploy task.

You can add Azure Powershell task by using the following sample script:

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"

Then in the next task, you can use $(TenantID) to get its value.

Here you can refer to this two excellent blog: Blog1 and Blog2


I still recommend you to use the first solution since the volume of the pipeline will increase and complicate if choosing the second solution.

这篇关于ARM 模板错误请求中发现错误的 JSON 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆