Home > database >  ARM Template - Conditionally add to an Array
ARM Template - Conditionally add to an Array

Time:01-12

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {        
        "externalSubnet1": {
            "type": "string",
            "defaultValue": ""
        }
    },
    "variables": {
        "SQLServerName": "someName",
        "SQLDatabaseName": "someDatabase",
        "Subnet1": "/subscriptions/771adxxx-xxxx-xxxx-9xxx-xxxxxxxxxxxx/resourceGroups/some_resource_group/providers/Microsoft.Network/virtualNetworks/some_vnet/subnets/some_subnet1",
        "Subnet2": "/subscriptions/771adxxx-xxxx-xxxx-9xxx-xxxxxxxxxxxx/resourceGroups/some_resource_group/providers/Microsoft.Network/virtualNetworks/some_vnet/subnets/some_subnet1",
    },
    "resources": [{
            "name": "[variables('SQLServerName')]",
            "type": "Microsoft.Sql/servers",
            "location": "Central US",
            "apiVersion": "2021-05-01-preview",
            "dependsOn": [],
            "tags": {
                "displayName": "Logical SQL Server"
            },
            "kind": "v12.0",
            "properties": {
                "administratorLogin": "xyz",
                "administratorLoginPassword": "xyz",
                "version": "12.0"
            },
            "resources": [
                {
                    "type": "Microsoft.Sql/servers/virtualNetworkRules",
                    "apiVersion": "2021-05-01-preview",
                    "name": "x1",
                    "dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
                    "properties": {
                        "virtualNetworkSubnetId": "[parameters('externalSubnet1')]",
                        "ignoreMissingVnetServiceEndpoint": false
                    }
                }, {
                    "type": "Microsoft.Sql/servers/virtualNetworkRules",
                    "apiVersion": "2021-05-01-preview",
                    "name": "x2",
                    "dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
                    "properties": {
                        "virtualNetworkSubnetId": "[variables('Subnet1')]",
                        "ignoreMissingVnetServiceEndpoint": false
                    }
                }, {
                    "type": "Microsoft.Sql/servers/virtualNetworkRules",
                    "apiVersion": "2021-05-01-preview",
                    "name": "x3",
                    "dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
                    "properties": {
                        "virtualNetworkSubnetId": "[variables('Subnet2')]",
                        "ignoreMissingVnetServiceEndpoint": false
                    }
                }
            ]
        }
    ]
}

I have the following ARM template. In the innermost resources array there are 3 virtual network rules. What I want is that if the value of the parameter externalSubnet1 is there i.e. it is non blank string then I want the first element of the resources array to be included. This makes sense because the variable externalSubnet1 needs to be a valid subnet id (something similar to variables Subnet1 or Subnet2) otherwise the deployment fails.

What I tried?

I looked at enter image description here

enter image description here

  •  Tags:  
  • Related