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

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

问题描述

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

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

您可以尝试使用 Write-Host subscription().tenantId 来获取其值,并通过使用 Azure powershell任务打印出来.您将看到它什么也没得到.一个字,只能在 Json 文件中使用,而不能在task中使用.

现在,由于此表达式中没有值,因此您也覆盖了JSON文件中定义的先前值.当任务将要使用API​​创建天蓝色资源时,它将缺少请求主体中的关键参数值( tenantId ).


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

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)来获取其值.

在这里您可以参考这两个出色的博客:

https://i.stack.imgur.com/o9sbD.png

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天全站免登陆