Terraform azurerm 计划 start_time 始终在新部署时重置

2024-05-07

我正在尝试获取资源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"
  }
}

The "${local.update_date}T${local.update_time}:00+02:00"在当前时间上添加 2 小时并将日期向前调 1。这是确保日程安排在未来开始所必需的。

这工作正常,除了下次我回来运行部署时,它会检测到由于日期更改而发生的新更改,即使没有发生真正的更改。 start_time 总是向前滴答。

我似乎找不到任何可以提供帮助的地形逻辑。 有没有办法在变量中设置静态开始时间,并且只有在发生变化时才更新它? (不是日期)。

伪代码将是:

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

Update

我的最终工作代码(奖励:使用 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"
}

它不断计划更改的原因是因为您编写的代码指的是当前时间,而不是获取“明天”并以某种方式跟踪它。

为此,您需要一种获得“明天”的方法once,并将其粘在状态下。存在于状态中的事物是资源,因此您需要一个代表带有偏移量的时间的资源。那就是time提供者 https://www.terraform.io/docs/providers/time/r/offset.html进来。

这是最重要的部分:

resource "time_offset" "tomorrow" {
  offset_days = 1
}

这将为您提供“明天”,应用后它将保存在 Terraform 状态中。

time_offset.tomorrow.rfc3339

将评估为:

2020-05-13T04:28:07Z

但是,我们只想要 YYYY-MM-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'如果需要的话,强制重新计算时间。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Terraform azurerm 计划 start_time 始终在新部署时重置 的相关文章

随机推荐

  • C++中exit和kill的区别

    我已经编写了一个信号处理程序来处理SIG 如果我得到的进程太多 我想终止该进程 那么 以下哪个代码更好 或者我应该同时使用它们 exit 1 or some other exit code kill getpid SIGKILL 您可能不想
  • 如何研究.NET 中的非托管内存泄漏?

    我有一个通过 MSMQ 运行的 WCF 服务 内存随着时间的推移逐渐增加 表明存在某种内存泄漏 我在本地运行该服务并使用 PerfMon 监视一些计数器 CLR 内存托管堆字节总数保持相对恒定 而进程的私有字节随着时间的推移而增加 这让我相
  • 如何将现场 prestashop 站点移至本地主机?

    我在将 PS 1 7 从服务器域传输到本地主机时遇到问题 我已按照 Prestashop 文档中的文件传输的所有步骤进行操作 我执行此步骤 1 将所有 prestashop 文件从服务器下载到我的 mac 并将其放入 mamp htdocs
  • iphone XMPP 应用程序运行后台

    我使用 XMPP 框架创建了一个聊天应用程序 当我退出应用程序 进入后台模式 时 我想接收聊天消息 并且还需要显示图标徽章 我该怎么做 您确实可以通过将基于 XMPP 框架的应用程序称为 VoIP 应用程序来在 iOS4 中的后台运行该应用
  • SBT - 运行任务来设置SettingKey

    所以我的一般问题是我想根据任务的结果设置版本密钥 但是版本密钥是在任务运行之前设置的 据我了解 一旦设置了键的值 我就无法更改它 因此我无法在我的任务中更改它 我想要做的是将任务作为发布任务的依赖项运行并更改版本的值 我觉得一定有办法做到这
  • 从列表视图启动活动

    您好 我有一个列表视图 我正在尝试通过以下方式从列表视图启动一项活动startActivity class java public class ll2 extends Activity public void onCreate Bundle
  • dplyr - 分组并选择 TOP x %

    使用 dplyr 包和函数sample frac可以从每个组中抽取一定比例的样本 我需要的是首先对每个组中的元素进行排序 然后从每个组中选择前 x 有一个功能top n 但这里我只能确定行数 并且我需要一个相对值 例如 以下数据按齿轮分组并
  • Python - 在和不在列表中语法错误

    我正在尝试从另一个现有的浮点数列表构建一个新的浮点数列表 通过示例更容易识别第一个列表的预期内容 price list 39 99 74 99 24 99 49 99 预期的后期功能 print new price list gt gt 2
  • java中filewriter的flush和close函数之间的区别

    我需要知道Java中的flush和close函数之间的确切区别是什么 当在写入文件期间将数据转储到文件中时 请提供一个例子 flush just确保所有缓冲数据都写入磁盘 在这种情况下 更一般地说 通过您正在使用的任何 IO 通道刷新 之后
  • Windows 7 VM 上的 Android Studio 虚拟设备不兼容

    我的计算机上有一个 VirtualBox VM 该 VM 运行 Windows 7 64 位 我在该虚拟机上安装了 Android Studio 我只有基本的 Hello World 应用程序 当我尝试运行 AVD 时 我收到以下消息 运行
  • java:在目录和子目录中根据文件名搜索文件

    我需要根据目录树中的名称查找文件 然后显示该文件的路径 我发现了类似的东西 但它根据扩展名进行搜索 谁能帮助我如何根据我的需要重新编写这段代码 谢谢 public class filesFinder public static void m
  • 使用传感器方向

    在我的应用程序中 我想显示设备方向 例如北 南 东 西 为此 我使用加速度计和磁传感器并尝试使用以下代码 public class MainActivity extends Activity implements SensorEventLi
  • 数据库被锁定?

    我如何修复数据库锁 因为我的测试没有通过 它使同一类别中的一堆测试失败 谢谢 1 UsersController GET edit should have a link to change the Gravatar Failure Erro
  • 在 Go 中修改导入的库

    我的问题 弹性节拍 https www elastic co products beats是一个用 Go 编写的日志传送程序的开源项目 它具有多种日志输出功能 包括控制台 Elasticsearch 和 Redis 我想将我自己的输出添加到
  • 使用 array.map 后如何运行函数?

    我想做的是在使用 array map 之后运行一个函数 理论上 我应该能够在 array map 之后运行 但是 由于某种原因 它在 array map 完成之前运行该函数 我该如何解决 这是我的代码 var channelIds chan
  • Android 地理围栏广播接收器

    我已经使用 GoogleApiClient 实现了地理围栏 gt 触发时 服务会连接到 GoogleApiClient 并添加多个地理围栏 在我将另一个 IntentService 注册为地理围栏事件的 回调 之前 这或多或少有效 但仅限于
  • 获取引用而不下载对象[重复]

    这个问题在这里已经有答案了 我想检查 origin master 是否与我的 HEAD 不同 I do not想要git fetch 因为它可能非常昂贵 我滥用 git 的方式使得成本高得令人望而却步 任何允许我从远程获取提交列表或顶部提交
  • PySpark 用数组替换 Null

    通过 ID 连接后 我的数据框如下所示 ID Features Vector 1 50 Array 1 1 2 3 2 50 Null 我最终得到 向量 列中某些 ID 的空值 我想用 300 维的零数组替换这些 Null 值 与非空向量条
  • 用于多个项目构建的多个设置 gradle 文件

    我有以下项目结构 gt Starnderd Location gt Project1 gt settings gradle gt build gradle gt Subproject11 gt build gradle gt Subproj
  • Terraform azurerm 计划 start_time 始终在新部署时重置

    我正在尝试获取资源azurerm automation schedule在特定时间部署 ex 18 00 每月发生 我正在使用以下代码 locals update time 18 00 update date formatdate YYYY