【问题标题】:parsing variables in Azure ARM template file解析 Azure ARM 模板文件中的变量
【发布时间】:2020-03-11 13:47:12
【问题描述】:

我正在编写用于部署 VNET 的 Azure ARM 模板。因此我有一个部署文件“VNET.json”和一个参数文件“VNET.parameter.json。

现在我只想在参数文件中交出一个变量,但总是出错:

'The template variable 'IP' is not found. Please see https://aka.ms/arm-template/#variables for usage details.

或者是不是“允许”在参数中使用变量?

这是我的 json 文件。

{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "variables": {
        "IP": "172.37.0.0/24"
    },
    "parameters": {
        "VNetSettings":{
            "value":{
                "name":"ManagementNW",
                "addressPrefixes": [
                    {
                        "name": "VPN",
                        "addressPrefix": "192.168.0.0/16"
                    },
                    {
                        "name": "Control",
                        "addressPrefix": "172.37.0.0/16"
                    },
                    {
                        "name": "Service",
                        "addressPrefix": "172.36.0.0/16"
                    },
                    {
                        "name": "DMZ",
                        "addressPrefix": "172.35.4.0/24"
                    }
                ],
                "subnets":[
                    {
                        "name": "VPN-subnet",
                        "addressPrefix": "192.168.1.0/24"
                    },
                    {
                        "name": "Control-subnet",
                        "addressPrefix": "172.37.0.0/24"
                    },
                    {
                        "name": "Service-subnet",
                        "addressPrefix": "172.36.0.0/24"
                    },
                    {
                        "name": "DMZ-subnet",
                        "addressPrefix": "172.35.4.0/24"
                    },
                    {
                        "name": "Gateway-subnet",
                        "addressPrefix": "192.168.255.0/24"
                    },
                    {
                        "name":"AzureFirewall-subnet",
                        "addressPrefix":"192.168.254.0/24"
                    }
                ]
            }
        },
        "Control-NSG-settings": {
            "value": {
                "securityRules": [
                    {
                        "direction": "Inbound",
                        "name": "VPNGW_to_Tableau_all",
                        "sourceAddressPrefix": "192.168.255.0/24",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "172.37.0.41",
                        "destinationPortRange": "*",
                        "protocol": "*",
                        "access": "Allow",
                        "priority": 101,
                        "description": "allow RDP connections"
                    },
                    {
                        "direction": "Inbound",
                        "name": "VPNGW_to_Control_all",
                        "sourceAddressPrefix": "192.168.255.0/24",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "[variables('IP')]",
                        "destinationPortRange": "*",
                        "protocol": "*",
                        "access": "deny",
                        "priority": 102,
                        "description": "deny"
                    },

韩语 马文

【问题讨论】:

  • 参数部分不能使用变量。
  • 另外,您的模板看起来很可疑。它是模板文件还是参数文件?如果是模板,则参数中没有value 部分。如果它是一个参数文件,您可能希望将$schema 更改为here 中提到的参数。参数文件也没有变量部分。

标签: json azure arm-template


【解决方案1】:

在模板中,你reference the value for the parameter by using the variables函数。

在 ARM 模板 json 文件中,您使用变量来简化模板。您无需在整个模板中重复复杂的表达式,而是定义一个包含复杂表达式的变量。但是,如果只使用一次就不需要使用变量了。

另外,正如 usrone 所说,您的模板看起来很可疑。正常的template 就是你可以参考的这个。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "",
  "apiProfile": "",
  "parameters": {  },
  "variables": {  },
  "functions": [  ],
  "resources": [  ],
  "outputs": {  }
}

【讨论】:

  • 嗨,这不是一个部署模板:我复制了错误的标题 - 它是一个参数文件:所以如果我理解正确,我不能在模板文件中使用变量作为参数?
  • 不,您可以像article 所说的那样,在模板文件中使用变量作为参数。
  • 您在variables 中定义变量并在resources 中使用[variables('IP')]
  • 但是我的参数文件中没有资源部分,只有我移交给部署文件的参数
  • resources 在模板文件中。我的意思是您可以在模板文件中使用变量作为参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 2021-01-10
  • 2019-01-18
相关资源
最近更新 更多