Search code examples
azurevirtual-machineazure-rm-templateazure-bicep

Pass Array value to a Variable


I'd like to variablise one element of an Array parameter that I'm passing to a Bicep template. Here's a cut down 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 Array parameter values I'm trying to set are dataDisks.value.name and dataDiskResources.value.name. I tried using "[concat(parameters('virtualMachineName'), '-_DataDisk_0')]'" in order to set the Virtual Machine disk's name, but I got an error "The value of parameter disk.name is invalid.".

Is there a way I can pass these values to a Variable in my Bicep template file and only edit the "name" value of each of these two parameters? Or is there another way I can edit it?


Solution

  • In Parameters you can't concat value . It will error out with "The value of parameter disk.name is invalid.". Instead you can use Variables in both Bicep ARM or JSON ARM.

    So your ARM template will be something link below :

    {
        "$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:

    enter image description here

    You can refer Variables in Bicep - Azure Resource Manager | Microsoft Docs & Variables in templates - Azure Resource Manager | Microsoft Docs for more information.