在 ARM 模板中,参数和变量都是用来传递值的。但是,它们的用途和生命周期有所不同。
参数(parameters)是输入到模板中的值,可以在部署模板时动态地进行设置。例如,可以将虚拟机大小、存储帐户名称等作为参数传递到模板中。在 ARM 模板中,参数是一个 JSON 对象,可在参数部分中定义,如下所示:
"parameters": { "vmName": { "type": "string", "defaultValue": "myvm", "metadata": { "description": "Name of the VM" } }, "vmSize": { "type": "string", "defaultValue": "Standard_DS1_v2", "metadata": { "description": "Size of the VM" } } }
变量(variables)是模板内部使用的值,可在不同部分中使用。例如,在创建虚拟网络时,可以在多个部分中使用相同的 IP 地址前缀。在 ARM 模板中,变量也是一个 JSON 对象,可在变量部分中定义,如下所示:
"variables": { "addressPrefix": "10.0.0.0/16" }
可以使用 ${{variables.variablename}} 语法将变量引用到模板的其他部分中,如下所示:
"resources": [ { "name": "[concat(parameters('vmName'), '-nsg')]", "type": "Microsoft.Network/networkSecurityGroups", "location": "[resourceGroup().location]", "properties": { "securityRules": [ { "name": "SSH", "properties": { "protocol": "Tcp", "sourcePortRange": "", "destinationPortRange": "22", "sourceAddressPrefix": "", "destinationAddressPrefix": "[concat(variables('addressPrefix'), '/28')]", "access": "Allow", "