robertdebock.logrotate

Ansible角色 logrotate

在您的系统上安装和配置 logrotate。

GitHub GitLab 下载量 版本
github gitlab downloads 版本

示例剧本

这个示例来自于 molecule/default/converge.yml,并在每次推送、拉取请求和发布时进行测试。

---
- name: Converge
  hosts: all
  become: true
  gather_facts: true

  vars:
    logrotate_frequency: daily
    logrotate_keep: 7
    logrotate_compress: true
    logrotate_entries:
      - name: example
        path: "/var/log/example/*.log"
      - name: example-frequency
        path: "/var/log/example-frequency/*.log"
        frequency: weekly
      - name: example-keep
        path: "/var/log/example-keep/*.log"
        keep: 14
      - name: example-compress
        path: "/var/log/example-compress/*.log"
        compress: true
      - name: example-copylog
        path: "/var/log/example-copylog/*.log"
        copylog: true
      - name: example-copytruncate
        path: "/var/log/example-copytruncate/*.log"
        copytruncate: true
      - name: example-delaycompress
        path: "/var/log/example-delaycompress/*.log"
        delaycompress: true
      - name: example-script
        path: "/var/log/example-script/*.log"
        postrotate: killall -HUP some_process_name
      - name: btmp
        path: /var/log/btmp
        missingok: true
        frequency: monthly
        create: true
        create_mode: "0660"
        create_user: root
        create_group: utmp
        dateext: true
        dateformat: "-%Y-%m-%d"
        keep: 1
      - name: wtmp
        path: /var/log/wtmp
        missingok: true
        frequency: monthly
        create: true
        create_mode: "0664"
        create_user: root
        create_group: utmp
        minsize: 1M
        maxsize: 128M
        dateext: true
        dateformat: "-%Y%m%d"
        keep: 1
      - name: dnf
        path: /var/log/hawkey.log
        missingok: true
        notifempty: true
        keep: 4
        frequency: weekly
        create: true
      - name: example-sharedscripts
        path: "/var/log/example-sharedscripts/*.log"
        sharedscripts: true
      - name: example-dateyesterday
        state: present
        path: "/var/log/example-dateyesterday/*.log"
        dateyesterday: true
      - name: example-absent
        state: absent
      # 负数在某些发行版中有效: `error: example-negative:10 bad rotation count '-1'\`
      # - name: example-negative
      #   path: "/var/log/example-keep-negative/*.log"
      #   keep: -1

  roles:
    - role: robertdebock.logrotate

机器需要准备。在 CI 中,这使用 molecule/default/prepare.yml 完成:

---
- name: Prepare
  hosts: all
  become: true
  gather_facts: false

  roles:
    - role: robertdebock.bootstrap
    - role: robertdebock.cron

  post_tasks:
    - name: 创建日志目录
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        mode: "0755"
      loop:
        - /var/log/example
        - /var/log/example-frequency
        - /var/log/example-keep
        - /var/log/example-compress
        - /var/log/example-copylog
        - /var/log/example-copytruncate
        - /var/log/example-delaycompress
        - /var/log/example-script
        - /var/log/example-sharedscripts
        - /var/log/example-dateyesterday

    - name: 创建日志文件
      ansible.builtin.copy:
        dest: "{{ item }}"
        content: "example"
        mode: "0644"
      loop:
        - /var/log/example/app.log
        - /var/log/example-frequency/app.log
        - /var/log/example-keep/app.log
        - /var/log/example-compress/app.log
        - /var/log/example-copylog/app.log
        - /var/log/example-copytruncate/app.log
        - /var/log/example-delaycompress/app.log
        - /var/log/example-script/app.log
        - /var/log/example-sharedscripts/app.log
        - /var/log/example-dateyesterday/app.log
        - /var/log/btmp
        - /var/log/wtmp
        - /var/log/hawkey.log

还可以参阅完整的说明和示例以了解如何使用这些角色。

角色变量

变量的默认值设置在 defaults/main.yml:

---
# logrotate 的默认文件

# 日志旋转的频率,可以是 daily、weekly 或 monthly。
logrotate_frequency: weekly

# 要保留的文件数量。
logrotate_keep: 4

# 是否应该压缩旋转的日志?
logrotate_compress: true

# 日志文件名上是否使用日期扩展
logrotate_dateext: false

# 旋转日志文件的用户/组(如果找到,将通过操作系统特定变量加载,或可以手动设置)
logrotate_user: "{{ _logrotate_user[ansible_distribution] | default(_logrotate_user['default']) }}"
logrotate_group: "{{ _logrotate_group[ansible_distribution] | default(_logrotate_group['default']) }}"

# 设置服务的状态
logrotate_service_state: "started"
logrotate_service_enabled: true

要求

使用的角色状态

以下角色用于准备一个系统。您可以用其他方式准备系统。

需求 GitHub GitLab
robertdebock.bootstrap Build Status GitHub Build Status GitLab
robertdebock.cron Build Status GitHub Build Status GitLab

上下文

此角色是多个兼容角色的一部分。有关更多信息,请查看这些角色的文档

以下是相关角色的概述: 依赖关系

兼容性

此角色已在以下容器镜像 上进行测试:

容器 标签
Alpine 所有
Amazon 候选
EL 9
Debian 所有
Fedora 所有
Ubuntu 所有

所需的最小 Ansible 版本为 2.12,已进行以下测试:

  • 先前版本。
  • 当前版本。
  • 开发版本。

如果您发现问题,请在GitHub上报告它们。

许可证

Apache-2.0

作者信息

robertdebock

请考虑赞助我

关于项目

Install and configure logrotate on your system.

安装
ansible-galaxy install robertdebock.logrotate
许可证
apache-2.0
下载
196.4k
拥有者
I know my way around (Linux) infrastructure, have a passion for automation, Docker, Ansible, Molecule and ci/cd.