'ARM Template If Condition for Prepoluting of Variables declared

I am trying to implement if condition for the arm template, we have two V-Net’s and 2 subnets associated to each V-Net

AVD-US V-Net have two subnets (avd-us-subnet1, avd-us-subnet2) AVD-UK V-Net have two subnets (avd-uk-subnet1, avd-uk-subnet2)

In the template we have declared all the V-Nets and Subnets while deploying we would be selecting the required parameters.

when we select AVD-US V-Net, the associated Subnet (avd-us-Subnet1 or avd-us-subnet 2) only should Pre-Populate using the if condition.

"existingVnetName": {
            "type": "string",
            "metadata": {
                "description": "The name of the virtual network the VMs will be connected to."
            },
            "allowedValues": [
                "AVD-US V-Net",
                "AVD-UK V-Net"
            ]
        },
        "existingSubnetName": {
            "type": "string",
            "metadata": {
                "description": "The subnet the VMs will be placed in."
            },

            "allowedValues": [
                "avd-us-subnet1",
                "avd-us-subnet2",
                "avd-uk-subnet1",
                "avd-uk-subnet2"

            ]
        }, 

"variables": {
        existingVnetName":if


Solution 1:[1]

As per you requirement you can use below ARM TEMPLATE and can able to populate the subnets when we select US VNET or UK VNET accordingly.

ARM TEMPLATE:

{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "existingVnetName": {
            "type": "string",
            "metadata": {
                "description": "The name of the virtual network the VMs will be connected to."
            },
            "allowedValues": [
                "AVD-US V-Net",
                "AVD-UK V-Net"
            ]
        }
    },
    "variables": {
      "ussubnet" : [
                "avd-us-subnet1",
                "avd-us-subnet2"
      ],
      "uksubnet" :[
                "avd-uk-subnet1",
                "avd-uk-subnet2"
      ],
      "existingSubnetName": "[if(contains(parameters('existingVnetName'),'US'),variables('ussubnet'),variables('uksubnet'))]"
    },
    "resources": [],
    "outputs": {
        "existingSubnet": {
            "type": "array",
            "value": "[variables('existingSubnetName')]"
        }
    }
}

OUTPUT:- enter image description here

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AjayKumarGhose-MT