How to copy files from multiple directories with ansible?

1,316 views
Skip to first unread message

Dev

unread,
Nov 9, 2017, 2:54:38 AM11/9/17
to Ansible Project

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



Kai Stian Olstad

unread,
Nov 9, 2017, 4:51:38 AM11/9/17
to ansible...@googlegroups.com
On 09.11.2017 08:54, Dev wrote:
> 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

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

- name: Copy
copy:
src: "{{ item.src }}"
dest: ~/dir2/{{ item.src.split('/') | last }}
with_filetree: ~/dir1//target/


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

--
Kai Stian Olstad

Dev

unread,
Nov 13, 2017, 2:40:15 AM11/13/17
to Ansible Project
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"

Reply all
Reply to author
Forward
0 new messages