On 03.10.2018 16:13, Deepan M wrote:
> 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.
> On Wednesday, October 3, 2018 at 11:10:22 AM UTC+5:30, Deepan M wrote:
>>
>> 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
--
Kai Stian Olstad