ARM deployment multi-line string with escaped quotes

Viewed 974

I am trying to deploy this ARM template

/* example_template.json */
{
    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "var": {
            "type": "string",
            "defaultValue": "
                echo ${VARIABLE}
            ",
            "metadata": {
                "description": "some description"
            }
        }
    },
    "resources": [],
    "outputs": {
        "ouput": {
            "type": "string",
            "value": "[string(parameters('var'))]"
        }
    }
}

which successfully outputs what I want:

    "outputs": {
      "ouput": {
        "type": "String",
        "value": "\n                echo ${VARIABLE}\n            "
      }
    }

The problem is, if I am trying to use this in a bash script, $VARIABLE may have a space in it, hence I need to output to be

    "outputs": {
      "ouput": {
        "type": "String",
        "value": "\n                echo \"${USER}\"\n            "
      }
    }

to prevent argument splitting.

So, I have tried to edit my template to include the quotes

/* example_template.json */
{
    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "var": {
            "type": "string",
            "defaultValue": "
                echo \"${VARIABLE}\"
            ",
            "metadata": {
                "description": "some description"
            }
        }
    },
    "resources": [],
    "outputs": {
        "ouput": {
            "type": "string",
            "value": "[string(parameters('var'))]"
        }
    }
}

Which gives me a validate and create error:

> az deployment group validate -f example_template.json -g resource-group-name
Failed to parse 'example_template.json', please check whether it is a valid JSON format

This seems to only happen with the multi-line string - if I put the whole defaultValue on a single line i.e.,

"defaultValue": "echo \"${VARIABLE}\""

it is successful again.

I need to use a multiline string as this variable is for a long deployment script which would be infeasible to put on one-line.

I believe this is a bug due to the parser only failing with the multiline string, but am unsure where to report it!

Does anyone know what a possible solution to this could be?

Thanks, Akhil

1 Answers

Actually there is no significance to do multi-line manually like you did.

As you can see, the type of this "var" parameter is "string", so it should be a string in "defaultValue", thus the reason you got the error message. Even it worked without error when using PowerShell command like "New-AzDeployment" ,but the result is same.

As you want to "use a multiline string as this variable is for a long deployment script", you can try do this to make the defaultValue to be a string:

"parameters": {
    "var": {
        "type": "string",
        "defaultValue": "VARIABLE={value1}\n echo \"${VARIABLE}\"",
        "metadata": {
            "description": "some description"
        }
    }
},

The output should be:

VARIABLE={value1}
echo "${VARIABLE}"

Actually az command is based on python, Power Shell command is based on c#, which made them parse json differently.

About how Azure CLI parse json, it starts reading defaultValue string with ", and cannot find the opposite " in the same line, then the error occurs.

About how Power Shell parse json, it starts reading defaultValue string with :, and read the whole content included in that :.

Deploy ARM template with Power Shell.

Related