Search code examples
ansiblesyslogrsysloglogrotate

Logrotate for rsyslog in Ansible


I'm trying to change rsyslog logrotate configuration using Ansible, but when running task:

- name: Setup logrotate.d scripts
  template:
    src: logrotate.d.j2
    dest: "{{ logrotate_conf_dir }}{{ item.name }}"
  with_items: "{{ logrotate_scripts }}"
  when: logrotate_scripts is defined

Which is adding this kind of configuration:

logrotate_scripts:
  - name: rsyslog
    path: 
        - "/var/log/syslog.log"
        - "/var/log/daemon.log"
        - "/var/log/kern.log"
        - "/var/log/mail.log"
        - "/var/log/user.log"
        - "/var/log/lpr.log"
        - "/var/log/auth.log"
        - "/var/log/cron.log"
        - "/var/log/debug"
        - "/var/log/messages"
    options:
      - daily
      - missingok
      - maxsize 100M
      - rotate 14
      - compress
      - compresscmd /bin/bzip2
      - compressoptions -4
      - compressext .bz2      
      - notifempty

I get this wrong format:

['/var/log/syslog.log', '/var/log/daemon.log', '/var/log/kern.log', '/var/log/mail.log', '/var/log/user.log', '/var/log/lpr.log', '/var/log/auth.log', '/var/log/cron.log', '/var/log/debug', '/var/log/messages'] {
  daily
  missingok
  maxsize 100M
  rotate 14
  compress
  compresscmd /bin/bzip2
  compressoptions -4
  compressext .bz2
  notifempty
  }

This is template I used for all my logrotate scripts (nginx, php and so on), but is not working properly for rsyslog.

{{ item.path }} {
  {% if item.options is defined -%}
  {% for option in item.options -%}
  {{ option }}
  {% endfor -%}
  {% endif %}
  {%- if item.scripts is defined -%}
  {%- for name, script in item.scripts.iteritems() -%}
  {{ name }}
    {{ script }}
  endscript
  {% endfor -%}
  {% endif -%}
}

How should I properly pass list of paths in order to get this effect?

/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
  daily
  missingok
  maxsize 100M
  rotate 14
  compress
  compresscmd /bin/bzip2
  compressoptions -4
  compressext .bz2
  notifempty
  }

Solution

  • You didn't share your template file but you probably want something like this in there:

    {% for path in item.path %}
    path
    {% endfor %}
    

    to get your list of paths.

    Unless you are editing multiple files it seems wrong to use with_items like this. It is probably better to use lineinfile to customise the standard logrotate configuration with your settings.