robertdebock.logrotate
Ansible Role Logrotate
This guide explains how to install and set up logrotate on your system.
GitHub | GitLab | Downloads | Version |
---|---|---|---|
Example Playbook
Here’s a simple example to use logrotate, taken from molecule/default/converge.yml
. It is tested every time changes are made.
---
- name: Converge
hosts: all
become: true
gather_facts: true
vars:
logrotate_frequency: daily
logrotate_keep: 7
logrotate_compress: true
logrotate_entries:
- name: example
path: "/var/log/example/*.log"
- name: example-frequency
path: "/var/log/example-frequency/*.log"
frequency: weekly
- name: example-keep
path: "/var/log/example-keep/*.log"
keep: 14
- name: example-compress
path: "/var/log/example-compress/*.log"
compress: true
- name: example-copylog
path: "/var/log/example-copylog/*.log"
copylog: true
- name: example-copytruncate
path: "/var/log/example-copytruncate/*.log"
copytruncate: true
- name: example-delaycompress
path: "/var/log/example-delaycompress/*.log"
delaycompress: true
- name: example-script
path: "/var/log/example-script/*.log"
postrotate: killall -HUP some_process_name
- name: btmp
path: /var/log/btmp
missingok: true
frequency: monthly
create: true
create_mode: "0660"
create_user: root
create_group: utmp
dateext: true
dateformat: "-%Y-%m-%d"
keep: 1
- name: wtmp
path: /var/log/wtmp
missingok: true
frequency: monthly
create: true
create_mode: "0664"
create_user: root
create_group: utmp
minsize: 1M
maxsize: 128M
dateext: true
dateformat: "-%Y%m%d"
keep: 1
- name: dnf
path: /var/log/hawkey.log
missingok: true
notifempty: true
keep: 4
frequency: weekly
create: true
- name: example-sharedscripts
path: "/var/log/example-sharedscripts/*.log"
sharedscripts: true
- name: example-dateyesterday
state: present
path: "/var/log/example-dateyesterday/*.log"
dateyesterday: true
- name: example-absent
state: absent
roles:
- role: robertdebock.logrotate
Make sure the system is set up correctly. In CI (Continuous Integration), this is done using molecule/default/prepare.yml
:
---
- name: Prepare
hosts: all
become: true
gather_facts: false
roles:
- role: robertdebock.bootstrap
- role: robertdebock.cron
post_tasks:
- name: Create log directory
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: "0755"
loop:
- /var/log/example
- /var/log/example-frequency
- /var/log/example-keep
- /var/log/example-compress
- /var/log/example-copylog
- /var/log/example-copytruncate
- /var/log/example-delaycompress
- /var/log/example-script
- /var/log/example-sharedscripts
- /var/log/example-dateyesterday
- name: Create log file
ansible.builtin.copy:
dest: "{{ item }}"
content: "example"
mode: "0644"
loop:
- /var/log/example/app.log
- /var/log/example-frequency/app.log
- /var/log/example-keep/app.log
- /var/log/example-compress/app.log
- /var/log/example-copylog/app.log
- /var/log/example-copytruncate/app.log
- /var/log/example-delaycompress/app.log
- /var/log/example-script/app.log
- /var/log/example-sharedscripts/app.log
- /var/log/example-dateyesterday/app.log
- /var/log/btmp
- /var/log/wtmp
- /var/log/hawkey.log
You can find a detailed explanation and examples on how to use these roles.
Role Variables
Default values for variables are in defaults/main.yml
:
---
# defaults file for logrotate
# How often to rotate logs: daily, weekly, or monthly.
logrotate_frequency: weekly
# Number of files to keep.
logrotate_keep: 4
# Should logs be compressed?
logrotate_compress: true
# Use date extensions on log file names.
logrotate_dateext: false
# User and Group for rotated log files
logrotate_user: "{{ _logrotate_user[ansible_distribution] | default(_logrotate_user['default']) }}"
logrotate_group: "{{ _logrotate_group[ansible_distribution] | default(_logrotate_group['default']) }}"
# Set the state of the logrotate service
logrotate_service_state: "started"
logrotate_service_enabled: true
Requirements
- Check the pip packages in requirements.txt.
State of Used Roles
These roles are used to prepare the system. You can choose other methods as well.
Requirement | GitHub | GitLab |
---|---|---|
robertdebock.bootstrap | ||
robertdebock.cron |
Context
This role is part of a bigger set of functions. Check out the documentation for more information.
Compatibility
This role has been tested on the following container images:
Container | Tags |
---|---|
Alpine | all |
Amazon | Candidate |
EL | 9 |
Debian | all |
Fedora | all |
Ubuntu | all |
Ansible version requirement is 2.12. Tests have been conducted for the prior version, the current version, and the development version.
If you encounter any issues, please report them on GitHub.
License
Author Information
Consider supporting my work.
Install and configure logrotate on your system.
ansible-galaxy install robertdebock.logrotate