az2020
az2020

Reputation: 21

Azure template how do we change the resource name based on resource group location?

I am working on a common template where it uses different vnet in each Region. How do we change the template based on location, set vnet name.

Can we use if (resourcegroup.location) ?

Upvotes: 0

Views: 2009

Answers (1)

I think that I know what it's happening to you, you are not comparing against a valid value, "EastUs" it's not the resourceGroup().location, in this case is "eastus". You can make a test deployment ande see the output to check that kind of values.

I have modified the template that you have passed in the comments to do what you wanted.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "storageAccountName": "[if(equals(resourceGroup().location,'eastus'),'eastusstorageaccount','noteastusstorageaccount')]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2018-07-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountName": {
      "type": "string",
      "value": "[variables('storageAccountName')]"
    },
    "location": {
      "type": "string",
      "value": "[resourceGroup().location]"
    }
  }
}

To do what you want in a dynamic way, I would try to encode the name of the vbnet. Something like this:

  1. eastusvbnet
  2. westeuropevbnet
  3. uksouthvbnet

And in the variable I will put this:

"variables": {
    "storageAccountName": "[concat(resourceGroup().location,'vbnet')]"
  }

Upvotes: 1

Related Questions