so i would like to create a ansible playbook that installs logrotate on all the servers in company. Also configs them to to set logs to be backed up weekly and then deleted a week after. So each week it makes a new log, backups last week log and on the third week it deletes the first one and repeats.
So far i have found this but we do not use nginx. And it does not do exactly what i want. My knowledge in playbooks is quite limited so if someone could help with it would be awesome. Also i need it to check if the server has tomcat, apache or wildfly and then takes those logs.
logrotate_scripts:
- name: nginx-options
path: /var/log/nginx/options.log
options:
- daily
- weekly
- size 25M
- rotate 7
- missingok
- compress
- delaycompress
- copytruncate
logrotate Ansible role allows you to manage log rotation configuration for system packages, or create custom log configuration. The role can be used by other roles as a dependency to make automatic logrotate configuration easier.
The postrotate command tells logrotate that the script to run, starts on the next line, and the endscript command says that the script is done.
missingok : If the log file is missing, go on to the next one without issuing an error message. noolddir : Logs are rotated in the same directory the log normally resides in (this overrides the olddir option). daily : Log files are rotated every day.
logrotate maxsize <MAX-SIZE> no logrotate maxsize. Description. Specifies the maximum allowed log file size. The no form of this command resets the size of the log file to the default (100 MB). Parameter.
Let's use blockinfile. For example the task
- blockinfile:
path: "/etc/logrotate.d/{{ item.path }}"
block: "{{ item.conf }}"
create: true
loop: "{{ lp_logrotate_confd }}"
with the variable
lp_logrotate_confd:
- path: ansible
conf: |
/var/log/ansible.log {
weekly
rotate 3
size 10M
compress
delaycompress
}
creates
shell> cat /etc/logrotate.d/ansible
# BEGIN ANSIBLE MANAGED BLOCK
/var/log/ansible.log {
weekly
rotate 3
size 10M
compress
delaycompress
}
# END ANSIBLE MANAGED BLOCK
Add items to the list and fit the configuration data to your needs. For your convenience, the code is available at GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With