'Is there any way in ARM Templates to use a Case-like statement?

Is there any way in Azure to use a CASE-like statement depending on the value of a parameter or variable?

For instance:

    if vmName = "DB" then adminloginName = "DBAdmin"
    if vmName = "RDP" then adminlogonNAme = "RDPAdmin"


Solution 1:[1]

No, ARM Template functions as of today NOT YET support Case like statements.

However, they do support the logical if function already.

Logical functions

Resource Manager provides the following functions for working with logical conditions:

  • and
  • bool
  • if
  • not
  • or

Reference: Azure Resource Manager template functions - Logical Functions

Solution 2:[2]

You can emulate that with this:

"variables": {
    "vmName": "DB" # this supposed to be parameter, but for brievity i'll use variable
    "adminloginNameDB": "DBADMIN"
    "adminloginNameRDP": "RDPADMIN"
    "adminlogin": "[variables(concat('adminloginName', variables('vmName')))]"
}

so in reality you would use it like this: "[variables(concat('adminloginName', parameters('vmName')))]"

Solution 3:[3]

As @juvchan pointed out. There is no support for a CASE statement, though there is support for some logical checks. To solve the problem you've described, however, you could use variables in conjunction with the parameters to construct the admin name. For example...

"parameters": {
   "vm_name": {
     "defaultValue": "DB",
     "type": "string"
},

"variables": {
   "admin_name": "[concat(parameters('vm_name'), 'Admin')]"
}

Then you can just use the admin_name variable down in the resource definition.

"resources":[
   {
      ...other resource properties
      "adminloginName": "[variables('admin_name')]"
   }
]

There are also some string manipulation functions (Azure ARM string functions), including a substring function so that if you have VM names that start with or end with DB or RDP, then you can use just the part of the name you need.

Solution 4:[4]

Sorry but so far the is no way to do such approach, however, you can apply some workaround / hacks:

OPTION 1:

"variables": {
  "adminloginName": "[if(equals(parameters('vmName'),'DB'),'DBAdmin',   'RDPAdmin'  )]"
}

OPTION 2:

"variables": {
  "adminloginNames":["DBAdmin","RDPAdmin"],
  "adminloginName": "[if(equals(parameters('vmName'),'DB'),first(variables('adminloginNames'),last(variables('adminloginNames')  )]",
}

There are other many combinations

Solution 5:[5]

You could emulate case statements with nested if statements like so.

"variables": {
    "subscription_id": "[if(equals(parameters('env'),'dev'), parameters('devsubscriptionid'), if(equals(parameters('env'),'uat'), parameters('uatsubscriptionid'), parameters('prodsubscriptionid')))]",
    "routeTables_rt_gdp_apim_dev_ne_001_externalid": "[concat('/subscriptions/', variables('subscription_id'),'/resourceGroups/RG-COM-FIREWALL-DEV-NE-001-test/providers/Microsoft.Network/routeTables/rt-gdp-apim-dev-ne-001')]"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 juvchan
Solution 2
Solution 3
Solution 4 Alberto S.
Solution 5 Suraj Rao