简体   繁体   中英

How to give default value to resource group field in ARM template deployment?

I have sample ARM template given below. The fields subscription, resource group and location are provided in first section ARM template to user while deploying and parameters section is provided after this. Resource group is drop down field provided by Azure ARM itself, where I need to provide any one resource group as default resource group present in list. How can this possible?

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "writePermission": {
            "defaultValue": "No",
            "allowedValues": [
                "No",
                "Yes"
            ],
            "type": "String",
            "metadata": {
                "description": "the permission type"
            }
        }
    },
    "variables": {
        "location": "[resourceGroup().location]",
        "templateUri": "[deployment().properties.templateLink.uri]"
    },
    "resources": [
    ],
    "outputs": {
        "result": {
            "type": "String",
            "value": "[variables('templateUri')]"
        }
    }
}   

This is how template is rendered.

自定义部署

Expected solution : Instead of blank value in resource group, it should be pre-populated with first field from drop-down of resource group.

A deployment with ARM templates is always providing the deployment resource group outside the template. A deployment is always based in a resource group that you provide as a parameter outside the template. The name of the command to deploy using PS is: New-AzResourceGroupDeployment indicating that you have the provided resource group as a base and the argument to choose the Resource Group is named: -ResourceGroup

So basically you can't choose the base resource group inside your ARM template since that's provided outside for the API to know where to start the deployment.

This is the PS command to do the deployment:

New-AzResourceGroupDeployment
   [-Name <String>]
   -ResourceGroupName <String>
   [-Mode <DeploymentMode>]
   [-DeploymentDebugLogLevel <String>]
   [-RollbackToLastDeployment]
   [-RollBackDeploymentName <String>]
   [-Tag <Hashtable>]
   [-WhatIfResultFormat <WhatIfResultFormat>]
   [-WhatIfExcludeChangeType <String[]>]
   [-Force]
   [-AsJob]
   -TemplateFile <String>
   [-SkipTemplateParameterPrompt]
   [-ApiVersion <String>]
   [-Pre]
   [-DefaultProfile <IAzureContextContainer>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

The PS command is using the ARM Rest API which is the same API used by the Azure Portal, looking at the Rest API you can also see that ResourceGroup is a parameter that needs to be provided outside the ARM template:

https://learn.microsoft.com/en-us/rest/api/resources/deployments/createorupdate

So what I'm saying is that you can't use the template to manipulate the drop down box for the base Resource Group, the only way you can do this is by limiting the access for the users using RBAC so that the user only can see the resource groups the user is supposed to be able to deploy to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM