Use Ansible to replace *.desktop files by custom versions ?

Hi,

Up until recently I’ve been using a bone-headed setup.sh shell script to setup my desktop clients based on Rocky Linux 8 and a heavily customized KDE desktop:

* https://gitlab.com/kikinovak/rocky-8-desktop

I’ve been learning to use Ansible for some time now (with a few false starts) and I’m currently translating this script into Ansible’s way of doing things:

* https://gitlab.com/kikinovak/rocky-8-ansible

In the original setup.sh script I have a replace_menus() function that loops through *.desktop files in /usr/share/applications and other places and replaces some of these with a custom version, but only if they exist in the first place.

I wonder how I could possibly translate this into Ansible code. Here’s what should happen:

1. Loop through all custom desktop entries in the templates/ directory.

2. If the corresponding .desktop file exists in /usr/share/applications, then replace it with the custom version from templates/.

3. If it doesn’t exist, then don’t install the template.

4. In other words: install the template only if the target already exists.

Any idea how I could possibly translate this into Ansible code ?

Cheers from the sunny South of France,

Niki