Using conditional statement in a json

Copper Contributor

I am attempting to do conditional statement inside a .json file. I am finding that when a condition is met, the other conditions are ignored. It will only look a the first condition in my file whether it be true or false. Not sure what I am doing wrong or if this is just how json works. Thanks.

 

{

    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",

    "contentVersion": "1.0.0.0",

    "parameters": {

        "subnetName": {

            "type": "String"

        },

        "subnetAddress": {

            "type": "String"

        },

        "virtualNetworkName": {

          "type": "String"

        },

        "networkSecurityGroupID": {

            "type": "String"

        },

        "routeTableID": {

            "type": "String"

        }

    },

    "variables": {},

    "resources": [

        {

            "condition": "[and(equals(parameters('networkSecurityGroupID'),'N/A'), equals(parameters('routeTableID'),'N/A'))]",

            "type": "Microsoft.Network/virtualNetworks/subnets",

            "apiVersion": "2020-08-01",

            "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName'))]",

            "dependsOn": [],

            "properties": {

                "addressPrefix": "[parameters('subnetAddress')]",

                "serviceEndpoints": [],

                "delegations": [],

                "privateEndpointNetworkPolicies": "Enabled",

                "privateLinkServiceNetworkPolicies": "Enabled"

            }

        },

        {

            "condition": "[and(not(equals(parameters('networkSecurityGroupID'),'N/A')), not(equals(parameters('routeTableID'),'N/A')))]",

            "type": "Microsoft.Network/virtualNetworks/subnets",

            "apiVersion": "2020-08-01",

            "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName'))]",

            "dependsOn": [],

            "properties": {

                "addressPrefix": "[parameters('subnetAddress')]",

                "networkSecurityGroup": {

                    "id": "[parameters('networkSecurityGroupID')]"

                },

                "routeTable": {

                    "id": "[parameters('routeTableID')]"

                },

                "serviceEndpoints": [],

                "delegations": [],

                "privateEndpointNetworkPolicies": "Enabled",

                "privateLinkServiceNetworkPolicies": "Enabled"

            }

        },

        {

            "condition": "[and(equals(parameters('networkSecurityGroupID'),'N/A'), not(equals(parameters('routeTableID'),'N/A')))]",

            "type": "Microsoft.Network/virtualNetworks/subnets",

            "apiVersion": "2020-08-01",

            "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName'))]",

            "dependsOn": [],

            "properties": {

                "addressPrefix": "[parameters('subnetAddress')]",

                "routeTable": {

                    "id": "[parameters('routeTableID')]"

                },

                "serviceEndpoints": [],

                "delegations": [],

                "privateEndpointNetworkPolicies": "Enabled",

                "privateLinkServiceNetworkPolicies": "Enabled"

            }

        },

        {

            "condition": "[and(not(equals(parameters('networkSecurityGroupID'),'N/A')), equals(parameters('routeTableID'),'N/A'))]",

            "type": "Microsoft.Network/virtualNetworks/subnets",

            "apiVersion": "2020-08-01",

            "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName'))]",

            "dependsOn": [],

            "properties": {

                "addressPrefix": "[parameters('subnetAddress')]",

                "networkSecurityGroup": {

                    "id": "[parameters('networkSecurityGroupID')]"

                },

                "serviceEndpoints": [],

                "delegations": [],

                "privateEndpointNetworkPolicies": "Enabled",

                "privateLinkServiceNetworkPolicies": "Enabled"

            }

        }

    ]

}

 

0 Replies