# Recursive Copy With Excluded Files?

**URL:** <https://forum.ansible.com/t/recursive-copy-with-excluded-files/43977>\
**Category:** Get Help\
**Created:** [July 22, 2025, 7:13am UTC](https://forum.ansible.com/t/recursive-copy-with-excluded-files/43977 "2025-07-22T07:13:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [July 22, 2025, 7:13am UTC](https://forum.ansible.com/t/recursive-copy-with-excluded-files/43977/1 "2025-07-22T07:13:34Z")

</div>

Hi All,

I’m looking for some help in desiging my playbook (as a relative n00b, I’m getting overwhealmed by the possibilities of _how_ to go about solving me issue).

I’m also trying to avoid fallling into the [XY Problem](https://en.wikipedia.org/wiki/XY_problem). 😄

My Issue:

I need to copy a given directory structure and the accompanying files within that structure from `my_src` to `my_dest`. However, there are some files (and folders) scattered about within this directory structure that are not required/wanted. Fortunately, all of these unrequired files can be easily identified because they all begin with the characters `_` or `Q_` (it is also relatively easy, at this point in time, to substitute different character strings for those given if those character combinations do/will cause problems). Likewise the unrequired folders are also easily identifiable.

For the record, all of these files are simple text files (ie these are no “executable” files) but they do have a variety of file extensions (ie `.txt`, `.md`, `.sc`, etc).

This is all on linux-based source and destination.

I’ve looked at using `ansible.posix.synchronize` but have ruled it out due to the requirement of having `rsync` installed: something that is not possible.

I think using `ansible.builtin.copy` is the way to do things, and I know how to do the recursive directory copy, etc, with that module, buy I’m not sure how to do the file/folder “exclusion” part.

So could someone help me out, please? Or if there’s a different solution then could you let me know - thanks.

Cheers  
Dulux-Oz

---

<div class="post-metadata">

**Author:** ![ildjarn](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@ildjarn](https://forum.ansible.com/u/ildjarn)\
**Post date:** [July 22, 2025, 7:31am UTC](https://forum.ansible.com/t/recursive-copy-with-excluded-files/43977/2 "2025-07-22T07:31:32Z")

</div>

Ugly solution: Use `ansible.builtin.copy` to copy everything and then `ansible.builtin.file` to delete the wrong files.  
Another ugly solution: Use `ansible.builtin.find` to generate a list of matching files and then loop, executing `ansible.builtin.copy` for each file (this is SLOW)  
Or write your own module, based on [ansible/lib/ansible/modules/copy.py at devel · ansible/ansible · GitHub](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/copy.py)

---

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [July 22, 2025, 9:59am UTC](https://forum.ansible.com/t/recursive-copy-with-excluded-files/43977/3 "2025-07-22T09:59:44Z")

</div>

Hi @ildjarn,

Thanks for getting back to me.

Can `ansible.builtin.file.path` take a wildcard? I can’t see if the doco says it can or it can’t.

Cheers  
Dulux-Oz
