Azure资源管理器:如何在链接模板中指定可选参数

问题描述 投票:1回答:1

我正在使用链接模板来部署公共资源。在这种情况下,我正在部署一个VM,它有一个可选参数定义的AdminPassword,仅在某些情况下需要(即当参数PasswordAuthenticationDisabled设置为false时):

"parameters": {
    "AdminPassword": {
        "type": "securestring",
        "defaultValue": null,
        "metadata": {
            "description": "Password when password-based authentication isn't disabled"
        }
    },
    "PasswordAuthenticationDisabled": {
        "type": "bool",
        "defaultValue": "true",
        "metadata": {
            "description": "Should password-based authentication thorugh SSH be disabled"
        }
    }
}

我正在引用链接模板,如下所示:

{
    "type": "Microsoft.Resources/deployments",
    "name": "[variables('nameDeploymentVmAttacker1')]",
    "apiVersion": "2017-05-10",
    "properties": {
        "mode": "Incremental",
        "templateLink": {
            "uri": "[concat(variables('urlTemplates'), '/vm/ubuntu-18.04.json')]"
        },
        "parameters": {
            "Name": {
                "value": "[variables('nameVmAttacker1')]"
            },
            "Region": {
                "value": "[resourceGroup().location]"
            },
            "AdminUsername": {
                "value": "[parameters('AdminUsername')]"
            },
            "AdminSshKey": {
                "value": "[parameters('AdminSshKey')]"
            },
            "VmSize": {
                "value": "[parameters('VmSize')]"
            },
            "VnetName": {
                "value": "[variables('nameVnet')]"
            },
            "PasswordAuthenticationDisabled": {
                "value": true
            }
        }
    }
}

没有指定可选参数。这导致ARM抱怨缺少的参数:Deployment template validation failed: 'The value for the template parameter 'AdminPassword' at line '25' and column '26' is not provided. Please see https://aka.ms/arm-deploy/#parameter-file for usage details.

如何判断调用模板是否符合参数的可选性,只是使用默认值?

azure azure-resource-manager arm-template
1个回答
0
投票

将defaultValue设置为null以外的值,例如一个空字符串。对于这种情况,您还可以执行以下操作:https://github.com/Azure/azure-quickstart-templates/blob/master/100-marketplace-sample/azuredeploy.json#L36

© www.soinside.com 2019 - 2024. All rights reserved.