using conditional IF statements in arm templates for properties,

Viewed 20852

I have a situation where I want to only add a property to a VM if a condition is met. For example if I want to add an availability set property to a machine then do this : Below I ONLY what to execute the availability set statement if a condition is TRUE, can you do this in an ARM template? eg if a value is true then do this line, if not skip?

 {
  "name": "[parameters('ComputerName')]",
  "type": "Microsoft.Compute/virtualMachines",
  "location": "[parameters('location')]",
  "apiVersion": "2017-03-30",
  "dependsOn": [
    "[resourceId('Microsoft.Network/networkInterfaces', variables('1stNicName'))]",
    "[resourceId('Microsoft.Network/networkInterfaces', variables('2ndicName'))]"
  ],
  "tags": {
    "displayName": "[parameters('ComputerName')]"
  },

  "properties": 

{
"availabilitySet": {
      "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
    },


 "hardwareProfile": {
      "vmSize": "[parameters('serverVmSize')]"


 },
    "osProfile": {
      "computerName": "[parameters('serverName')]",
      "adminUsername": "[parameters('adminUsername')]",
      "adminPassword": "[parameters('adminPassword')]"
    },
5 Answers
"variables": {
    "loadBalancerBackendAddressPools": [
      {
        "id": "[parameters('lbBackendAddressPool')]"
      }
    ]
},

Believe i have found a workable way to avoid defining two entire VM definitions for an availability set. following are from my own testing in an arm template i have made with extensive conditional controls.

this works:

"availabilitySet": {
  "id": "[resourceId('Microsoft.Compute/availabilitySets',parameters('availabilitySetName'))]"
}

this does not:

"availabilitySet": "[if(equals(parameters('availabilitySet'),'yes'), variables('availabilitySet'), json('null'))]",

this also does not work due to ID being 'null':

"availabilitySet": {
  "id": "[if(equals(parameters('availabilitySet'),'yes'), variables('availabilitySet'), json('null'))]"
}

I have added the following variable and parameter:

"parameters": {
"SpotInstance": {
            "type": "bool",
            "defaultValue": false,
            "allowedValues": [
                true,
                false
            ],
            "metadata": {
                "description": "Should the VM be deployed as a low cost Spot Instance, this will deploy without attaching the Availability Set."
            }
        }
},
"variables": {
"AvailibilitySet": {
            "On":{
                "id":"[resourceId('Microsoft.Compute/availabilitySets/',variables('ASName'))]"
            },
            "Off":"[json('null')]"
        }
    }

then used this logic on the availability set assignment:

"availabilitySet": "[if(equals(parameters('SpotInstance'),bool('true')),variables('AvailibilitySet').Off,variables('AvailibilitySet').On)]",

if you do not define id as a property of "AvailabilitySet" this can be null, if you Do however it cannot be null.

Related