【问题标题】:Azure arm output the IPs of an app serviceAzure arm 输出应用服务的 IP
【发布时间】:2021-09-20 05:41:17
【问题描述】:

我在 azure 中使用 ARM 模板从应用服务获取可能的OutboundIpAddresses,并使用它在 mysql 服务器中创建允许防火墙规则

下面是我用来获取ips的参数

"parameters": {
        "webAppOutboundIpAddresses": {
            "value": "[split(reference(concat('Microsoft.Web/sites/',parameters('wpsitename'))).properties.possibleOutboundIpAddresses,',')]"

以下是链接模板中使用的一段代码。

            "type": "Microsoft.DBforMySQL/servers/firewallRules",
        "apiVersion": "2017-12-01",
        "name": "[concat(parameters('sqlServerName'), '/Allow WebApp Outbound IP ',copyIndex('webAppOutboundIPAddressesCopy'))]",
        
         "properties": {
            "startIpAddress": "[parameters('webAppOutboundIpAddresses')[copyIndex('webAppOutboundIPAddressesCopy')]]",
            "endIpAddress": "[parameters('webAppOutboundIpAddresses')[copyIndex('webAppOutboundIPAddressesCopy')]]"
        },
        "copy": {
            "name": "webAppOutboundIPAddressesCopy",
            "count": "[length(parameters('webAppOutboundIpAddresses'))]"

但不知何故,管道出现故障,我收到无效参数错误

InvalidParameterValue",
    "message": "Invalid value given for parameter '{0}'. Specify a valid parameter value."

我注意到的另一件事是输出 IP 显示在方括号内,例如 ["192.168.1.2"]

有人可以解释一下吗?谢谢

【问题讨论】:

    标签: azure azure-web-app-service azure-resource-manager arm-template


    【解决方案1】:

    如果资源在同一个模板中,你必须使用reference()函数并传递资源id或者只传递名称:

    {
        "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "appServiceName": {
                "type": "string",
                "minLength": 1,
                "metadata": {
                    "description": "Specifies the name of the Azure App Service"
                }
            },
            "appServicePlanName": {
                "type": "string",
                "minLength": 1
            }
        },
        "variables": {
        },
        "resources": [
            {
                "apiVersion": "2015-08-01",
                "name": "[parameters('appServiceName')]",
                "type": "Microsoft.Web/sites",
                "kind": "app",
                "location": "[resourceGroup().location]",
                "dependsOn": [],
                "properties": {
                    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]",
                    "clientAffinityEnabled": false
                },
                "resources": [],
            }
        ],
        "outputs": {
            "appServiceName": {
                "type": "string",
                "value": "reference(parameters('appServiceName'), '2016-03-01', 'Full').properties.inboundIpAddress"
            },
            "ipAddress": {
                "type": "string",
                "value": "whatingodsnamegoeshere"
            }
        }
    }
    

    或 使用资源 ID:

    reference(resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName')), '2016-03-01', 'Full').properties.inboundIpAddress
    

    出站IP地址:参考this

    "parameters": {
                "webAppOutboundIpAddresses": {
                    "value": "[split(reference(concat('Microsoft.Web/sites/',variables('webAppName'))).possibleOutboundIpAddresses,',')]"
                },
    

    Web 应用程序可能的出站 IP 地址是 IP 地址列表,因此我们需要创建一些循环来遍历列表并将这些 IP 地址添加到 SQL 防火墙中。在 ARM 模板中,可以使用 copy element。要获取 WebApp 的对象,我们需要使用reference function。但是引用函数有一个限制——“你不能在复制循环中使用引用函数来设置 count 属性的值”。

    【讨论】:

    • 如果回答对您有帮助,请Accept it as an Answer,以便遇到相同问题的其他人可以找到此解决方案并解决他们的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2020-11-10
    相关资源
    最近更新 更多