file module, follow symlink when doing hardlink (#34228)

Fixes: #33911
pull/83332/head
odra 6 months ago committed by GitHub
parent 000bac9a3e
commit 46168c8cc2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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).

@ -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'

@ -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"
Loading…
Cancel
Save