Using conditional IF statements in hand templates for properties,

I have a situation where I want to add a property to a virtual machine only if a condition is met. For example, if I want to add a readiness set property to the machine, do the following: Below I ONLY execute the accessibility set statement, if the condition is TRUE, can you do it in the ARM template? for example if the value is true then execute this line if not missing?

 {
  "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')]"
    },

      

+3


source to share


3 answers


Starting this week (August 1, 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 ask, add (or not add) the set set for the VM:



"apiVersion": "2016-03-30",
            "type": "Microsoft.Compute/virtualMachines",
            "properties": {
                "availabilitySet": "[if(equals(parameters('availabilitySet'),'yes'), variables('availabilitySet'), json('null'))]",
                ...
            }

      

Note that the usable True value is stored as a variable, not a string in the if statement.

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

      

+2


source


There is no direct way to do this. you can use a conditional to select a value, but you cannot set a value or pass in null

(unless you want to define 2 variables for 'properties').

so you can just define 2 vm resources which are exactly the same except for the property in question (so availabilitySet

). That would be so, but not otherwise. And you would use "condition": expression

in both resources. The condition must be equal to false

or true

for work. ARM templates have several functions that can evaluate input and return true

or false

.



Help:
https://samcogan.com/conditions-in-arm-templates-the-right-way/

0


source


When I try to do the same for the loadbalancer firewall, I get errors:

12:21:49 - [ERROR] "message": "Unable to deserialize the current JSON object (for example,

12:21:49 - [ERROR] {\ "name \": \ "value \"}) to type "Microsoft.WindowsAzure.Networking.Nrp.Frontend

12:21:49 - [ERROR] .Contract.Csm.Public.ResourceReferenceHashSet`2 [Microsoft.WindowsAzure.Networki

12:21:49 - [ERROR] ng.Nrp.Frontend.Contract.Csm.Public.LoadBalancerBackendAddressPool, Microsoft.Wi

12:21:49 - [ERROR] ndowsAzure.Networking.Nrp.Frontend.Contract.Csm.Public.NetworkInterfaceIpConfig

12:21:49 - [ERROR] uration] 'because it requires a JSON array (eg [1,2,3]) to deserialize

12:21:49 - [ERROR] is correct. \ R \ nTo fix this error, either change the JSON to a JSON array (for example

12:21:49 - [ERROR] [1,2,3]) or change the deserialized type to be a regular .NET type

12:21:49 - [ERROR] (for example, not a primitive integer type, not a collection type like an array

12:21:49 - [ERROR] or List) that can be deserialized from a JSON object. JsonObjectAttribute

12:21:49 - [ERROR] can also be added to the type to make it deserialize from JSON

variables

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

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

0


source







All Articles