From 821211e6718f6941239bff300b5a955a606faed0 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 14:27:30 +0000 Subject: [PATCH] feat(ansible): recurse file module group and mode Co-Authored-By: Virgil --- mock_ssh_test.go | 6 ++++++ modules.go | 6 ++++++ modules_file_test.go | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/mock_ssh_test.go b/mock_ssh_test.go index 636b400..5c358d4 100644 --- a/mock_ssh_test.go +++ b/mock_ssh_test.go @@ -871,6 +871,12 @@ func moduleFileWithClient(_ *Executor, client sshFileRunner, args map[string]any if owner := getStringArg(args, "owner", ""); owner != "" { _, _, _, _ = client.Run(context.Background(), sprintf("chown -R %s %q", owner, path)) } + if group := getStringArg(args, "group", ""); group != "" { + _, _, _, _ = client.Run(context.Background(), sprintf("chgrp -R %s %q", group, path)) + } + if mode := getStringArg(args, "mode", ""); mode != "" { + _, _, _, _ = client.Run(context.Background(), sprintf("chmod -R %s %q", mode, path)) + } } return &TaskResult{Changed: true}, nil diff --git a/modules.go b/modules.go index a650a89..677db61 100644 --- a/modules.go +++ b/modules.go @@ -812,6 +812,12 @@ func (e *Executor) moduleFile(ctx context.Context, client sshExecutorClient, arg if owner := getStringArg(args, "owner", ""); owner != "" { _, _, _, _ = client.Run(ctx, sprintf("chown -R %s %q", owner, path)) } + if group := getStringArg(args, "group", ""); group != "" { + _, _, _, _ = client.Run(ctx, sprintf("chgrp -R %s %q", group, path)) + } + if mode := getStringArg(args, "mode", ""); mode != "" { + _, _, _, _ = client.Run(ctx, sprintf("chmod -R %s %q", mode, path)) + } } return &TaskResult{Changed: true}, nil diff --git a/modules_file_test.go b/modules_file_test.go index 75f1141..4b93165 100644 --- a/modules_file_test.go +++ b/modules_file_test.go @@ -495,6 +495,25 @@ func TestModulesFile_ModuleFile_Good_RecurseOwner(t *testing.T) { assert.True(t, mock.hasExecuted(`chown -R www-data "/var/www"`)) } +func TestModulesFile_ModuleFile_Good_RecurseGroupAndMode(t *testing.T) { + e, mock := newTestExecutorWithMock("host1") + + result, err := moduleFileWithClient(e, mock, map[string]any{ + "path": "/srv/app", + "state": "directory", + "group": "appgroup", + "mode": "0770", + "recurse": true, + }) + + require.NoError(t, err) + assert.True(t, result.Changed) + + assert.True(t, mock.hasExecuted(`chgrp appgroup "/srv/app"`)) + assert.True(t, mock.hasExecuted(`chgrp -R appgroup "/srv/app"`)) + assert.True(t, mock.hasExecuted(`chmod -R 0770 "/srv/app"`)) +} + func TestModulesFile_ModuleFile_Bad_MissingPath(t *testing.T) { e, mock := newTestExecutorWithMock("host1")