community.general.logrotate module – Manage logrotate configurations
Note
This module is part of the community.general collection (version 12.5.0).
You might already have this collection installed if you are using the ansible package.
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.general.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: community.general.logrotate.
New in community.general 12.5.0
Synopsis
Manage
logrotateconfiguration files and settings.Create, update, or remove
logrotateconfigurations for applications and services.
Requirements
The below requirements are needed on the host that executes this module.
logrotate >= 3.8.0
Parameters
Parameter |
Comments |
|---|---|
Compress rotated log files. Choices:
|
|
Options to pass to compression program. For For |
|
Compression method to use. Requires logrotate 3.18.0 or later for Choices:
|
|
Directory where logrotate configurations are stored. Default is Use This directory must exist before using the module. |
|
Copy the log file but do not truncate the original. Takes precedence over Choices:
|
|
Copy the log file and then truncate it in place. Useful for applications that cannot be told to close their logfile. Choices:
|
|
Create new log file with specified permissions after rotation. Format is Set to |
|
Use date as extension for rotated files (using the date format specified in Choices:
|
|
Format for date extension. Use with Format specifiers are |
|
Use yesterday’s date for Useful for rotating logs that span midnight. Choices:
|
|
Postpone compression of the previous log file to the next rotation cycle. Useful for applications that keep writing to the old log file for some time. Choices:
|
|
Whether the configuration should be enabled. When Choices:
|
|
Extension to use for rotated log files (including dot). Useful when |
|
Commands to execute once before all log files that match the wildcard pattern are rotated. |
|
Include additional configuration files from specified directory. |
|
Commands to execute once after all log files that match the wildcard pattern are rotated. |
|
Mail logs to specified address when removed. Set to |
|
Mail just-created log file, not the about-to-expire one. Choices:
|
|
Mail about-to-expire log file (default). Choices:
|
|
Remove rotated logs older than specified number of days. |
|
Rotate log file when it grows bigger than specified size, but at most once per Format is |
|
Rotate log file only if it has grown bigger than specified size. Format is Used with time-based rotation to avoid rotating too small files. |
|
Do not issue an error if the log file is missing. Choices:
|
|
Name of the logrotate configuration. This creates a file in |
|
Opposite of Note that in logrotate, Choices:
|
|
Keep rotated logs in the same directory as the original log. Choices:
|
|
Do not rotate the log file if it is empty. Set to Choices:
|
|
Move rotated logs into specified directory. |
|
List of log file paths or patterns to rotate. Can include wildcards (for example Required when creating a new configuration ( Optional when modifying existing configuration (for example to enable/disable). |
|
Commands to execute after rotating the log file. Can be a single string or list of commands. |
|
Commands to execute before removing rotated log files. |
|
Commands to execute before rotating the log file. Can be a single string or list of commands. |
|
Rename and copy the log file, leaving the original in place. Choices:
|
|
Number of rotated log files to keep. Set to Set to |
|
How often to rotate the logs. If not specified when modifying an existing configuration, the existing value is preserved. When creating a new configuration, this option is only included if specified. Choices:
|
|
Use Uses Choices:
|
|
Number of times to overwrite files when using |
|
Rotate log file when it grows bigger than specified size. Format is Overrides If not specified, existing value be preserved when modifying configuration. When creating new configuration, this option be omitted if not specified. |
|
Base number for rotated files. Allowed values are from For example, |
|
Whether the configuration should be present or absent. Choices:
|
|
Set user and group for rotated files. Format is Set to Set to |
|
Send logrotate messages to syslog. Choices:
|
|
List of extensions that logrotate should not touch. Set to |
Attributes
Attribute |
Support |
Description |
|---|---|---|
Support: full |
Can run in |
|
Support: full |
Returns details on what has changed (or possibly needs changing in |
Examples
- name: Ensure logrotate config directory exists
ansible.builtin.file:
path: /etc/logrotate.d
state: directory
mode: '0755'
- name: Configure log rotation for Nginx
community.general.logrotate:
name: nginx
paths:
- /var/log/nginx/*.log
rotation_period: daily
rotate_count: 14
compress: true
compress_options: "-9"
delay_compress: true
missing_ok: true
not_if_empty: true
create: "0640 www-data adm"
shared_scripts: true
post_rotate:
- "[ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)"
- "echo 'Nginx logs rotated'"
- name: Configure size-based rotation for application logs
community.general.logrotate:
name: myapp
paths:
- /var/log/myapp/app.log
- /var/log/myapp/debug.log
size: 100M
rotate_count: 10
compress: true
compress_options: "-1"
date_ext: true
date_yesterday: true
date_format: -%Y%m%d.%s
missing_ok: true
copy_truncate: true
- name: Configure log rotation with secure deletion
community.general.logrotate:
name: secure-app
paths:
- /var/log/secure-app/*.log
rotation_period: weekly
rotate_count: 4
shred: true
shred_cycles: 3
compress: true
compress_options: "-9"
- name: Configure log rotation with custom start number
community.general.logrotate:
name: custom-start
paths:
- /var/log/custom/*.log
rotation_period: monthly
rotate_count: 6
start: 1
compress: true
- name: Configure log rotation with old directory
community.general.logrotate:
name: with-old-dir
paths:
- /opt/app/logs/*.log
rotation_period: weekly
rotate_count: 4
old_dir: /var/log/archives
create_old_dir: true
compress: true
compression_method: zstd
- name: Disable logrotate configuration
community.general.logrotate:
name: old-service
enabled: false
- name: Remove logrotate configuration
community.general.logrotate:
name: deprecated-app
state: absent
- name: Complex configuration with multiple scripts
community.general.logrotate:
name: complex-app
paths:
- /var/log/complex/*.log
rotation_period: monthly
rotate_count: 6
compress: true
delay_compress: false
pre_rotate:
- "echo 'Starting rotation for complex app'"
- "systemctl stop complex-app"
post_rotate:
- "systemctl start complex-app"
- "echo 'Rotation completed'"
- "logger -t logrotate 'Complex app logs rotated'"
first_action:
- "echo 'First action: Starting batch rotation'"
last_action:
- "echo 'Last action: Batch rotation complete'"
- name: User-specific logrotate configuration
community.general.logrotate:
name: myuser-apps
config_dir: ~/.logrotate.d
paths:
- ~/app/*.log
- ~/.cache/*/*.log
rotation_period: daily
rotate_count: 30
compress: true
su: "{{ ansible_user_id }} users"
- name: Configuration with copy instead of move
community.general.logrotate:
name: copy-config
paths:
- /var/log/copy-app/*.log
rotation_period: daily
rotate_count: 7
copy: true
- name: Configuration with syslog notifications
community.general.logrotate:
name: syslog-config
paths:
- /var/log/syslog-app/*.log
rotation_period: daily
rotate_count: 14
syslog: true
compress: true
- name: Configuration without compression
community.general.logrotate:
name: nocompress-config
paths:
- /var/log/nocompress/*.log
rotation_period: daily
rotate_count: 7
compress: false
- name: Configuration with custom taboo extensions
community.general.logrotate:
name: taboo-config
paths:
- /var/log/taboo/*.log
rotation_period: daily
rotate_count: 7
taboo_ext: [".backup", ".tmp", ".temp"]
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
Path to the backup of the original configuration file, if it was backed up. Returned: success when backup was made Sample: |
|
The generated logrotate configuration content. Returned: success when Sample: |
|
Path to the created/updated logrotate configuration file. Returned: success when Sample: |
|
Current enabled state of the configuration. Returned: success Sample: |