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
1 Answer
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.