我试图让资源azurerm_automation_schedule在每月发生的特定时间部署(ex: 18:00)。
我使用以下代码:
locals {
update_time = "18:00"
update_date = formatdate("YYYY-MM-DD", timeadd(timestamp(), "24h"))
update_timezone = "UTC"
}
resource "azurerm_automation_schedule" "main" {
name = "test"
resource_group_name = "myresourcegroupname"
automation_account_name = "myautomationaccountname"
frequency = "Month"
timezone = local.update_timezone
start_time = "${local.update_date}T${local.update_time}:00+02:00"
description = "This is an example schedule"
monthly_occurrence {
day = "Tuesday"
occurrence = "1"
}
}"${local.update_date}T${local.update_time}:00+02:00"将当前时间增加2小时,并将日期提前1天。这是确保计划在未来开始的必要条件。
这很好,除非下次我回来运行部署时,它会检测到由于日期更改而发生的新更改,即使没有发生真正的更改。start_time将始终向前移动。
我似乎找不到任何能提供帮助的地形逻辑。是否有一种方法可以在变量中设置静态启动时间,并且只有在发生更改时才更新它?(不是日期)。
psuedocode是:
if [update_time] has not changed, do not update [azurerm_automation_schedule]
else update [azurerm_automation_schedule] with the new time, incrementing the day forward更新
我的最后工作代码(额外的好处:使用windows更新调度程序,这是一个痛苦的工作!)
//== Provider used to store timestamp for updates ==//
provider "time" {
version = "~> 0.4"
}
//== Store 1 day in the future, only update if [local.update_time] is altered ==//
resource "time_offset" "next_day" {
offset_days = 1
triggers = {
update_time = local.update_time
}
}
locals {
update_time = "19:40"
update_date = substr(time_offset.next_day.rfc3339, 0, 10)
update_timezone = "UTC"
update_max_hours = "4"
update_classifications = "Critical, Security, UpdateRollup, ServicePack, Definition, Updates"
update_reboot_settings = "IfRequired"
update_day = "Tuesday"
update_occurrence = "2"
}
#This type should eventually replace the manual deploy via azurerm: azurerm_automation_softwareUpdateConfigurations
#https://github.com/terraform-providers/terraform-provider-azurerm/issues/2812
resource "azurerm_template_deployment" "windows" {
name = "windows-update"
resource_group_name = module.stack.azurerm_resource_group.name
template_body = <<DEPLOY
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"apiVersion": "2017-05-15-preview",
"type": "Microsoft.Automation/automationAccounts/softwareUpdateConfigurations",
"name": "${module.stack.azurerm_automation_account.name}/windows-updates",
"properties": {
"updateConfiguration": {
"operatingSystem": "Windows",
"duration": "PT${local.update_max_hours}H",
"windows": {
"excludedKbNumbers": [
],
"includedUpdateClassifications": "${local.update_classifications}",
"rebootSetting": "${local.update_reboot_settings}"
},
"azureVirtualMachines": [
"${module.server_1.azurerm_virtual_machine.id}",
"${module.server_2.azurerm_virtual_machine.id}"
],
"nonAzureComputerNames": [
]
},
"scheduleInfo": {
"frequency": "Month",
"startTime": "${local.update_date}T${local.update_time}:00",
"timeZone": "${local.update_timezone}",
"interval": 1,
"advancedSchedule": {
"monthlyOccurrences": [
{
"occurrence": "${local.update_occurrence}",
"day": "${local.update_day}"
}
]
}
}
}
}
]
}
DEPLOY
deployment_mode = "Incremental"
}发布于 2020-05-13 04:34:31
它不断计划更改的原因是,正如所编写的代码所指的是当前时间,而不是获取“明天”并以某种方式跟踪它。
要做到这一点,你需要一个方法来获得“明天”一次,并坚持在州。生活在状态中的东西是资源,所以您需要一个表示带有偏移量的时间的资源。这就是 provider出现的原因。
这是最基本的部分:
resource "time_offset" "tomorrow" {
offset_days = 1
}这将为您获得“明天”,在应用程序之后,它将保存在Terraform状态中。
time_offset.tomorrow.rfc3339将评估如下:
2020-05-13T04:28:07Z但是,我们只需要其中的YYYY DD,所以我们使用substr来获得前10个字符:
substr(time_offset.tomorrow.rfc3339, 0, 10)综上所述,我们得到了以下内容(添加了4行,包括空格、1行更改):
locals {
update_time = "18:00"
update_date = substr(time_offset.tomorrow.rfc3339, 0, 10)
update_timezone = "UTC"
}
resource "time_offset" "tomorrow" {
offset_days = 1
}
resource "azurerm_automation_schedule" "main" {
name = "test"
resource_group_name = "myresourcegroupname"
automation_account_name = "myautomationaccountname"
frequency = "Month"
timezone = local.update_timezone
start_time = "${local.update_date}T${local.update_time}:00+02:00"
description = "This is an example schedule"
monthly_occurrence {
day = "Tuesday"
occurrence = "1"
}
}您可能需要引入time提供程序来使用它(如果没有它,则将它与您的AzureRM提供程序放在一起):
provider "time" {}如果需要,可以使用terraform taint 'time_offset.tomorrow'强制重新计算时间。
发布于 2020-05-27 15:01:50
分享回来。我从这个线程中的信息创建了一个terraform模块,以简化调度更新。同时适用于linux和windows VM:
https://github.com/canada-ca-terraform-modules/terraform-azurerm_update_management
下面是一个示例,说明如何使用该模块来完成您想要的任务:
locals {
update_time = "18:00"
update_date = substr(time_offset.tomorrow.rfc3339, 0, 10)
update_timezone = "UTC"
}
resource "time_offset" "tomorrow" {
offset_days = 1
}
module "linux-weekly-updates" {
source = "github.com/canada-ca-terraform-modules/terraform-azurerm_update_management?ref=20200527.1"
name = "test"
resource_group_name = "my_resource_group_name"
azurerm_automation_account = azurerm_automation_account.my_azurerm_automation_account
operatingSystem = "Linux"
scope = [azurerm_resource_group.somerg1.id, azurerm_resource_group.somerg1.id]
timeZone = "EST"
startTime = "${local.update_date}T${local.update_time}:00+02:00"
weekDays = ["Sunday"]
}https://stackoverflow.com/questions/61762648
复制相似问题