在 Ansible 中为 rsyslog 进行 Logrotate

Logrotate for rsyslog in Ansible

我正在尝试使用 Ansible 更改 rsyslog logrotate 配置,但是当 运行 任务:

- 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

这是在添加这样的配置:

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

我的格式有误:

['/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
  }

这是我用于所有 logrotate 脚本(nginx、php 等)的模板,但不适用于 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 -%}
}

我应该如何正确传递路径列表才能获得这种效果?

/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
  }

你没有分享你的模板文件,但你可能想要这样的东西:

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

获取路径列表。

除非您正在编辑多个文件,否则这样使用 with_items 似乎是错误的。最好使用 lineinfile 根据您的设置自定义标准 logrotate 配置。