I am trying to build an Azure VNET via .json template.
I am trying to use an inline conditional statement to either create a 2nd subnet or skip creating the 2nd subnet. I do not think I am using json('null') correctly, or if this is even possible. My understanding is if json('null') is chosen, nothing is chosen.
Any help is appreciated!
"apiVersion": "2016-06-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "My-VNET",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[parameters('virtualNetworkCIDR')]"
]
},
"subnets": [{
"name": "[parameters('firstSubnetName')]",
"properties": {
"addressPrefix": "10.10.1.0/24"
}
}, {
"name": "[if(equals(parameters('createSecondSubnet'), 'Yes'), parameters('secondSubnetName'), json('null'))]",
"properties": {
"addressPrefix": "10.10.2.0/24"
}
}
]
}
I'm sure that you managed to get some kind of solution to this a while ago ... however, I have a solution that works well for this sort of thing ... it doesn't use conditional statements though.
In PowerShell create some hash-tables like so ...
# Resource group Hashtables.
$rgDev = @{
Name = "DEV-RG"
SubscriptionId = $subNonProd
Location = "desiredregion"
}
$rgUat = @{
Name = "UAT-RG"
SubscriptionId = $subNonProd
Location = "desiredregion"
}
#Vnet Hashtables
$vnetDev = @{
ResourceGroup = $rgDev
VnetName = "Dev-vnet"
CIDR = @('x.x.x.x/27')
Subnets = @(
@{
Name = "Dev-Web-subnet"
CIDR = "y.y.y.y/28"
},
@{
Name = "Dev-DB-subnet"
CIDR = "z.z.z.z/28"
})
}
$vnetUat = @{
ResourceGroup = $rgUat
VnetName = "UAT-vnet"
CIDR = @('f.f.f.f/27')
Subnets = @(
@{
Name = "UAT-Web-subnet"
CIDR = "g.g.g.g/28"
},
@{
Name = "UAT-DB-subnet"
CIDR = "h.h.h.h/28"
})
}
Next I lump the hashtables into an array and foreach over the lot. I have a little script that switches my context so that I can deploy to multiple subscriptions in the one bootstrap type script.
$vnets = @($vnetDev, $vnetUat)
ForEach ($vn in $vnets) {
$deploymentName = $vn.VnetName + "_Deployment."
.\SwitchSubscription.ps1 -subName $vn.ResourceGroup.SubscriptionName -subId $vn.ResourceGroup.SubscriptionId
New-AzureRmResourceGroupDeployment -Name $deploymentName `
-ResourceGroupName $vn.ResourceGroup.Name `
-TemplateFile .\JSONFiles\Vnets.json `
-vnet $vn
}
The Parameters section of the ARM template looks like this ...
"parameters": {
"vnet": {
"type": "object",
}
},
then the Resource Section looks like this ...
{
"name": "[concat(parameters('vnet').VnetName)]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2017-10-01",
"location": "[resourceGroup().location]",
"tags": "[parameters('vnet').tags]",
"properties": {
"addressSpace": {
"addressPrefixes": "[parameters('vnet').CIDR]"
},
"copy": [
{
"name": "subnets",
"count": "[length(parameters('vnet').Subnets)]",
"input": {
"name": "[parameters('vnet').Subnets[copyIndex('Subnets')].Name]",
"properties": {
"addressPrefix": "[parameters('vnet').Subnets[copyIndex('Subnets')].CIDR]"
}
}
}
]
}
}
]
So what this all does is pass an object through to the ARM template that can have one Vnet with one or more subnets and create them all.
Hopefully this will help someone else out when / if they find this when googling.
Cheers,
Dave.
:)