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

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

Anyone can able to help me.

Thanks,
Deepan M

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
    find: path=/var/tmp/{{ansible_hostname}} patterns="*.tar.xz"
  - name: copy files
    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
    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 }}"

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
    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 }}"

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)

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

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

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

   - name: Fetch file
     copy:
       src: "{{ (found_files.files | sort(attribute='mtime',reverse=true)

first).path }}"

       dest: /var/tmp/hostname/sosreport/
       remote_src: yes