Azure Bicep:如果属性不存在则设置空值

问题描述 投票:0回答:2

我正在通过 bicep 部署 azure 更新管理服务的计划。我的代码如下:

param parSchedules array = [
  {
    name: 'mysched1'
    monthlyOccurrencesDay: null
    monthlyOccurrencesOccurence: null
    DaysOfWeek: 'Wednesday'
    StartTime: '${parBaseTimeForUpdateSchedules}T19:00:00'
    tag: {
      Update: [
        'tag1'
      ]
    }
  }
  {
    name: 'mysched2'
    monthlyOccurrencesDay: 'Wednesday'
    monthlyOccurrencesOccurence: 1
    DaysOfWeek: 'Wednesday'
    StartTime: '${parBaseTimeForUpdateSchedules}T07:00:00'
    tag: {
      Update: [
        'tag2'
      ]
    }
  }
]


resource resUpdateschedule 'Microsoft.Automation/automationAccounts/softwareUpdateConfigurations@2019-06-01' = [for schedule in parSchedules: {
  name: schedule.name
  parent: resAutomationAccount
  properties: {
    scheduleInfo: {
      advancedSchedule: {
        monthlyOccurrences: [
          {
            day: schedule.monthlyOccurrencesDay
            occurrence: schedule.monthlyOccurrencesOccurence
          }
        ]
        weekDays: [
          schedule.DaysOfWeek
        ]
      }
      description: ''
      frequency: 'Week'
      interval: 1
      isEnabled: true
      startTime: schedule.StartTime
      timeZone: 'UTC'
    }
    updateConfiguration: {
      duration: 'PT2H'
      operatingSystem: 'Windows'
      targets: {
        azureQueries: [
          {
            locations: [
              parLocation
            ]
            scope: [
              subscription().id
            ]
            tagSettings: {
              tags: schedule.tag
            }
          }
        ]
      }
      windows: {
        includedUpdateClassifications: 'Critical, Security'
        rebootSetting: 'IfRequired'
      }
    }
  }
}]

我收到错误,因为 monthlyOccurrencesDay 和monthlyOccurrencesOccurence 不接受空值。所以我想要的是能够通过循环相同的资源来使用包含不同类型的计划的相同列表(有或没有monthlyOccurrencesOccurence)。就像 如果 MonthlyOccurrencesOccurence 的值为 null,则不应考虑此属性。 这可能吗?

azure azure-resource-manager azure-bicep
2个回答
6
投票

您应该能够有条件地设置每月发生的次数:

resource resUpdateschedule 'Microsoft.Automation/automationAccounts/softwareUpdateConfigurations@2019-06-01' = [for schedule in parSchedules: {
  name: schedule.name
  ...
  properties: {
    scheduleInfo: {
      advancedSchedule: {
        monthlyOccurrences: schedule.monthlyOccurrencesOccurence != null ? [
          {
            day: schedule.monthlyOccurrencesDay
            occurrence: schedule.monthlyOccurrencesOccurence
          }
        ] : []
        ...
      }
      ...
    }
    ...
  }
}]


0
投票

您可以使用

contains
功能。

https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions-object#contains

包含(每月发生次数,'每月发生次数')。如果monthlyOccurrences包含名为monthlyOccurrences的属性,这将返回true。

© www.soinside.com 2019 - 2024. All rights reserved.