delete the older files using Ansible play book

5,130 views
Skip to first unread message

bibhu

unread,
Aug 25, 2017, 3:57:24 AM8/25/17
to Ansible Project

I need to write an ansible script to solve the below requirement. i am trying to write script, few steps i have written but i am having some doubt how to lsit all the files older than 7 days, once we got the list of fies we need to send those files “*.merged”  to a specific mail id, below are few steps which I have initiated . not sure that is right or wrong. Request you to suggest the steps.

 

REQUIREMENT:

 

1. monitoring the “*.merged” files older than 7 days and delete those files after sending the list of files to the Mail id

2. In linux we are using " ls -l  /prod/HDOP/data/*/bigplay.na/yarn/nm/usercache" to display all the Cache Files as "*.merged"

3. we need to find all the files older than 7 days and make a list

4. Send a mail having list of  files which is older than 7 days to X...@gmail.com on a weekly basis.

5.we need to delete all files older than 7 days ,Once we have sent those file list to X...@gmail.com

PLAYBOOK:

 

---

- hosts: All

  user :

  sudo: yes

  Connection : SSH

  gather-facts : yes

  tasks:

    - name: list the cache files

      find:

         paths= "/prod/HDOP/data/*"  age="8d" recurse=yes

         patterns: *.merged

**** Not sure how to list all the files and send those list to specific mail id*************

**** Then Need to delete those files which is older than 7days ****************

Dick Visser

unread,
Aug 25, 2017, 7:15:55 AM8/25/17
to ansible...@googlegroups.com
Pseudo code:

1. find - to find the specific files, and register the result.
2. http://docs.ansible.com/ansible/latest/mail_module.html and attach the files from the previous  task result
3. file - to remove the same set of files from 1.





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e5300906-4149-413f-85a0-76ebbba73212%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Dick Visser
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

bibhu

unread,
Aug 28, 2017, 2:07:10 PM8/28/17
to Ansible Project
Hello Group Members,

Can some one please  look into below ansible codes ,and let me know whether it's correct or we need to modify anywhere, as I am new to ansible and currently I don't have access to test the code.

REQUIREMENT: 

1. monitoring the “*.merged” files older than 7 days and delete those files after sending the list of files to the Mail id

2. In linux we are using " ls -l  /prod/HDOP/data/*/bigplay.na/yarn/nm/usercache" to display all the Cache Files as "*.merged"

3. we need to find all the files older than 7 days and make a list

4. Send a mail having list of  files which is older than 7 days to X...@gmail.com on a weekly basis.

5.we need to delete all files older than 7 days ,Once we have sent those file list to X...@gmail.com

PLAYBOOK: 

---

- hosts: All

  user :

  sudo: yes

  Connection : SSH

  gather-facts : yes

  tasks:

    - name: list the cache files

      find:

         paths= "/prod/HDOP/data/*"  age="8d" recurse=yes

         patterns: "*.merged"

         register: files_matched         

     - debug:

            msg: "{{ files_matched. stdout_lines }}"

     - name: Send a success email

       mail:

        host: ( Simple Mail Transfer Protocol (SMTP) name)

        port: xxxxxx

        username: 28283aeebd83616c6

        password: 0432eb4224e406

        to: xx...@xxxx.com

        subject: Cleaning Yarn Cache

        body: "List of files {{ files_matched. stdout_lines }}"

   

- name : clear more than 7 days older cache file

  files:

      path={{ item.path }}

       state=absent

       with_items: "{{ files_matched. stdout_lines }}"

Dick Visser

unread,
Aug 28, 2017, 6:17:03 PM8/28/17
to ansible...@googlegroups.com
Hi Bibhu

Not sure what you mean by "I don't have access to test the code".
Instead of guessing what might have happened if you ran the playbook,
just run it.

If you are concerned with accidentally removing files, use the
'--check' option for ansible-playbook.
Ansible will report any issues, usually in a easy to understand
fashion so you can fix the code.

Dick
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/139093aa-9f96-4789-bbd1-4fb98a215be1%40googlegroups.com.

bibhu

unread,
Aug 28, 2017, 9:51:26 PM8/28/17
to Ansible Project
Thank you Dick

Thanks and Regards
Bibhu
Reply all
Reply to author
Forward
0 new messages