Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARM Templates: conditionally add to array

For my Azure ARM templates, I want to conditionally add an extra NSG rule. If parameter is true, append extra rule to the "securityRules" array. How do I efficiently go about this? I can't use the "condition" properties for nested objects. Creating two resources seems clunky.

like image 287
Ted Avatar asked Jul 23 '18 22:07

Ted


People also ask

What is Apiversion in ARM template?

This API version corresponds to a version of REST API operations that are released by the resource provider (Microsoft.Compute/virtualMachines is the resource provider and namespace for virtual machines, Microsoft.Storage/storageAccounts is the resource provider and namespace for storage and storage accounts - as ...

What is nested template in ARM?

A nested template is just basically one template within another. The advantage of a nested template is that you can go ahead and deploy resources across resource groups.

Can we use nested Azure ARM templates?

To deploy complex solutions, you can break your Azure Resource Manager template (ARM template) into many related templates, and then deploy them together through a main template. The related templates can be separate files or template syntax that is embedded within the main template.


1 Answers

Depending on a condition, you would like to add an additional (string) value to an existing json array. This can be done via the concat function. In order to concat an array and a string value, the string value will need to be transformed into an array too. When the condition is true, the two arrays can be concatenated. When the condition is false, you can concatenate the existing string with an empty array.

"[concat( parameters('existingArray'), if( parameters('condition'), array('Cc'), variables('emptyArray')) )]"

Assuming the orriginal array is: ["Aa", "Bb"]

  • When the condition is true, this will result in: ["Aa", "Bb", "Cc"]
  • When the condition is false, this will result in: ["Aa", "Bb"]
like image 55
Gijs Walraven Avatar answered Sep 30 '22 09:09

Gijs Walraven