diff --git a/changelogs/fragments/68247-file-unreachable-code.yaml b/changelogs/fragments/68247-file-unreachable-code.yaml new file mode 100644 index 00000000000..ea2c01a8232 --- /dev/null +++ b/changelogs/fragments/68247-file-unreachable-code.yaml @@ -0,0 +1,2 @@ +bugfixes: + - file - Removed unreachable code in module diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py index 7ec3c3117e8..bd313baf9e8 100644 --- a/lib/ansible/modules/files/file.py +++ b/lib/ansible/modules/files/file.py @@ -707,7 +707,7 @@ def ensure_symlink(path, src, follow, force, timestamps): diff = initial_diff(path, 'link', prev_state) changed = False - if prev_state == 'absent': + if prev_state in ('hard', 'file', 'directory', 'absent'): changed = True elif prev_state == 'link': b_old_src = os.readlink(b_path) @@ -715,22 +715,6 @@ def ensure_symlink(path, src, follow, force, timestamps): diff['before']['src'] = to_native(b_old_src, errors='strict') diff['after']['src'] = src changed = True - elif prev_state == 'hard': - changed = True - if not force: - raise AnsibleModuleError(results={'msg': 'Cannot link because a hard link exists at destination', - 'dest': path, 'src': src}) - elif prev_state == 'file': - changed = True - if not force: - raise AnsibleModuleError(results={'msg': 'Cannot link because a file exists at destination', - 'dest': path, 'src': src}) - elif prev_state == 'directory': - changed = True - if os.path.exists(b_path): - if not force: - raise AnsibleModuleError(results={'msg': 'Cannot link because a file exists at destination', - 'dest': path, 'src': src}) else: raise AnsibleModuleError(results={'msg': 'unexpected position reached', 'dest': path, 'src': src}) diff --git a/test/integration/targets/file/tasks/state_link.yml b/test/integration/targets/file/tasks/state_link.yml index 55f959de810..15927881d8d 100644 --- a/test/integration/targets/file/tasks/state_link.yml +++ b/test/integration/targets/file/tasks/state_link.yml @@ -360,3 +360,46 @@ that: - src_state is failed - "'src option requires state to be' in src_state.msg" + +# Test creating a symlink when the destination exists and is a file +- name: create a test file + copy: + dest: '{{ output_dir }}/file.txt' + content: 'this is a test file\n' + mode: 0666 + +- name: Create a symlink with dest already a file + file: + src: '{{ output_file }}' + dest: '{{ output_dir }}/file.txt' + state: link + ignore_errors: true + register: dest_is_existing_file_fail + +- name: Stat to make sure the symlink was not created + stat: + path: '{{ output_dir }}/file.txt' + follow: false + register: dest_is_existing_file_fail_stat + +- name: Forcefully a symlink with dest already a file + file: + src: '{{ output_file }}' + dest: '{{ output_dir }}/file.txt' + state: link + force: true + register: dest_is_existing_file_force + +- name: Stat to make sure the symlink was created + stat: + path: '{{ output_dir }}/file.txt' + follow: false + register: dest_is_existing_file_force_stat + +- assert: + that: + - dest_is_existing_file_fail is failed + - not dest_is_existing_file_fail_stat.stat.islnk + - dest_is_existing_file_force is changed + - dest_is_existing_file_force_stat.stat.exists + - dest_is_existing_file_force_stat.stat.islnk