summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--changelogs/fragments/73700-let-file-module-not-change-link-to-absolute-on-touch.yml2
-rw-r--r--lib/ansible/modules/file.py2
-rw-r--r--test/integration/targets/file/tasks/link_rewrite.yml47
-rw-r--r--test/integration/targets/file/tasks/state_link.yml1
4 files changed, 50 insertions, 2 deletions
diff --git a/changelogs/fragments/73700-let-file-module-not-change-link-to-absolute-on-touch.yml b/changelogs/fragments/73700-let-file-module-not-change-link-to-absolute-on-touch.yml
new file mode 100644
index 0000000000..340c9b6a01
--- /dev/null
+++ b/changelogs/fragments/73700-let-file-module-not-change-link-to-absolute-on-touch.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - file - prevent link src from being rewritten when src is not specified explicitly (https://github.com/ansible/ansible/issues/65448) \ No newline at end of file
diff --git a/lib/ansible/modules/file.py b/lib/ansible/modules/file.py
index 7e9a6f8257..2446a03e49 100644
--- a/lib/ansible/modules/file.py
+++ b/lib/ansible/modules/file.py
@@ -685,7 +685,7 @@ def ensure_symlink(path, src, follow, force, timestamps):
if src is None:
if follow:
# use the current target of the link as the source
- src = to_native(os.path.realpath(b_path), errors='strict')
+ src = to_native(os.readlink(b_path), errors='strict')
b_src = to_bytes(src, errors='surrogate_or_strict')
if not os.path.islink(b_path) and os.path.isdir(b_path):
diff --git a/test/integration/targets/file/tasks/link_rewrite.yml b/test/integration/targets/file/tasks/link_rewrite.yml
new file mode 100644
index 0000000000..b0e1af3ec2
--- /dev/null
+++ b/test/integration/targets/file/tasks/link_rewrite.yml
@@ -0,0 +1,47 @@
+- name: create temporary build directory
+ tempfile:
+ state: directory
+ suffix: ansible_test_leave_links_alone_during_touch
+ register: tempdir
+
+- name: create file
+ copy:
+ mode: 0600
+ content: "chicken"
+ dest: "{{ tempdir.path }}/somefile"
+
+- name: Create relative link
+ file:
+ src: somefile
+ dest: "{{ tempdir.path }}/somelink"
+ state: link
+
+- stat:
+ path: "{{ tempdir.path }}/somelink"
+ register: link
+
+- stat:
+ path: "{{ tempdir.path }}/somefile"
+ register: file
+
+- assert:
+ that:
+ - "file.stat.mode == '0600'"
+ - "link.stat.lnk_target == 'somefile'"
+
+- file:
+ path: "{{ tempdir.path }}/somelink"
+ mode: 0644
+
+- stat:
+ path: "{{ tempdir.path }}/somelink"
+ register: link
+
+- stat:
+ path: "{{ tempdir.path }}/somefile"
+ register: file
+
+- assert:
+ that:
+ - "file.stat.mode == '0644'"
+ - "link.stat.lnk_target == 'somefile'"
diff --git a/test/integration/targets/file/tasks/state_link.yml b/test/integration/targets/file/tasks/state_link.yml
index 89150adc9f..1753481538 100644
--- a/test/integration/targets/file/tasks/state_link.yml
+++ b/test/integration/targets/file/tasks/state_link.yml
@@ -388,7 +388,6 @@
- name: assert that the link target was unmodified
assert:
that:
- - 'file10_result is changed'
- 'file10_target_stat["stat"]["mode"] == "0644"'