Azure Resource Manager: How to specify optional parameters in linked templates

倾然丶 夕夏残阳落幕 提交于 2020-01-03 19:16:46

问题


I'm making use of linked templates to deploy common resources. In this case I'm deploying a VM which has an optional parameter defined AdminPassword that is only required in certain scenarios (namely when the parameter PasswordAuthenticationDisabled is set to 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"
        }
    }
}

I'm referencing the linked template as follows:

{
    "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
            }
        }
    }
}

Without the optional parameter specified. This leads to ARM complaining about the missing parameter: 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.

How can I tell the calling template to honor the optionality of the parameter and just go with the default value?


回答1:


Set the defaultValue to something other than null, e.g. an empty string. For this scenario, you can also do something like this: https://github.com/Azure/azure-quickstart-templates/blob/master/100-marketplace-sample/azuredeploy.json#L36



来源:https://stackoverflow.com/questions/55869155/azure-resource-manager-how-to-specify-optional-parameters-in-linked-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!