I like to truncate or resize an already existing file which is opened and used by an other application. In example, the disk may be cluttered with log files taking up large amounts of space over time like /var/log/secure
.
In plain Linux the equivalent would be
truncate -s 0 /var/log/secure
dd if=/dev/zero of=/var/log/secure bs=1M count=0
echo > /var/log/secure
according
- How to empty ("truncate") a file on Linux that already exists and is protected in someway?
- How to create a file with a given size in Linux?
- How to create a file with ANY given size in Linux?
and via tail -F /var/log/secure
each would result into an
==> /var/log/secure <==
tail: /var/log/secure: file truncated
tail: /var/log/secure: file truncated
tail: /var/log/secure: file truncated
instead of
==> /var/log/secure <==
tail: ‘/var/log/secure’ has been replaced; following end of new file
After some testing it seems that all in Ansible v2.9 available modules (file
, lineinfile
, copy
, etc.) replace the existing file with a new empty one, but not truncating or resizing and resulting into the already mentioned
==> /var/log/secure <==
tail: ‘/var/log/secure’ has been replaced; following end of new file
I am aware that rotating, compressing and removing logs files with via logrotate
might be a better solution.