From bbe110c1c0e4f4ba8695388b5100b5d267c74e89 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 13:04:55 +0000 Subject: [PATCH] feat(ansible): accept hostname alias --- modules.go | 3 +++ modules_adv_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/modules.go b/modules.go index 7e31f73..a24bd1a 100644 --- a/modules.go +++ b/modules.go @@ -2522,6 +2522,9 @@ func containsInt(values []int, target int) bool { func (e *Executor) moduleHostname(ctx context.Context, client sshExecutorClient, args map[string]any) (*TaskResult, error) { name := getStringArg(args, "name", "") + if name == "" { + name = getStringArg(args, "hostname", "") + } if name == "" { return nil, coreerr.E("Executor.moduleHostname", "name required", nil) } diff --git a/modules_adv_test.go b/modules_adv_test.go index 00e285a..680e573 100644 --- a/modules_adv_test.go +++ b/modules_adv_test.go @@ -191,6 +191,23 @@ func TestModulesAdv_ModuleHostname_Good_ChangesWhenDifferent(t *testing.T) { assert.True(t, mock.hasExecuted(`sed -i`)) } +func TestModulesAdv_ModuleHostname_Good_HostnameAlias(t *testing.T) { + e, mock := newTestExecutorWithMock("host1") + mock.expectCommand(`^hostname$`, "old-host\n", "", 0) + mock.expectCommand(`hostnamectl set-hostname "alias-host" \|\| hostname "alias-host"`, "", "", 0) + mock.expectCommand(`sed -i 's/127\.0\.1\.1\..*/127.0.1.1\talias-host/' /etc/hosts`, "", "", 0) + + result, err := e.moduleHostname(context.Background(), mock, map[string]any{ + "hostname": "alias-host", + }) + + require.NoError(t, err) + assert.True(t, result.Changed) + assert.False(t, result.Failed) + assert.True(t, mock.hasExecuted(`hostnamectl set-hostname`)) + assert.True(t, mock.hasExecuted(`sed -i`)) +} + // --- group module --- func TestModulesAdv_ModuleGroup_Good_CreateNewGroup(t *testing.T) {