feat(ansible): support custom reboot commands
This commit is contained in:
parent
b75ba32cc2
commit
17691b9ff0
2 changed files with 31 additions and 1 deletions
13
modules.go
13
modules.go
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue