From 46168c8cc26c3100d220529abe5f72a127967824 Mon Sep 17 00:00:00 2001 From: odra Date: Thu, 30 May 2024 02:22:19 -0300 Subject: [PATCH] file module, follow symlink when doing hardlink (#34228) Fixes: #33911 --- changelogs/fragments/file_hardlink.yml | 3 ++ lib/ansible/modules/file.py | 2 + .../targets/file/tasks/link_follow.yml | 45 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 changelogs/fragments/file_hardlink.yml create mode 100644 test/integration/targets/file/tasks/link_follow.yml diff --git a/changelogs/fragments/file_hardlink.yml b/changelogs/fragments/file_hardlink.yml new file mode 100644 index 00000000000..26c5eeaf7c6 --- /dev/null +++ b/changelogs/fragments/file_hardlink.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - file - retrieve the link's full path when hard linking a soft link with follow (https://github.com/ansible/ansible/issues/33911). diff --git a/lib/ansible/modules/file.py b/lib/ansible/modules/file.py index 564d7f6cdbe..6ba39903a52 100644 --- a/lib/ansible/modules/file.py +++ b/lib/ansible/modules/file.py @@ -872,6 +872,8 @@ def ensure_hardlink(path, src, follow, force, timestamps): 'path': path}) else: try: + if follow and os.path.islink(b_src): + b_src = os.readlink(b_src) os.link(b_src, b_path) except OSError as e: raise AnsibleModuleError(results={'msg': 'Error while linking: %s' diff --git a/test/integration/targets/file/tasks/link_follow.yml b/test/integration/targets/file/tasks/link_follow.yml new file mode 100644 index 00000000000..f45360f249f --- /dev/null +++ b/test/integration/targets/file/tasks/link_follow.yml @@ -0,0 +1,45 @@ +- name: Ensure output_dir + file: + path: "{{output_dir}}" + state: directory + +- name: Touch a file in it + file: + path: "{{output_dir}}/original.txt" + state: touch + +- name: Create a symlink + file: + src: "{{output_dir}}/original.txt" + dest: "{{output_dir}}/soft.txt" + state: link + +- name: Create an hard link with follow to the sym link + file: + src: "{{output_dir}}/soft.txt" + dest: "{{output_dir}}/hard.txt" + state: hard + follow: yes + +- name: Create a symlink from another symlink + file: + src: "{{output_dir}}/soft.txt" + dest: "{{output_dir}}/soft2.txt" + follow: yes + state: link + +- name: Hard link stat + stat: + path: "{{output_dir}}/hard.txt" + register: hard_stat + +- name: Soft link stat + stat: + path: "{{output_dir}}/soft2.txt" + register: soft_stat + +- name: Check link status + assert: + that: + - "hard_stat.stat.exists == true" + - "soft_stat.stat.exists == true"