Search code examples
azureazure-virtual-machineazure-rm-template

Azure Virtual Machine ARM Template CopyIndex exclude avSet


I'm trying to deploy a number of Virtual Machines from a Variable Object, some of the virtual machines need to be added to an AVSet while others do not.

I'm struggling to find a way to iterate through the object while excluding AVSet etc.

    "virtualMachineSettings": [
        {
            "name": "WEBSERVER",
            "subnet": "web",
            "vmSize": "Standard_B2ms",
            "publisher": "[parameters('standardVMPublisher')]",
            "offer": "[parameters('standardVMoffer')]",
            "sku": "[parameters('standardVMsku')]",
            "avset": "webServersAVSet"
        },  
        {
            "name": "APPSERVER",
            "subnet": "app",
            "vmSize": "Standard_B2ms",
            "publisher": "[parameters('standardVMPublisher')]",
            "offer": "[parameters('standardVMoffer')]",
            "sku": "[parameters('standardVMsku')]",
            "avset": "appServersAVSet"
        },
        {
            "name": "TESTSERVER",
            "subnet": "data",
            "vmSize": "Standard_B2ms",
            "publisher": "[parameters('standardVMPublisher')]",
            "offer": "[parameters('standardVMoffer')]",
            "sku": "[parameters('standardVMsku')]",
            "avset": ""
        },
        {
            "name": "SQLSERVER",
            "subnet": "data",
            "vmSize": "Standard_B4ms",
            "publisher": "MicrosoftSQLServer",
            "offer": "SQL2017-WS2016",
            "sku": "SQLDEV",
            "avset": ""
        }

    ],

        {
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2019-07-01",
            "name": "[concat(variables('VirtualMachineSettings')[copyIndex()].name, '-', parameters('octopusTenant'))]",
            "copy": {
                "name": "vmIterator",
                "count": "[length(variables('VirtualMachineSettings'))]"
            },
            "dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/', variables('virtualMachineSettings')[copyIndex()].name, '-', parameters('octopusTenant'),'-NIC')]",
                "avSet"
            ],
            "location": "[resourceGroup().location]",
            "tags": {
            },
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[variables('virtualMachineSettings')[copyIndex()].vmSize]"
                },
                 "availabilitySet": {
                    "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('virtualMachineSettings')[copyIndex()].avSet)]"
                },

So I think I need to set an if statement if the parameter avset is empty? but I'm stuck. Any help would be amazing.

Thanks


Solution

  • You can make a little change in the solution you added as the comment and the variable virtualMachineSettings:

    "virtualMachineSettings": [
      {
          "name": "WEBSERVER",
          "subnet": "web",
          "vmSize": "Standard_B2ms",
          "publisher": "[parameters('standardVMPublisher')]",
          "offer": "[parameters('standardVMoffer')]",
          "sku": "[parameters('standardVMsku')]",
          "avset": "webServersAVSet",
          "availiabilitySet": {
            "id": "[resourceId('Microsoft.Compute/availabilitySets', "webServersAVSet")]"
          }
      },
      ...
    ]
    
    "properties": {
      "availabilitySet": "[if(not(empty(variables('virtualMachineSettings')[copyIndex()].avSet)), variables('virtualMachineSettings')[copyIndex()].availiabilitySet)]",
      ...
    }
    

    Or you can set a boolean property in the variable virtualMachineSettings for each member to judge if you need to set the availability set for the VM or not.