gpt4 book ai didi

azure - Bicep 将出站 IP 地址添加到多个 Web 应用程序,尽管有 if 语句

转载 作者:行者123 更新时间:2023-12-03 06:16:09 25 4
gpt4 key购买 nike

出于某种原因,我的二头肌部署模板将出站 IP 地址部署到所有 Web 应用程序,尽管逻辑表明它应该仅在迭代参数文件中的特定参数时执行此操作。

来自主二头肌文件:

module WebApps 'webapps.bicep' = [for WebAppConfig in WebAppDeployments: {
name: 'WebApp${WebAppConfig.NamingSuffix}'
dependsOn: [
applicationInsights
sqlServer
]
scope: resourceGroup('${NamePrefix}-RG-1')
params: {
NamePrefix: NamePrefix
NamePrefixInfra: NamePrefixInfra
Tags: Tags
Location: Location
dockerImageName: dockerImageName
WebAppConfig: WebAppConfig
}
}]

// Azure SQL Whitelists

module SQLServerWhitelistIPs 'sqlWhitelist.bicep' = [for (WebAppConfig, index) in WebAppDeployments: if (WebAppConfig=='BACKEND') {
name: 'SqlServerWhitelists${index}'
dependsOn: [
WebApps
sqlServer
]
scope: resourceGroup('${NamePrefix}-RG-1')
params: {
NamePrefix: NamePrefix
WebAppConfig: WebAppConfig
WebAppIps: WebApps[index].outputs.WebAppIps
}

在 webapp 模块中,我在最后放置:

output WebAppIps array = split(WebApp.properties.possibleOutboundIpAddresses, ',')

在参数文件中:

 "WebAppDeployments": {
"value": [
{
"NamingSuffix": "FRONTEND"
},
{
"NamingSuffix": "BACKEND"
},
{
"NamingSuffix": "CMS"
}
]
}

问题是,它不是创建三个 Web 应用程序,其中只有一个将出站 IP 地址列入白名单,而是填充所有三个 Web 应用程序。我不确定我在这里做错了什么。

我尝试更改主二头肌文件:

module SQLServerWhitelistIPs 'sqlWhitelist.bicep' = [for (WebAppConfig, index) in WebAppDeployments: if (WebAppConfig=='BACKEND') {
name: 'SqlServerWhitelists${index}'
dependsOn: [
WebApps
sqlServer
]
scope: resourceGroup('${NamePrefix}-RG-1')
params: {
NamePrefix: NamePrefix
WebAppConfig: WebAppConfig
WebAppIps: WebApps[index].outputs.WebAppIps
}
}]

module SQLServerWhitelistIPs 'sqlWhitelist.bicep' = [for (WebAppConfig, index) in WebAppDeployments: if (WebAppConfig.NamingSuffix=='BACKEND')

但无济于事。它仍然填充每个网络应用程序,而不仅仅是后端应用程序。

最佳答案

可能的原因之一是 output block 。您可以通过添加 Backend webapp 的条件来修改 output block ,如下所示:

output WebAppIps array = if (WebAppConfig.NamingSuffix == 'BACKEND') { split(WebApp.properties.possibleOutboundIpAddresses, ',') }

此外,您需要使用 Namingsuffix 传递条件,而不是传递 if (WebAppConfig=='BACKEND')。将其修改为 if(WebAppConfig.NamingSuffix=='BACKEND') 因为 WebAppConfig 对象包含一个名为 NamingSuffix 的属性,用于不同的 webapp 参数。

然后您可以过滤 SQLServerWhitelistIPs 模块中的 WebAppDeployments 数组,以仅包含后端 Web 应用程序。

module SQLServerWhitelistIPs 'sqlWhitelist.bicep' = [for (WebAppConfig, index) in WebAppDeployments:  if (WebAppConfig.NamingSuffix == 'BACKEND') 
{
name: 'SqlServerWhitelists${index}'
dependsOn: [
WebApps
sqlServer
]
scope: resourceGroup('RG')
params: {
NamePrefix: NamePrefix
WebAppConfig: WebAppConfig
WebAppIps: WebApps[index].outputs.WebAppIps } }]

检查完上述内容后,我修改了您的代码并能够成功部署。

enter image description here

enter image description here

关于azure - Bicep 将出站 IP 地址添加到多个 Web 应用程序,尽管有 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76236392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com