# Looking for playbook to Find and Copy recently modified file from remote machine folder to folder

**URL:** <https://forum.ansible.com/t/looking-for-playbook-to-find-and-copy-recently-modified-file-from-remote-machine-folder-to-folder/28710>\
**Category:** Ansible Project\
**Created:** [October 3, 2018, 5:40am UTC](https://forum.ansible.com/t/looking-for-playbook-to-find-and-copy-recently-modified-file-from-remote-machine-folder-to-folder/28710 "2018-10-03T05:40:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [October 3, 2018, 5:40am UTC](https://forum.ansible.com/t/looking-for-playbook-to-find-and-copy-recently-modified-file-from-remote-machine-folder-to-folder/28710/1 "2018-10-03T05:40:22Z")

</div>

Hello,

I’m writing a playbook for patching and in that i required 2 modules to Find and Copy the recently modified file in remote machine , I have tried with below modules, but not working! if someone have an idea, kindly share.

::-Find and copy

- name: find recently modified file and copy it  
find: path=/var/tmp/{{ansible\_hostname}} patterns=“\*.tar.xz”
- name: copy files  
fetch: path=/var/tmp/{{ansible\_hostname}} dest=/kdump/sosreport/ flat=yes

::- Try to find recently modified file.  
First play

- name: find recently modified file and copy it  
find:  
path: “/var/tmp/{{ansible\_hostname}}”  
register: found\_files
- name: Get latest file  
set\_fact:  
latest\_file: “{{ found\_files.files | sort(attribute=‘mtime’,age=-1h,reverse=true) | first }}”

Second play

- name: find recently modified file and copy it  
find:  
path: “/var/tmp/{{ansible\_hostname}}”  
register: found\_files

- name: Get latest file  
set\_fact:  
latest\_file: “{{ found\_files.files | sort(attribute=‘mtime’,age=-1h,reverse=true) | first }}”

- name: print matches  
debug:  
msg: “{{ found\_files.files }}”

- name: print matches  
debug:  
msg: “{{ found\_files.files }}”

Thanks,  
Deepan M

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [October 3, 2018, 2:13pm UTC](https://forum.ansible.com/t/looking-for-playbook-to-find-and-copy-recently-modified-file-from-remote-machine-folder-to-folder/28710/2 "2018-10-03T14:13:05Z")

</div>

Anyone can able to help me.

Thanks,  
Deepan M

---

<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:** [October 4, 2018, 8:21am UTC](https://forum.ansible.com/t/looking-for-playbook-to-find-and-copy-recently-modified-file-from-remote-machine-folder-to-folder/28710/3 "2018-10-04T08:21:09Z")

</div>

> Anyone can able to help me.

When you don't get any answer the usual cause is that people don't understand you need or you have provided to little information.  
Here we have little of both.

> > Hello,
> > 
> > I'm writing a playbook for patching and in that i required 2 modules to  
> > Find and Copy the recently modified file in remote machine , I have tried  
> > with below modules, but not working! if someone have an idea, kindly share.

You are saying copy but using fetch.  
copy module in Ansible transferring a file(s) from Ansible controller to remote host.  
fetch is transferring a file from remote host to the Ansible controller.

And your explanation doesn't say what you are actually trying to do.  
Wild guess, you are trying to fetch a file from remote host to the Ansible controller?

When say it's not working, what is not working is always good to include in the post.

> > ::-Find and copy  
> > - name: find recently modified file and copy it  
> > &nbsp;&nbsp;&nbsp;&nbsp;find: path=/var/tmp/{{ansible\_hostname}} patterns="\*.tar.xz"  
> > &nbsp;&nbsp;- name: copy files  
> > &nbsp;&nbsp;&nbsp;&nbsp;fetch: path=/var/tmp/{{ansible\_hostname}} dest=/kdump/sosreport/ flat=yes

Here you use the find module, but not the result of it and the fetch would get the file /var/tmp/{{ansible\_hostname}}

> > ::- Try to find recently modified file.  
> > First play  
> > - name: find recently modified file and copy it  
> > &nbsp;&nbsp;&nbsp;&nbsp;find:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "/var/tmp/{{ansible\_hostname}}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;register: found\_files  
> > &nbsp;&nbsp;- name: Get latest file  
> > &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;latest\_file: "{{ found\_files.files | sort(attribute='mtime',age=-1h,reverse=true) | first }}"

Here you use the find and register the result, but don't use the result in fetch just set\_fact.

> > Second play  
> > - name: find recently modified file and copy it  
> > &nbsp;&nbsp;&nbsp;&nbsp;find:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "/var/tmp/{{ansible\_hostname}}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;register: found\_files  
> > &nbsp;&nbsp;- name: Get latest file  
> > &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;latest\_file: "{{ found\_files.files | sort(attribute='mtime',age=-1h,reverse=true) | first }}"  
> > &nbsp;&nbsp;- name: print matches  
> > &nbsp;&nbsp;&nbsp;&nbsp;debug:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ found\_files.files }}"
> > 
> > &nbsp;&nbsp;- name: print matches  
> > &nbsp;&nbsp;&nbsp;&nbsp;debug:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ found\_files.files }}"

The same here only find and set\_fact but you are missing the fetch task.

Try to add this fetch(and remove the set\_fact or change it accordingly)

&nbsp;&nbsp;&nbsp;- name: Fetch file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fetch:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ (found\_files.files | sort(attribute='mtime',reverse=true) | first).path }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /kdump/sosreport/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flat: yes

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [October 4, 2018, 10:06am UTC](https://forum.ansible.com/t/looking-for-playbook-to-find-and-copy-recently-modified-file-from-remote-machine-folder-to-folder/28710/4 "2018-10-04T10:06:19Z")

</div>

Hi Kai,

Thank you for your response and sorry for the information which is not clear.

Actually I have a created playbook for patching, before patching i want to run sosreport and recently created sosreport.tar.xz should be copied on same remote machine into local folders, like from /var/tmp/hostname/sosreport/\* to some NFS mount point!

So i’m looking for module like find & copy (to search recently modified file from specified folder on remote machine and that should be copied into specified folder.)

Please suggest for play.

Thanks,  
Deepan M

---

<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:** [October 4, 2018, 10:34am UTC](https://forum.ansible.com/t/looking-for-playbook-to-find-and-copy-recently-modified-file-from-remote-machine-folder-to-folder/28710/5 "2018-10-04T10:34:59Z")

</div>

> Hi Kai,
> 
> Thank you for your response and sorry for the information which is not  
> clear.
> 
> Actually I have a created playbook for patching, before patching i want to  
> run sosreport and recently created sosreport.tar.xz should be copied on  
> same remote machine into local folders, like from  
> /var/tmp/hostname/sosreport/\* to some NFS mount point!
> 
> So i'm looking for module like find & copy (to search recently modified  
> file from specified folder on remote machine and that should be copied into  
> specified folder.)

You can use copy module to copy from and to the same host by setting "remote\_src: yes"

The rest of it you already have, use the find module you had in you mail and add this copy

&nbsp;&nbsp;&nbsp;- name: Fetch file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ (found\_files.files | sort(attribute='mtime',reverse=true)

> first).path }}"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /var/tmp/hostname/sosreport/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remote\_src: yes
