From 2927fb4c78c94190ae7dee9bc9154d291c73ea16 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 13:51:58 +0000 Subject: [PATCH] feat(ansible): support cron special_time schedules Co-Authored-By: Virgil --- mock_ssh_test.go | 4 ++++ modules.go | 4 ++++ modules_adv_test.go | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/mock_ssh_test.go b/mock_ssh_test.go index 9f83885..cdf62a5 100644 --- a/mock_ssh_test.go +++ b/mock_ssh_test.go @@ -1564,6 +1564,7 @@ func moduleCronWithClient(_ *Executor, client sshRunner, args map[string]any) (* state := getStringArg(args, "state", "present") user := getStringArg(args, "user", "root") disabled := getBoolArg(args, "disabled", false) + specialTime := getStringArg(args, "special_time", "") minute := getStringArg(args, "minute", "*") hour := getStringArg(args, "hour", "*") @@ -1583,6 +1584,9 @@ func moduleCronWithClient(_ *Executor, client sshRunner, args map[string]any) (* // Build cron entry schedule := sprintf("%s %s %s %s %s", minute, hour, day, month, weekday) + if specialTime != "" { + schedule = "@" + specialTime + } entry := sprintf("%s %s # %s", schedule, job, name) if disabled { entry = "# " + entry diff --git a/modules.go b/modules.go index 6f400c2..df58627 100644 --- a/modules.go +++ b/modules.go @@ -2768,6 +2768,7 @@ func (e *Executor) moduleCron(ctx context.Context, client sshExecutorClient, arg state := getStringArg(args, "state", "present") user := getStringArg(args, "user", "root") disabled := getBoolArg(args, "disabled", false) + specialTime := getStringArg(args, "special_time", "") minute := getStringArg(args, "minute", "*") hour := getStringArg(args, "hour", "*") @@ -2787,6 +2788,9 @@ func (e *Executor) moduleCron(ctx context.Context, client sshExecutorClient, arg // Build cron entry schedule := sprintf("%s %s %s %s %s", minute, hour, day, month, weekday) + if specialTime != "" { + schedule = "@" + specialTime + } entry := sprintf("%s %s # %s", schedule, job, name) if disabled { entry = "# " + entry diff --git a/modules_adv_test.go b/modules_adv_test.go index c2da7cc..d771aba 100644 --- a/modules_adv_test.go +++ b/modules_adv_test.go @@ -487,6 +487,23 @@ func TestModulesAdv_ModuleCron_Good_CustomUser(t *testing.T) { assert.True(t, mock.containsSubstring("0 */4 * * *")) } +func TestModulesAdv_ModuleCron_Good_SpecialTime(t *testing.T) { + e := NewExecutor("/tmp") + mock := NewMockSSHClient() + mock.expectCommand(`crontab -u root`, "", "", 0) + + result, err := e.moduleCron(context.Background(), mock, map[string]any{ + "name": "daily-backup", + "job": "/usr/local/bin/backup.sh", + "special_time": "daily", + }) + + require.NoError(t, err) + assert.True(t, result.Changed) + assert.False(t, result.Failed) + assert.True(t, mock.containsSubstring(`@daily /usr/local/bin/backup.sh # daily-backup`)) +} + func TestModulesAdv_ModuleCron_Good_DisabledJobCommentsEntry(t *testing.T) { e, mock := newTestExecutorWithMock("host1") mock.expectCommand(`crontab -u root`, "", "", 0)