ansible – delete unmanaged files from directory?

Question:

I want to recursively copy over a directory and render all .j2 files in there as templates. For this I am currently using the following lines:

Now I’m looking for a way to remove unmanaged files from this directory. For example if I remove a file/template from /src/conf.d/ I want Ansible to remove it from /dest/conf.d/ as well.

Is there some way to do this? I tried fiddling around with rsync --delete, but there I got a problem with the templates which get their suffix .j2 removed.

Answer:

I’d do it like this, assuming a variable defined as ‘managed_files’ up top that is a list.

Leave a Reply