Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logrotate with ansible playbook

Tags:

ansible

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
like image 722
Mairold Kunimägi Avatar asked Oct 15 '19 07:10

Mairold Kunimägi


People also ask

What is logrotate in Ansible?

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.

What is Postrotate in logrotate?

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.

What is Missingok in logrotate?

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.

What is Maxsize in logrotate?

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.


1 Answers

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.

like image 184
Vladimir Botka Avatar answered Oct 19 '22 22:10

Vladimir Botka