6

I can't seem to find any way to use the value of a parameter as part of another parameter in a parameter file for ARM templates:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "someCustomParam": {
        "value": "desired value"
    },
    "tags": {
        "value": {
            "tag1": "[parameters('someCustomParam')]",
            "tag2": "some tag value"
        }
    },
  }
}

Notice how I want to use the value of a previous parameter for the value of another.

The value for "tag1" is simply the string and the value does not get substituted in from the parameter() function. I've tested this by using the Test-AzResourceGroupDeployment PowerShell cmdlet.

Is there any way I can do this?

Birdman
  • 1,404
  • 5
  • 22
  • 49

3 Answers3

0
  • You can do this using PowerShell. Before calling Test-AzResourceGroupDeployment you can get content of parameter file in PowerShell variable/object using
  • $ParameterObject = Get-Content ./ParameterFileName.json
  • Update required value like:

  • $ParameterObject.parameters.tags.value.tag1 = #Value to assign

  • Pass $parameterObject to -TemplateParameterObject parameter of Test-AzResourceGroupDeployment

------OR------

  • Convert $parameterObject to JSON using ConvertTo-Json and Save as temp.json and pass temp.json to -TemplateParameterFile [reference below]
  • $TempParameterFile = ( $ParametersObject | ConvertTo-Json -Depth 20 ) -replace "\\u0027", "'" -replace "\\u0026", "&" | Out-File $tmp -Force
  • and use $TempParameterFile for -TemplateParameterFile
-1

You need to use variables.

In variables, you can use "[parameters('parameterName')]".

And you can use variables in a similar way as parameters : "[variables('variableName')]"

Update:

Here is a sample:

"parameters": {
    "someCustomParam": {
        "type": "string"
    }
},
"variables": {
    "tags": {
        "tag1": "[parameters('someCustomParam')]",
        "tag2": "some tag value"
    }
}

And then you can use the variable in the resource of your template.

Jack Jia
  • 5,268
  • 1
  • 12
  • 14
  • 1
    The problem is I'm trying to do this in a parameter file. I don't think I can use variables in a parameter file, correct? I'm trying to have 1 main ARM tempalte file and many parameter files. So I'm trying to hard code specifics in the param files and not the main ARM file. This is why I'm trying to do this in the parameter file specifically where I don't think I can use variables. – Birdman Jul 25 '19 at 02:26
  • You can pass some parts to the parameters. And use the passed values to construct a variable. The variable would be the final object you want. And you can use it in the template. – Jack Jia Jul 25 '19 at 02:55
  • 1
    You can not use any function in the parameter. the parameter can only accept a static value. So, I suggest you use variable. – Jack Jia Jul 25 '19 at 02:57
  • 1
    that is not actually true, you can use functions in the parameter default values (maybe not all of the functions) – 4c74356b41 Mar 14 '20 at 16:25
-2

You can do this just like what you normally do when you reference a parameter, an example as following:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "paramOne": {
            "value": "hello"
        },
        "paramTwo": {
            "value": "[concat(parameters('paramOne'), '-', 'world)]"
        }
    }
}

the output value of 'paramTwo' will be 'hello-world'.

Hope this helps whoever wants to utilize referencing a parameter in a parameter in a parameter file.