gpt4 book ai didi

azure - 我可以将 Azure 资源的现有状态与 Azure Bicep 或 Pulumi 文件合并吗?

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

我目前正在使用一个解决方案,该解决方案使用通过 ARM/Bicep 部署的 Azure 应用程序网关。随着时间的推移,会部署使用此 AppGw 的其他应用程序,因此在部署时通过 Az CLI(在中央基础设施 IaC 管道/流程之外)为这些应用程序创建规则/后端池/监听器。当谈到重新部署/更新中央 AppGw 时,我遇到了 ARM/Bicep 模板覆盖所有这些额外添加内容的经典问题,因为 AppGw 是单个资源,并且更改不在 ARM/Bicep 文件中已删除。

我过去通过检查 AppGw 是否存在、输出现有规则/池/等来解决这个问题。然后在重新部署之前将它们合并到 ARM/Bicep JSON 中。这工作得很好,但 AppGw 现在变得如此之大/复杂,以至于我在通过 Azure Devops 构建管道部署更新时遇到了 Bash 字符限制。因此,我正在寻找更好的方法来处理这个问题。我还尝试将现有配置输出到文件并通过 Azure Bicep 中的文件加载进行摄取,但我需要使用不同的配置在全局部署多个 AppGw,因此由于 Bicep 中的编译时文件引用限制,这对我不起作用.

我需要确保以某种方式遵守我的 AppGw 基线模板文件(该文件设置 TLS 级别或诊断设置等核心内容),同时不会覆盖单独部署过程中发生的修改。

我的问题是,我是否可以将现有 AppGw 的状态与我的基线模板合并/合并,可以使用 Azure Bicep,也可以重新工具到 Pulumi/Terraform 之类的东西(如果这会公开功能)。我想到的方法是:

  • 管道 CLI 任务检查 AppGw 是否已存在
  • 如果否,则使用具有基本要求的基线模板进行部署
  • 如果是,则获取现有的后端池/监听器/等。 (或获取整体状态)
  • 与模板 IaC 文件比较
  • 合并状态,确保应用 IaC 文件中的核心设置(即诊断设置、TLS 级别等),同时现有后端池/监听器等。被保留

我知道,但没有经验,Pulumi 的忽略更改和转换的概念。我不确定这是否涵盖了这里的用例。我在这里试图实现的目标可能与这些声明性语言的目的相冲突,但我只是想看看其他人是否有任何想法。

提前非常感谢!

最佳答案

使用 Bicep,如果应用程序网关已存在,您始终可以检索现有配置。以下是使用 httpListeners 的示例。

您可以像这样定义 app-gateway.bicep 模块:

param appGatewayName string
param location string = resourceGroup().location
...
param httpListeners array

resource appGateway 'Microsoft.Network/applicationGateways@2020-11-01' = {
name: appGatewayName
location: location
...
properties: {
...
httpListeners: httpListeners
}
}

然后从您的 main.bicep 文件中,使用默认或现有配置:

param appGateWayExists bool
param appGatewayName string
...

// Get existing app gateway if existing
resource existing 'Microsoft.Network/applicationGateways@2020-11-01' existing = if (appGateWayExists) {
name: appGatewayName
}

// Deploy app gateway
module appgateway './app-gateway.bicep' = {
name: 'app-gateway'
params: {
appGatewayName: appGatewayName
...
// Use existing configuration if exists
httpListeners: appGateWayExists ? existing.properties.httpListeners : [
{
// default listener configuration
}
]
}
}

然后你可以像这样调用它:

$appGatewayName = "<app-gateway-name>"
$appGatewayExists = (az resource list --name "$appGatewayName" --query '[].[id]' | ConvertFrom-Json).Length -gt 0
az deployment group create `
--resource-group "<resource-group-name>" `
...
--parameters `
appGateWayExists=$appGatewayExists `
appGatewayName="$appGatewayName" `
...

关于azure - 我可以将 Azure 资源的现有状态与 Azure Bicep 或 Pulumi 文件合并吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72991904/

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