diff --git a/changelogs/fragments/76727-chattr-fix-for-backups-of-symlinks.yml b/changelogs/fragments/76727-chattr-fix-for-backups-of-symlinks.yml new file mode 100644 index 00000000000000..fe2c0946411256 --- /dev/null +++ b/changelogs/fragments/76727-chattr-fix-for-backups-of-symlinks.yml @@ -0,0 +1,2 @@ +bugfixes: + - check if there are attributes to set before attempting to set them (https://github.com/ansible/ansible/issues/76727) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index eca66085f3e33c..35fe5efb9583ab 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -958,7 +958,7 @@ def set_attributes_if_different(self, path, attributes, changed, diff=None, expa attr_mod = attributes[0] attributes = attributes[1:] - if existing.get('attr_flags', '') != attributes or attr_mod == '-': + if attributes and (existing.get('attr_flags', '') != attributes or attr_mod == '-'): attrcmd = self.get_bin_path('chattr') if attrcmd: attrcmd = [attrcmd, '%s%s' % (attr_mod, attributes), b_path] diff --git a/test/integration/targets/lineinfile/tasks/main.yml b/test/integration/targets/lineinfile/tasks/main.yml index b7c7f626becc5c..ae34c757593c97 100644 --- a/test/integration/targets/lineinfile/tasks/main.yml +++ b/test/integration/targets/lineinfile/tasks/main.yml @@ -1418,3 +1418,30 @@ - testend1 is changed - testend2 is changed - testend_file.stat.checksum == 'ef36116966836ce04f6b249fd1837706acae4e19' + +- name: Integration test for issue 76727 + block: + - name: Create a symbolic link for the test file + file: + src: "{{ remote_tmp_dir }}/test.txt" + dest: "{{ remote_tmp_dir }}/test-76727.txt" + state: link + + - name: Insert a line and back it up + lineinfile: + dest: "{{ remote_tmp_dir }}/test-76727.txt" + state: present + line: "#Line for issue 76727" + backup: yes + register: result1 + + - name: Stat the backup file + stat: + path: "{{ result1.backup }}" + register: result2 + + - name: Assert that the line was inserted and backup is created + assert: + that: + - result1 is changed + - result2.stat.exists