【问题标题】:Logrotate for rsyslog in Ansible在 Ansible 中为 rsyslog 进行 Logrotate
【发布时间】:2020-05-19 15:36:23
【问题描述】:

我正在尝试使用 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
  }

【问题讨论】:

    标签: ansible syslog rsyslog logrotate


    【解决方案1】:

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

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

    获取您的路径列表。

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

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 2013-07-31
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2018-01-05
      • 2014-11-06
      • 2014-12-24
      • 2012-12-18
      相关资源
      最近更新 更多