I'm not keen on duplicating templates several times, so I decided to design a single template to support all the options based on parameters passed to the template.
I assumed this would be fairly simple to do, but the lack of logical and comparison operators available in the Resource Manager Template functions actually make this tricky.
The good news is that we can take advantage of JSON objects and data to achieve a dynamic template for multiple conditional options. This has allowed me to create a template that can be used for conditionally configuring the following options on a VM:
- Dynamic or static private IP
- Public IP address
- Availability set
I'll start with the parameters that are important of this design, there are a number of parameters in the full template but we can ignore them for this explanation:
These are fairly self explanatory, so I'll move onto the variables that use the parameters.
The first variable, publicIpConfig, is actually a JSON object with two child objects:
In our environment, the name of a public IP address matches the name of the VM, so we can use the name parameter for the VM, however you can easily change that to suit your needs.
"None" returns an empty object, not much more to say about that!
The publicIpAddressObject variable is where it gets interesting, this references either publicIpConfig.assigned or publicIpConfig.none, depending on the value of the publicIpAssignment parameter.
It's this reference that allows us to conditionally assign properties to the resource.
Next we move onto nicIpConfiguration variable, which has two child objects:
the nicIpProperties variable references either nicIpConfiguration.Static or nicIpConfiguration.Dynamic, depending on the value of the privateIPAllocationMethod parameter.
Again, it's the value of the parameter that is driving the configuration that is assigned to the resource.
Last of all, we have availabilitySetConfiguration, which has two child objects:
These objects contain the necessary information for configuring a VM as part of an availability set.
As you'll notice I'm not using the resourceId() function, instead I'm building the object ID with the subscription().subscriptionId and resourceGroup()..name properties.
availabilitySetProperties returns either availabilitySetConfiguration.member or availabilitySetConfiguration.none depending on the availabilitySetStatus parameter.
ResourcesWe all of the variables configured, we can move onto the resources.
The following example uses the nicIpProperties variable to set the properites on a network interface:
The following example uses the availabilitySetProperties variable to configure an availability set on a VM, the value will be an empty object if the VM isn't part of an availability set:
It took me some time to piece this all together, so I really do hope it helps others save time working this out and reduces the number of templates you need to use! I'm sure there are many other conditional objects that can use this design, feel free to contact me with any questions.
Here's a copy of the full template: