如何在ARM模板中转义单引号

时间:2015-11-29 18:10:16

标签: azure azure-resource-manager

鉴于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
    }
  }
},

4 个答案:

答案 0 :(得分:27)

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

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

输出

This is a 'quoted' word.

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

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

输出

'single' and "double" quotes.

答案 1 :(得分:14)

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

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

它不优雅,但它有效。

答案 2 :(得分:0)

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

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

答案 3 :(得分:-5)

不需要在commandToExecute部分编码单引号。 下面的json段已被验证为有效的json in 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
        }
    }
}