Simple ansible logrotate script

Ansible logs

Ansible logs details of playbooks runs into files defined with the:

log_path = /path/text.log

variable if defined in the ansible.cfg file.

For my simple personal use cases, I turned logging on and for a while did nothing with the logfile.

After several months, the first log file turned into several thousand lines long.

I wrote a fairly simple shell scrip to do basic logrotating with the ansible log file.

I uploaded this script to a Github gist, with the raw script link being:

ansible-log-rotate.sh

There's also a Gitlab copy of this script:

ansible-log-rotate.sh

Cron job

I run a cron job monthly, which looks like this:

#Ansible: monthly ansible logrotate
@monthly bash $HOME/path/scripts/ansible-log-rotate  > ${LOG_FILE_PATH}/ansible-logrotate-$(date +\%Y-\%m-\%d-\%H-\%M-\%S).log 2>&1

This way the log rotate scripts runs every month, on the first of month at midnight.

I've been running this script with cron for a few months, and it resulted in the following tar.gz files, which are readable with zcat:

ansible.log             ansible.log-2019-12-01-00-00-01.tar.gz
ansible-log-201907310913.tar.gz     ansible.log-2020-01-01-00-00-01.tar.gz
ansible.log-2019-09-01-00-00-01.tar.gz  ansible.log-2020-02-01-00-00-01.tar.gz
ansible.log-2019-10-01-00-00-01.tar.gz  ansible.log-2020-03-01-00-00-01.tar.gz
ansible.log-2019-11-01-00-00-01.tar.gz  ansible.log-2020-04-01-00-00-01.tar.gz

Of course, a lot better way would be to ship these log files to a central log server, but for a personal use case I find these adequate.

links

social