From b1d88ae3c13da261fd1c3bc90d8ae52efc7981ba Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 10 May 2026 10:22:07 +0200 Subject: [PATCH] [PR #12014/b8659f5c backport][stable-12] pam_limits: only create backup file when content actually changes (#12020) pam_limits: only create backup file when content actually changes (#12014) * fix(pam_limits): only create backup when file is actually changed Fixes #12011 * changelog: add fragment for PR 12014 --------- (cherry picked from commit b8659f5c61306d005804b21be0d6180dcc417638) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 --- changelogs/fragments/12014-pam_limits-backup.yml | 2 ++ plugins/modules/pam_limits.py | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/12014-pam_limits-backup.yml diff --git a/changelogs/fragments/12014-pam_limits-backup.yml b/changelogs/fragments/12014-pam_limits-backup.yml new file mode 100644 index 0000000000..61e0f6d0cb --- /dev/null +++ b/changelogs/fragments/12014-pam_limits-backup.yml @@ -0,0 +1,2 @@ +bugfixes: + - "pam_limits - only create backup file when the target file is actually modified (https://github.com/ansible-collections/community.general/issues/12011, https://github.com/ansible-collections/community.general/pull/12014)." diff --git a/plugins/modules/pam_limits.py b/plugins/modules/pam_limits.py index 022f1d58ee..e05caaa64b 100644 --- a/plugins/modules/pam_limits.py +++ b/plugins/modules/pam_limits.py @@ -235,10 +235,7 @@ def main(): _assert_is_valid_value(module, limit_item, value) - # Backup - if backup: - backup_file = module.backup_local(limits_conf) - + backup_file = None space_pattern = re.compile(r"\s+") if does_not_exist: @@ -356,6 +353,9 @@ def main(): with open(limits_conf, "a"): pass + if backup and changed and not does_not_exist: + backup_file = module.backup_local(limits_conf) + # Move tempfile to newfile module.atomic_move(os.path.abspath(nf.name), os.path.abspath(limits_conf)) @@ -370,7 +370,7 @@ def main(): diff=dict(before=b"".join(lines), after=content_new), ) - if backup: + if backup_file: res_args["backup_file"] = backup_file module.exit_json(**res_args)