feat(ansible): support cron special_time schedules
Some checks are pending
CI / test (push) Waiting to run
CI / auto-fix (push) Waiting to run
CI / auto-merge (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 13:51:58 +00:00
parent fd6b8b0d2f
commit 2927fb4c78
3 changed files with 25 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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)