【问题标题】:How to escape single quote in ARM template如何在ARM模板中转义单引号
【发布时间】:2016-03-03 08:32:06
【问题描述】:

鉴于 AzureRM 模板中的以下资源,如何对 commandToExecute 部分中的单引号进行编码?

{
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "name": "[concat(variables('vmNameMaster'), copyIndex(), '/sethost')]",
  "apiVersion": "2015-06-15",
  "location": "[resourceGroup().location]",
  "copy": {
      "name": "extensionLoopNode",
      "count": "[variables('masterCount')]"
  },
  "dependsOn": [
      "[concat('Microsoft.Compute/virtualMachines/', variables('vmNameMaster'), copyIndex(),'/extensions/DockerExtension')]"
  ],
  "properties": {
    "publisher": "Microsoft.OSTCExtensions",
    "type": "CustomScriptForLinux",
    "typeHandlerVersion": "1.4",
    "settings": {
      "fileUris": [ ],
      "commandToExecute": "[concat('/bin/bash -c \'echo \"export DOCKER_HOST=:2375\" >> /home/', parameters('adminUsername') ,'/.profile\'')]",
      "timestamp": 123456789
    }
  }
},

【问题讨论】:

  • 你试过用\转义吗?即 "commandToExecute": "[concat('/bin/bash -c \'echo \"export DOCKER_HOST=:2375\" >> /home/\', parameters(\'adminUsername\') ,\'/.profile \'')]"

标签: azure azure-resource-manager


【解决方案1】:

转义 Azure ARM 函数的方式与 VB 字符串相同:只需将单引号字符加倍即可。

[concat('This is a ''quoted'' word.')]

输出

This is a 'quoted' word.

双引号仍然需要从 JSON 中转义。

 [concat('''single'' and \"double\" quotes.')]

输出

'single' and "double" quotes.

【讨论】:

  • 这应该是被接受的答案,因为当前接受的答案并不优雅。
  • 不幸的是,Visual Studio 会将此视为错误:应为逗号。它可能有效,但您将不得不忽略红色曲线。很多人会觉得这是不可接受的。
  • 这对我不起作用。所以我不得不求助于不太优雅的解决方案:)
【解决方案2】:

我用一个变量解决了这个问题:

"variables": {
    "singleQuote": "'",
},
...
"settings": {
    "fileUris": [],
    "commandToExecute": "[concat('/bin/bash -c ', variables('singleQuote'), 'echo \"export DOCKER_HOST=:2375\" >> /home/', parameters('adminUsername') ,'/.profile', variables('singleQuote'))]",
}

它并不优雅,但它确实有效。

【讨论】:

  • 谢谢你,为我节省了很多时间
  • '' 的答案如何?
【解决方案3】:

在 DevOps 发布管道中,对于 APIM 策略,使用 &quote;转义表达式中的引号,

<when condition='@(context.Variables.GetValueOrDefault&lt;bool&gt;(&quot;isAuthOk&quot;))' />

【讨论】:

    【解决方案4】:

    没有必要在 commandToExecute 部分编码单引号。 下面的 json 段已被验证为有效的 json http://jsonlint.com/

    {
        "type": "Microsoft.Compute / virtualMachines / extensions ",
        "name": "[concat(variables('vmNameMaster'), copyIndex(), '/sethost')]",
        "apiVersion": "2015-06-15",
        "location": "[resourceGroup().location]",
        "copy": {
            "name": "extensionLoopNode",
            "count": "[variables('masterCount')]"
        },
        "dependsOn": [
            "[concat('Microsoft.Compute/virtualMachines/', variables('vmNameMaster'), copyIndex(),'/extensions/DockerExtension')]"
        ],
        "properties": {
            "publisher": "Microsoft.OSTCExtensions",
            "type": "CustomScriptForLinux",
            "typeHandlerVersion": "1.4",
            "settings": {
                "fileUris": [],
                "commandToExecute": "[concat('/bin/bash -c 'echo \"export DOCKER_HOST=:2375\" >> /home/', parameters('adminUsername') ,'/.profile'')]",
                "timestamp": 123456789
            }
        }
    }
    

    【讨论】:

    • 但它不是有效的手臂模板
    • 你得到的错误是什么,你的 ARM 模板 json 架构是什么?
    • 这里是架构:schema.management.azure.com/schemas/2015-01-01/…。问题是,arm 会将单引号识别为自己的处理指令...
    • 这不起作用,因为未转义的单引号是为 ARM 模板中的模板函数保留的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2013-12-26
    • 2018-05-29
    • 2011-02-28
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多