Problem
One member of an Array argument that I’m sending to a Bicep template needs to be variable. The following is a condensed version of the parameter file:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "uksouth"
},
"networkSecurityGroupRules": {
"value": [
{
"name": "RDP",
"properties": {
"priority": 300,
"protocol": "TCP",
"access": "Allow",
"direction": "Inbound",
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "3389"
}
}
]
},
"vnetName": {
"value": "Dtldevopsagent"
},
"subnetName": {
"value": "sandbox_subnet_2"
},
"virtualMachineName": {
"value": "test-db"
},
"osDiskType": {
"value": "Premium_LRS"
},
"dataDisks": {
"value": [
{
"lun": 0,
"createOption": "attach",
"caching": "ReadOnly",
"writeAcceleratorEnabled": false,
"id": null,
"name": "[concat(parameters('virtualMachineName'), '-_DataDisk_0')]'",
"storageAccountType": null,
"diskSizeGB": null,
"diskEncryptionSet": null
},
{
"lun": 1,
"createOption": "attach",
"caching": "None",
"writeAcceleratorEnabled": false,
"id": null,
"name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
"storageAccountType": null,
"diskSizeGB": null,
"diskEncryptionSet": null
}
]
},
"dataDiskResources": {
"value": [
{
"name": "[concat(parameters('virtualMachineName'),'-DataDisk_0')]",
"sku": "Premium_LRS",
"properties": {
"diskSizeGB": 32,
"creationData": {
"createOption": "empty"
}
}
},
{
"name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
"sku": "Premium_LRS",
"properties": {
"diskSizeGB": 32,
"creationData": {
"createOption": "empty"
}
}
}
]
},
}
}
The dataDisks.value.name and dataDiskResources.value.name Array parameter values I’m trying to set are dataDisks.value.name and dataDiskResources.value.name. I attempted to set the Virtual Machine disk’s name using “[concat(parameters(‘virtualMachineName’), ‘- DataDisk 0’)]” but received the error “The value of parameter disk.name is illegal.”
Is there a way to transfer these values to a Variable in my Bicep template file and just change the “name” value of these two parameters? Is there any other way I can make changes?
Asked by Powellellogram
Solution #1
You can’t concat value in parameters. “The value of parameter disk.name is invalid.” will be the error message. Variables can be used instead in both Bicep ARM and JSON ARM.
As a result, your ARM template will look like this:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "uksouth"
},
"networkSecurityGroupRules": {
"type": "array",
"defaultValue": [
{
"name": "RDP",
"properties": {
"priority": 300,
"protocol": "TCP",
"access": "Allow",
"direction": "Inbound",
"sourceAddressPrefix": "*",
"sourcePortRange": "*",
"destinationAddressPrefix": "*",
"destinationPortRange": "3389"
}
}
]
},
"vnetName": {
"type": "string",
"defaultValue": "Dtldevopsagent"
},
"subnetName": {
"type": "string",
"defaultValue": "sandbox_subnet_2"
},
"virtualMachineName": {
"type": "string",
"defaultValue": "test-db"
},
"osDiskType": {
"type": "string",
"defaultValue": "Premium_LRS"
}
},
"variables":{
"dataDisks": [
{
"lun": 0,
"createOption": "attach",
"caching": "ReadOnly",
"writeAcceleratorEnabled": false,
"id": null,
"name": "[concat(parameters('virtualMachineName'), '-_DataDisk_0')]",
"storageAccountType": null,
"diskSizeGB": null,
"diskEncryptionSet": null
},
{
"lun": 1,
"createOption": "attach",
"caching": "None",
"writeAcceleratorEnabled": false,
"id": null,
"name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
"storageAccountType": null,
"diskSizeGB": null,
"diskEncryptionSet": null
}
],
"dataDiskResources": [
{
"name": "[concat(parameters('virtualMachineName'),'-DataDisk_0')]",
"sku": "Premium_LRS",
"properties": {
"diskSizeGB": 32,
"creationData": {
"createOption": "empty"
}
}
},
{
"name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
"sku": "Premium_LRS",
"properties": {
"diskSizeGB": 32,
"creationData": {
"createOption": "empty"
}
}
}
]
},
"resources":[],
"outputs": {
"DatadiskName": {
"value": "[variables('dataDisks')[0].name]",
"type": "string"
},
"DatadiskResourceName": {
"value": "[variables('dataDiskResources')[0].name]",
"type": "string"
}
}
}
Output:
For further details, see Bicep variables – Azure Resource Manager | Microsoft Docs and Variables in templates – Azure Resource Manager | Microsoft Docs.
Answered by AnsumanBal-MT
Post is based on https://stackoverflow.com/questions/70851828/pass-array-value-to-a-variable