Poul K. Sørensen
Poul K. Sørensen

Reputation: 17540

How to escape single quote in ARM template

Given the following resource in an AzureRM template, how would one encode the single quote in the commandToExecute part?

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

Upvotes: 30

Views: 16065

Answers (4)

usman shaheen
usman shaheen

Reputation: 3766

In DevOps release pipeline, for APIM policy, use & quote; to escape quotes within an expression,

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

Upvotes: 2

Eric Cote
Eric Cote

Reputation: 891

You escape Azure ARM functions in the same way as with VB strings: you simply double the single quote characters.

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

outputs

This is a 'quoted' word.

Double quotes still needs to be escaped from JSON.

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

outputs

'single' and "double" quotes.

Upvotes: 64

Jason Stangroome
Jason Stangroome

Reputation: 4489

I've worked around this with a variable:

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

It isn't elegant but it works.

Upvotes: 18

juvchan
juvchan

Reputation: 6255

it is not necessary encode the single quote in the commandToExecute part. The json segment below has been validated as valid 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
        }
    }
}

Upvotes: -4

Related Questions