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

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
    `

2 tasks

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

  • command: …
    when: apache.stat.size > 5248000

The final version appears as:

`

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

Brian Coca

Well, replacing debug: action with cron: call seems doable, but I am not sure what the logrotate command should be.

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
}