来自bash脚本的ARM.Template。未终止的字符串。预期分隔符:

时间:2018-05-30 12:45:11

标签: json linux bash shell

我正在编写一个bash脚本,用于使用“armclient”将证书从linux-server上传到azure keyvault

我按照本指南关于如何使用armclient: https://blogs.msdn.microsoft.com/appserviceteam/2016/05/24/deploying-azure-web-app-certificate-through-key-vault/

我想要执行的命令是:

ARMClient.exe PUT /subscriptions/<Subscription Id>/resourceGroups/<Server Farm Resource Group>/providers/Microsoft.Web/certificates/<User Friendly Resource Name>?api-version=2016-03-01 "{'Location':'<Web App Location>','Properties':{'KeyVaultId':'<Key Vault Resource Id>', 'KeyVaultSecretName':'<Secret Name>', 'serverFarmId':'<Server Farm (App Service Plan) resource Id>'}}"

我创建了一个填充所需字段的字符串:

putparm=$resolved_armapi" \"{'Location':'$resolved_locationid','Properties':{'KeyVaultId':'$resolved_keyvaultid','KeyVaultSecretName':'$certname','serverFarmId':'$resolved_farmid'}}"\"

当我回显变量putparm的输出时,结果看起来像预期的那样(X-ed out names / ids):

/subscriptions/f073334f-240f-4261-9db5-XXXXXXXXXXXXX/resourceGroups/XXXXXXXX/providers/Microsoft.Web/certificates/XXXX-XXXXX-XXXXX?api-version=2016-03-01 "{'Location':'Central US','Properties':{'KeyVaultId':'/subscriptions/f073334f-240f-4261-9db5-XXXXXXXXXXXXX/resourceGroups/XXXXXXXX/providers/Microsoft.KeyVault/vaults/XXXXXXXX','KeyVaultSecretName':'XXXX-XXXXX-XXXXX','serverFarmId':'/subscriptions/f073334f-240f-4261-9db5-XXXXXXXXXXXXX/resourceGroups/XXXXXXXX/providers/Microsoft.Web/serverfarms/ServicePlan59154b1c-XXXX'}}"

当我在脚本中运行armclient put $putparm时,我收到此错误:

  

“错误”:{       “code”:“InvalidRequestContent”,       “message”:“请求内容无效,无法反序列化:'未终止的字符串。预期的分隔符:\”。路径'',   第1行,第21行。'。“}

但是当我接受$ putparm变量的输出并在服务器上运行“手动”命令时,它可以工作。

我猜它与linux存储变量的方式以及API请求JSON(或其他东西......)

为任何帮助感到高兴。

1 个答案:

答案 0 :(得分:1)

定义变量putparam的方式是错误的。

它可能被解释为文字字符串而不是对象。请注意,像"hello"这样的简单字符串是有效的JSON数据,但它可能不是您所期望的服务器。

如果你应该正确引用你的变量:

putparm="{\"Location\":\"$resolved_locationid\",\"Properties\":{\"KeyVaultId\":\"$resolved_keyvaultid\",\"KeyVaultSecretName\":\"$certname\",\"serverFarmId\":\"$resolved_farmid\"}}"

并像这样使用它:

armclient put "$resolved_armapi" "$putparm"