Varun Sharma
Varun Sharma

Reputation: 2758

ARM Template - Conditionally add to an Array

{
    "$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 condition but it is at resource level so can't use that.

I also looked at if but the problem is that it still includes that JSON block in the array with a blank virtualNetworkSubnetId and that also fails. I want the entire first element (JSON block) of the virtualNetworkRules resource array to be excluded i.e. there should be only 2 elements in the array when the parameter externalSubnet1 is empty string.

So, how can we achieve this? Thanks.

Upvotes: 0

Views: 612

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11401

If I Understand it correctly then you want to conditionally execute the VirtualNetworkRule depending on the string parameter externalsubnet1 . If parameter value is present then x1 will be executed and if value is blank then x2 and x3 will be executed. In that case you can use the below template:

Template:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {        
        "externalSubnet1": {
            "type": "string"
        }
    },
    "variables": {
        "SQLServerName": "ansserver",
        "Subnet1": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/ansumantest/providers/Microsoft.Network/virtualNetworks/ansuman-vnet/subnets/default",
        "Subnet2": "/subscriptions/xxxxxxxxxxxxxxxxxxxx/resourceGroups/ansumantest/providers/Microsoft.Network/virtualNetworks/ansuman-vnet/subnets/subnet1"
    },
    "resources": [{
            "name": "[variables('SQLServerName')]",
            "type": "Microsoft.Sql/servers",
            "location": "East US",
            "apiVersion": "2015-05-01-preview",
            "properties": {
                "administratorLogin": "ansuman",
                "administratorLoginPassword": "password",
                "version": "12.0",
                "publicNetworkAccess":"Enabled"
            },
            "resources": [
                {   
                    "condition":"[not(empty(parameters('externalSubnet1')))]",
                    "type": "virtualNetworkRules",
                    "apiVersion": "2021-05-01-preview",
                    "name": "[concat(variables('SQLServerName'),'test1')]",
                    "dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
                    "properties": {
                        "virtualNetworkSubnetId": "[parameters('externalSubnet1')]",
                        "ignoreMissingVnetServiceEndpoint": false
                    }
                }, {
                    "condition":"[empty(parameters('externalSubnet1'))]",
                    "type": "virtualNetworkRules",
                    "apiVersion": "2021-05-01-preview",
                    "name": "[concat(variables('SQLServerName'),'test2')]",
                    "dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
                    "properties": {
                        "virtualNetworkSubnetId": "[variables('Subnet1')]",
                        "ignoreMissingVnetServiceEndpoint": false
                    }
                }, {
                    "condition":"[empty(parameters('externalSubnet1'))]",
                    "type": "virtualNetworkRules",
                    "apiVersion": "2021-05-01-preview",
                    "name": "[concat(variables('SQLServerName'),'test3')]",
                    "dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
                    "properties": {
                        "virtualNetworkSubnetId": "[variables('Subnet2')]",
                        "ignoreMissingVnetServiceEndpoint": false
                    }
                }
            ]
        }
    ]
}

Output:

enter image description here

enter image description here

Upvotes: 1

Related Questions