feat(ansible): support custom reboot commands
Some checks are pending
CI / test (push) Waiting to run
CI / auto-fix (push) Waiting to run
CI / auto-merge (push) Waiting to run

This commit is contained in:
Virgil 2026-04-03 13:02:11 +00:00
parent b75ba32cc2
commit 17691b9ff0
2 changed files with 31 additions and 1 deletions

View file

@ -3329,6 +3329,7 @@ func (e *Executor) moduleReboot(ctx context.Context, client sshExecutorClient, a
postRebootDelay := getIntArg(args, "post_reboot_delay", 0)
rebootTimeout := getIntArg(args, "reboot_timeout", 600)
testCommand := getStringArg(args, "test_command", "whoami")
rebootCommand := getStringArg(args, "reboot_command", "")
msg := getStringArg(args, "msg", "Reboot initiated by Ansible")
runReboot := func(cmd string) (*TaskResult, error) {
@ -3343,7 +3344,17 @@ func (e *Executor) moduleReboot(ctx context.Context, client sshExecutorClient, a
return nil, nil
}
if preRebootDelay > 0 {
if rebootCommand != "" {
if preRebootDelay > 0 {
if result, err := runReboot(sprintf("sleep %d && %s", preRebootDelay, rebootCommand)); err != nil || result != nil {
return result, err
}
} else {
if result, err := runReboot(rebootCommand); err != nil || result != nil {
return result, err
}
}
} else if preRebootDelay > 0 {
cmd := sprintf("sleep %d && shutdown -r now '%s' &", preRebootDelay, msg)
if result, err := runReboot(cmd); err != nil || result != nil {
return result, err

View file

@ -2040,6 +2040,25 @@ func TestModulesAdv_ModuleReboot_Good_WaitsForTestCommand(t *testing.T) {
assert.True(t, mock.hasExecuted(`whoami`))
}
func TestModulesAdv_ModuleReboot_Good_CustomRebootCommand(t *testing.T) {
e, mock := newTestExecutorWithMock("host1")
mock.expectCommand(`sleep 1 && /sbin/reboot`, "", "", 0)
mock.expectCommand(`whoami`, "root\n", "", 0)
result, err := e.moduleReboot(context.Background(), mock, map[string]any{
"reboot_command": "/sbin/reboot",
"pre_reboot_delay": 1,
"reboot_timeout": 5,
})
require.NoError(t, err)
assert.True(t, result.Changed)
assert.Equal(t, "Reboot initiated", result.Msg)
assert.Equal(t, 2, mock.commandCount())
assert.True(t, mock.hasExecuted(`sleep 1 && /sbin/reboot`))
assert.True(t, mock.hasExecuted(`whoami`))
}
func TestModulesAdv_ModuleReboot_Bad_TimesOutWaitingForTestCommand(t *testing.T) {
e, mock := newTestExecutorWithMock("host1")
mock.expectCommand(`shutdown -r now 'Reboot initiated by Ansible' &`, "", "", 0)