when: filesize(/var/log/apache2/access.log) > 5Mb

131 views
Skip to first unread message

anatoly techtonik

unread,
Mar 4, 2014, 5:24:14 AM3/4/14
to ansible...@googlegroups.com
What is the way to conditionally run task based on filesystem check on remote system?
I want something like this, but of course it doesn't work:

  tasks:
 
- name: cleaning up the logs that exceed 5Mb
    command
: cat /dev/null > {{ item }}
   
when: filesize(/var/log/apache2/access.log) > 5Mb


Brian Coca

unread,
Mar 4, 2014, 7:58:21 AM3/4/14
to ansible...@googlegroups.com
2 tasks

- stat: path=/var/log/apache2/access.log
  register: apache

- command: ...
  when: apache.stat.size > 5248000

anatoly techtonik

unread,
Mar 4, 2014, 3:54:59 PM3/4/14
to ansible...@googlegroups.com
The final version appears as:

---
- hosts: "{{ host | default('localhost') }}"
  vars
:
   
- loglist:
     
- /var/log/wtmp
     
- /var/log/apache2/access.log
     
- /var/log/apache2/error.log

  tasks
:
 
- name: erase old downloaded .deb archives
    command
: apt autoclean

 
- name: get log size info
    stat
: path={{ item }}
    with_items
: loglist
   
register: logsize

 
- name: cleaning up logs that exceed 5Mb
    debug
: "msg='{{ item.item }}: {{ item.stat.size }}'"
    with_items
: logsize.results
   
when: item.stat.size > 5*(1024**2)


But the output of Ansible is way too huge.

Brian Coca

unread,
Mar 4, 2014, 4:25:36 PM3/4/14
to ansible...@googlegroups.com

Now that I think of it, wouldn't running logrotate with a specific config from cron every few mins be better?

Brian Coca

anatoly techtonik

unread,
Mar 4, 2014, 4:59:55 PM3/4/14
to ansible...@googlegroups.com
Well, replacing `debug:` action with `cron:` call seems doable, but I am not sure what the logrotate command should be.

Brian Coca

unread,
Mar 4, 2014, 5:33:11 PM3/4/14
to ansible...@googlegroups.com
taken from the apache normal logrotate file, this will rotate the files weekly or when over 5mb, whichever comes first. I used a glob on the first line, if you want to avoid other log files, just specify the file list there. logrotate has good man page.

*/5 * * * * logrotate /etc/logrotate_apache_5mb.conf

--- /etc/logrotate_apache_5mb.conf

/var/log/apache2/*.log
{
        rotate 52
        weekly
        size 5Mb
        missingok
        notifempty
        compress
        create 640 root adm
        sharedscripts
        postrotate
                /etc/init.d/apache2 reload > /dev/null
        endscript
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi; \
        endscript
}




--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno
Reply all
Reply to author
Forward
0 new messages