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')]"
},
The default value attribute is used to make a parameter optional: if you deploy the template and don't specify a value for a particular parameter, ARM will just use the default value configured on that parameter. If you don't specify a value and the parameter doesn't have a default value, you get a deployment error.
For linked or nested templates, you can only set the deployment mode to Incremental. However, the main template can be deployed in complete mode.
The basic syntax of the Azure Resource Manager template (ARM template) is JavaScript Object Notation (JSON). However, you can use expressions to extend the JSON values available within the template.
As of this week (1st August 2017), ARM templates now have an IF function that can be used for variables, properties and resource parameters. It works like most other ARM functions, with syntax like:
[if(condition, true value, false value)]
The example in the documentation does exactly what you are asking, adds (or doesn't add) an availbility set to a VM:
"apiVersion": "2016-03-30",
"type": "Microsoft.Compute/virtualMachines",
"properties": {
"availabilitySet": "[if(equals(parameters('availabilitySet'),'yes'), variables('availabilitySet'), json('null'))]",
...
}
Note that the True value, the availiblity set to use, is stored as a variable rather than inline in the if statement.
"variables": {
...
"availabilitySetName": "availabilitySet1",
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
}
},
There is no direct way of doing this. you can use conditional to choose a value, but you cannot not set a value or pass in null
(unless you want to define 2 variables for `properties').
so you can just define 2 vm resources that are exactly the same except for the property in question (so availabilitySet
). One would have it and the other one would not. And you would use "condition": expression
in both resources. Condition should equals to false
or true
to work. There are several functions in ARM templates that can evaluate input and return true
or false
.
Reference:
https://samcogan.com/conditions-in-arm-templates-the-right-way/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With