Home > Back-end >  Azure ARM Template - conditional input parameter
Azure ARM Template - conditional input parameter

Time:11-30

In Short:

Is it possible to have condition field (or something else to achieve this functionality), inside a parameter, so that this parameter input will be asked to the user, only based on another parameter's value.

i.e., Only if user selects Create New on a parameter, the input for the name of that parameter should be asked.


In Detail:

I have a parameter virtualNetworkCreateNewOrUseExisting which will accept two values - Create New and Use Existing.

"parameters": {
    "virtualNetworkCreateNewOrUseExisting": {
        "type": "string",
        "defaultValue": "Create New",
        "allowedValues": [
            "Create New",
            "Use Existing"
        ]
    },
    // Other parameters
}

I am trying to create a Virtual Network based on an input from user. If user selects Create New, it will be created, and if they select Use Existing, it will be skipped. I see that this is achievable by using condition field inside the resource.

"resources": [
    {
        "condition": "[equals(parameters('virtualNetworkCreateNewOrUseExisting'), 'Create New')]",
        // Other fields
    }
    // Other resources
]

Now, my question here is that,

Similar to this, is it possible to have condition field, inside another parameter, so that this parameter input will be asked to the user, only based on the previous parameter value.

i.e., Only if user selects Create New, the input for Virtual Network Name should be asked.

Something like this, by using a condition field:

"parameters": {
    "virtualNetworksName": {
        "condition": "[equals(parameters('virtualNetworkCreateNewOrUseExisting'), 'Create New')]",
        "defaultValue": "vn-1",
        "type": "string"
    },
    // Other parameters
}

But, I see that condition field, is not supported inside parameters (at least as of now).

Or, is it at least possible to have, if statements, like this (So that, the value of the field will be displayed empty by default if user selects Use Existing.):

"parameters": {
    "virtualNetworksName": {
        "defaultValue": "[if(equals(parameters('virtualNetworkCreateNewOrUseExisting'),'Create New'), 'vn-1', '')]",
        "type": "string"
    },
    // Other parameters
}

Or, any other way to achieve this goal?

CodePudding user response:

As per the current Azure documentation, we cannot add conditions to parameters in parameters block.

As a side note you can use PowerShell parameter inline functions while deploying the template.

We see that there is a feature request already in place to add Support functions within the definition of parameters... · Community (azure.com) We would suggest you to make a comment & Upvote on the exiting feedback request .

CodePudding user response:

You can't do this natively in the template file, but in the portal user experiences for deploying the template you can... See:

https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-specs-create-portal-forms

and

https://docs.microsoft.com/en-us/azure/azure-resource-manager/managed-applications/create-uidefinition-overview

You don't have to use a templateSpec or managedApp (see the "Deploy to Azure button here: https://github.com/Azure/azure-quickstart-templates/tree/master/demos/100-marketplace-sample )

but templateSpec or managedApp will give you a better experience if that's an option.

  • Related