feat(ansible): preserve blockinfile backups on remove
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
508248a722
commit
29b433fdbf
3 changed files with 44 additions and 3 deletions
|
|
@ -1152,7 +1152,11 @@ func moduleBlockinfileWithClient(_ *Executor, client sshFileRunner, args map[str
|
|||
replaceAll(endMarker, "/", "\\/"),
|
||||
path)
|
||||
_, _, _, _ = client.Run(context.Background(), cmd)
|
||||
return &TaskResult{Changed: true}, nil
|
||||
result := &TaskResult{Changed: true}
|
||||
if backupPath != "" {
|
||||
result.Data = map[string]any{"backup_file": backupPath}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Create file if needed (best-effort)
|
||||
|
|
|
|||
18
modules.go
18
modules.go
|
|
@ -3565,8 +3565,22 @@ func (e *Executor) moduleBlockinfile(ctx context.Context, client sshExecutorClie
|
|||
replaceAll(beginMarker, "/", "\\/"),
|
||||
replaceAll(endMarker, "/", "\\/"),
|
||||
path)
|
||||
_, _, _, _ = client.Run(ctx, cmd)
|
||||
return &TaskResult{Changed: true}, nil
|
||||
stdout, stderr, rc, err := client.Run(ctx, cmd)
|
||||
if err != nil || rc != 0 {
|
||||
return &TaskResult{Failed: true, Msg: stderr, Stdout: stdout, RC: rc}, nil
|
||||
}
|
||||
|
||||
result := &TaskResult{Changed: true}
|
||||
if backupPath != "" {
|
||||
result.Data = map[string]any{"backup_file": backupPath}
|
||||
}
|
||||
if e.Diff {
|
||||
if after, ok := remoteFileText(ctx, client, path); ok && before != after {
|
||||
result.Data = ensureTaskResultData(result.Data)
|
||||
result.Data["diff"] = fileDiffData(path, before, after)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Create file if needed (best-effort)
|
||||
|
|
|
|||
|
|
@ -1045,6 +1045,29 @@ func TestModulesFile_ModuleBlockinfile_Good_RemoveBlock(t *testing.T) {
|
|||
assert.True(t, mock.hasExecuted(`sed -i '/.*BEGIN ANSIBLE MANAGED BLOCK/,/.*END ANSIBLE MANAGED BLOCK/d'`))
|
||||
}
|
||||
|
||||
func TestModulesFile_ModuleBlockinfile_Good_RemoveBlockWithBackup(t *testing.T) {
|
||||
e, mock := newTestExecutorWithMock("host1")
|
||||
mock.addFile("/etc/config", []byte("before\n# BEGIN ANSIBLE MANAGED BLOCK\nmanaged\n# END ANSIBLE MANAGED BLOCK\nafter\n"))
|
||||
|
||||
result, err := moduleBlockinfileWithClient(e, mock, map[string]any{
|
||||
"path": "/etc/config",
|
||||
"state": "absent",
|
||||
"backup": true,
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result.Changed)
|
||||
require.NotNil(t, result.Data)
|
||||
|
||||
backupPath, ok := result.Data["backup_file"].(string)
|
||||
require.True(t, ok)
|
||||
assert.Contains(t, backupPath, "/etc/config.")
|
||||
|
||||
backupContent, err := mock.Download(context.Background(), backupPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []byte("before\n# BEGIN ANSIBLE MANAGED BLOCK\nmanaged\n# END ANSIBLE MANAGED BLOCK\nafter\n"), backupContent)
|
||||
}
|
||||
|
||||
func TestModulesFile_ModuleBlockinfile_Good_RemoveBlockWithCustomMarkerBeginAndEnd(t *testing.T) {
|
||||
e, mock := newTestExecutorWithMock("host1")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue