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

**URL:** <https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693>\
**Category:** Ansible Project\
**Created:** [March 4, 2014, 10:24am UTC](https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693 "2014-03-04T10:24:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [March 4, 2014, 10:24am UTC](https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693/1 "2014-03-04T10:24:14Z")

</div>

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  
`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 4, 2014, 12:58pm UTC](https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693/2 "2014-03-04T12:58:21Z")

</div>

2 tasks

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

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

---

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [March 4, 2014, 8:54pm UTC](https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693/3 "2014-03-04T20:54:59Z")

</div>

The final version appears as:

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 4, 2014, 9:25pm UTC](https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693/4 "2014-03-04T21:25:36Z")

</div>

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

Brian Coca

---

<div class="post-metadata">

**Author:** ![anatoly\_techtonik](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anatoly\_techtonik](https://forum.ansible.com/u/anatoly_techtonik)\
**Post date:** [March 4, 2014, 9:59pm UTC](https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693/5 "2014-03-04T21:59:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 4, 2014, 10:33pm UTC](https://forum.ansible.com/t/when-filesize-var-log-apache2-access-log-5mb/14693/6 "2014-03-04T22:33:11Z")

</div>

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  
{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rotate 52  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weekly  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size 5Mb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;missingok  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;notifempty  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compress  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create 640 root adm  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sharedscripts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postrotate  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/etc/init.d/apache2 reload \> /dev/null  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endscript  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prerotate  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [-d /etc/logrotate.d/httpd-prerotate]; then \  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run-parts /etc/logrotate.d/httpd-prerotate; \  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi; \  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endscript  
}
