ARM Template - Conditionally add to an Array
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: