Deploy directory hierarchy of templates

Hi

I’m looking to deploy a number of configuration files using the template module. The files are structured in a directory hierarchy that needs to be maintained. So for this purpose I want to use the with_filetree lookup as mentioned on
https://docs.ansible.com/ansible/latest/plugins/lookup/filetree.html.

This is (at least) a two step process whereby the directory structure is created first, and the actual templates are done next.
The example also lists a third task that recreates symlinks if one needs that.

While this probably works fine, I do see an issue when a template is deployed first and then removed from the local directory later.
Redeployment would not remove this ‘orphan’ file from the target directory which will cause issues with my application.

So I think a final ‘cleanup’ step would be needed that deletes any files on the target that aren’t in the local directory.

Before I do any wheel reinvention - does someone already have such a task at hand?

Thx

Dick

OK to answer my own question, it wasn't very trivial, but I got it to
work the way I wanted.
Entire playbook:

I once had a similar problem and I solved it in also a similar way,
then made it into a role https://galaxy.ansible.com/bcoca/tidy

Sorry for resurrecting this old thread.
I shuffled things around a bit lately, and came up with a single
playbook file that can be used to template a directory tree. See
below.