如何在ARM模板部署中为资源组字段赋予默认值?

时间:2020-08-20 09:17:17

标签: azure arm-template

我在下面提供了示例ARM模板。在第一部分ARM模板中为用户提供了部署时的字段订阅,资源组和位置,并在部署之后提供了参数部分。资源组是由Azure ARM本身提供的下拉字段,我需要在其中提供任何一个资源组作为列表中存在的默认资源组。这怎么可能?

{
    "$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')]"
        }
    }
}   

这是呈现模板的方式。

custom deployment

期望的解决方案: 应该使用资源组下拉列表中的第一个字段来预先填充资源组中的空白值。

1 个答案:

答案 0 :(得分:2)

带有ARM模板的部署始终在模板之外提供部署资源组。部署始终基于您在模板外部作为参数提供的资源组。使用PS进行部署的命令的名称为:New-AzResourceGroupDeployment,指示您已将提供的资源组作为基础,并且选择资源组的参数名为:-ResourceGroup

因此,基本上您不能在ARM模板中选择基础资源组,因为API外部提供了该基础资源组,以了解从何处开始部署。

这是执行部署的PS命令:

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>]

PS命令​​使用的ARM Rest API与Azure门户使用的API相同,在查看Rest API时,您还可以看到ResourceGroup是需要在ARM模板之外提供的参数:

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

所以我的意思是,您不能使用模板来操纵基本资源组的下拉框,唯一的方法是限制使用RBAC的用户的访问权限,用户只能看到该用户应该能够部署到的资源组。