Behavior of ansible.builtin.copy backup=yes on symbolic links
0 reputation · 09 Jan 2023, 20:54 UTC
0 reputation · 09 Jan 2023, 20:54 UTC
The ansible.builtin.copy module's backup=yes parameter creates a timestamped backup of the destination file before overwriting it. When the destination is a symbolic link, the module backs up the link target rather than the link itself, potentially discarding the original link semantics. This behavior is not explicitly guaranteed across Ansible core versions, and administrators have no built-in way to confirm whether the restored path retains its symlink characteristics. The interaction between backup preservation and link integrity remains ambiguous in the documentation.
Does backup=yes unconditionally replace the symlink with a regular file copy? How does the behavior change between Ansible 2.9 and 2.10? Can checksum comparison alone confirm that link semantics are preserved?
33275 reputation · 09 Jan 2023, 23:29 UTC
When ansible.builtin.copy is run with backup=yes, the module first follows any symbolic links that appear in the src path (the default follow=True setting). It then creates a timestamped backup of the *target file* that the link points to. The destination on the remote host is overwritten with a regular file copy of that target; the original symlink is lost. This behavior is identical in Ansible 2.9 and 2.10 because the default follow flag did not change between those releases.
Thus, backup=yes does not preserve a symlink – it replaces it with a regular file. A checksum comparison between the backup and the source target will match, but that only verifies file contents, not that the restored path is a symlink.
ln -s /etc/hosts /tmp/hosts_link
- hosts: localhost
tasks:
- name: Copy symlink with backup
ansible.builtin.copy:
src: /tmp/hosts_link
dest: /tmp/hosts_link
backup: yes
ls -l /tmp/hosts_link.bak.*
# The output should show a regular file (e.g., "-rw-r--r--")
ls -l /tmp/hosts_link
# Should display a regular file, not a link
file module with state: link to create or maintain a link.copy but want to keep the link, set follow: false (available in newer Ansible versions) so the module copies the link itself instead of its target.If your goal is to keep the symlink semantics intact across runs, do you want to switch to the file module or enable follow: false in your copy task?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.