The Ansible file module will allow you to use ‘state: absent’ to remove a file path, but if you need to perform this action if and only if the path is a symbolic link then you will need to register a test for this condition.
By first using the stat module to test if the path is a link, you can then use that in the subseqent ‘when’ of the deletion.
# check stat of path - stat: path: "{{link_path}}" register: link - debug: msg: "does path {{link_path}} exist? {{link.stat.exists}} and is it link {{link.stat.islnk|default(false)}}" # only deletes if symbolic link - name: remove symlink file: path: "{{link_path}}" state: absent when: link.stat.islnk is defined and link.stat.islnk
The first check for whether ‘link.stat.islnk’ is defined is necessary because if the path is to a normal file, the ‘islnk’ attribute will not be defined.
Here is a link to the full playbook, playbook-remove-symlink.yml.
REFERENCES
stackoverflow.com, checking existence of symlink with Ansible