# How to copy files from multiple directories with ansible?

**URL:** <https://forum.ansible.com/t/how-to-copy-files-from-multiple-directories-with-ansible/25971>\
**Category:** Ansible Project\
**Created:** [November 9, 2017, 7:54am UTC](https://forum.ansible.com/t/how-to-copy-files-from-multiple-directories-with-ansible/25971 "2017-11-09T07:54:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dev](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@Dev](https://forum.ansible.com/u/Dev)\
**Post date:** [November 9, 2017, 7:54am UTC](https://forum.ansible.com/t/how-to-copy-files-from-multiple-directories-with-ansible/25971/1 "2017-11-09T07:54:37Z")

</div>

Hello everyone,  
I have the following structure:

dir1  
├── a1  
│ └── one.jar  
├── a2  
│ └── target  
│ └── two.jar  
├── a3  
│ └── subdir  
│ └── target  
│ ├── three.jar  
│ ├── four.jar  
│ ├── five.jar  
│ └── six.war  
└── a4  
└── target  
└── tmp  
└── seven.jar

And I want to copy all \*.jar files just under target directory to single remote directory. So the remote location should include files: two, three, four and five (except one, six and seven). I tried to use with\_fileglob and with\_filetree but without success. Can you help me?

`

- name: Copy  
copy:  
src: “{{ item }}”  
dest: ~/dir2  
with\_filetree: ~/dir1/…/target/\*.jar

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 9, 2017, 9:50am UTC](https://forum.ansible.com/t/how-to-copy-files-from-multiple-directories-with-ansible/25971/2 "2017-11-09T09:50:47Z")

</div>

with\_filetree doesn't support wildcard and with\_fileglobe doesn't support recursive.

If would like to copy all the files recursive in a directory you can use this

&nbsp;&nbsp;&nbsp;- name: Copy  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item.src }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: ~/dir2/{{ item.src.split('/') | last }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_filetree: ~/dir1//target/

If not you would need to use find module and then copy the files with the copy module.

---

<div class="post-metadata">

**Author:** ![Dev](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@Dev](https://forum.ansible.com/u/Dev)\
**Post date:** [November 13, 2017, 7:40am UTC](https://forum.ansible.com/t/how-to-copy-files-from-multiple-directories-with-ansible/25971/3 "2017-11-13T07:40:14Z")

</div>

Thanks for suggestion. I already did what I wanted using the following:

`

- name: Copy  
copy:  
src: “{{ item.src }}”  
dest: `~/dir2`  
with\_filetree: `~/dir1/`  
when:
- item.state == “file”
- item.path.split(‘/’)[-2] == “target”
- item.path.split(‘.’) | last == “jar”

`

W dniu czwartek, 9 listopada 2017 08:54:38 UTC+1 użytkownik Dev napisał:
