From b0e5245e4e54ac25232d0c2e0d01191b7a2090ae Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Wed, 9 Sep 2020 16:17:43 +0200 Subject: [PATCH 1/2] Import github.com/arillso/ansible.logrotate at version 1.5.2 (commit 038649f --- defaults/main.yml | 49 ++++++++++++++++++ meta/main.yml | 32 ++++++++++++ tasks/main.yml | 63 ++++++++++++++++++++++++ templates/etc/logrotate.conf.j2 | 30 +++++++++++ templates/etc/logrotate.d/application.j2 | 37 ++++++++++++++ vars/CentOS.yml | 9 ++++ vars/Debian.yml | 9 ++++ vars/RedHat.yml | 9 ++++ vars/Ubuntu.yml | 10 ++++ 9 files changed, 248 insertions(+) create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/etc/logrotate.conf.j2 create mode 100644 templates/etc/logrotate.d/application.j2 create mode 100644 vars/CentOS.yml create mode 100644 vars/Debian.yml create mode 100644 vars/RedHat.yml create mode 100644 vars/Ubuntu.yml diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..239dd3b --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,49 @@ +--- +# List of global options. If this is empty the default options of the +# distribution are used. +logrotate_options: [] + +# Path to the include files +logrotate_include_dir: /etc/logrotate.d + +# package name to install logrotate. +logrotate_package: logrotate + +# Enable hourly rotation with cron. +logrotate_use_hourly_rotation: false + +# logroate for wtmp +logrotate_wtmp: + logs: + - /var/log/wtmp + options: + - missingok + - monthly + - create 0664 root utmp + - rotate 1 + +# logroate for btmp +logrotate_btmp: + logs: + - /var/log/btmp + options: + - missingok + - monthly + - create 0660 root utmp + - rotate 1 + +# More log files can be added that will log rotate. +# An example of multiple log rotate applications with available settings: +# logrotate_applications: +# - name: name-your-log-rotate-application +# definitions: +# - logs: +# - /var/log/apt/term.log +# - /var/log/apt/history.log +# options: +# - rotate 12 +# - monthly +# - missingok +# - notifempty +# - compress +logrotate_applications: [] diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..f417fd8 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,32 @@ +--- +galaxy_info: + author: 'stationgroup' + description: | + Ansible role for installings and configuring lograte on Linux, deploying a list of defaults only if the + logfiles are present on the system. + license: MIT + min_ansible_version: 2.8 + platforms: + - name: EL + versions: + - 6 + - 7 + - name: Fedora + versions: + - 29 + - name: Ubuntu + versions: + - bionic + - cosmic + - disco + - name: Debian + versions: + - jessie + - stretch + - buster + + galaxy_tags: + - system + - logrotate + - log + - rotate diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..6c7621e --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,63 @@ +--- +- name: add OS specific variables + include_vars: '{{ loop_vars }}' + with_first_found: + - files: + - '{{ distribution }}-{{ distribution_version }}.yml' + - '{{ distribution }}-{{ distribution_major_version }}.yml' + - '{{ distribution }}.yml' + - '{{ ansible_os_family }}.yml' + - '{{ ansible_system }}.yml' + - 'defaults.yml' + paths: + - 'vars' + loop_control: + loop_var: loop_vars + vars: + distribution: '{{ ansible_distribution }}' + distribution_version: '{{ ansible_distribution_version }}' + distribution_major_version: '{{ ansible_distribution_major_version }}' + tags: + - configuration + - packages + +- name: 'install logrotate packages' + become: true + package: + name: '{{ logrotate_package }}' + state: present + register: register_install_package + until: register_install_package is succeeded + retries: 3 + tags: + - packages + +- name: 'create logrotate configuration file' + become: true + template: + src: 'etc/logrotate.conf.j2' + dest: '/etc/logrotate.conf' + owner: root + group: root + mode: 0644 + tags: + - configuration + +- name: 'create logrotate application configuration files' + become: true + template: + src: 'etc/logrotate.d/application.j2' + dest: '/etc/logrotate.d/{{ item.name }}' + owner: root + group: root + mode: 0644 + with_items: + - '{{ logrotate_applications }}' + tags: + - configuration + +- name: Symlink for hourly rotation + file: + path: "/etc/cron.hourly/logrotate" + src: "/etc/cron.daily/logrotate" + state: "{{ 'link' if logrotate_use_hourly_rotation else 'absent' }}" diff --git a/templates/etc/logrotate.conf.j2 b/templates/etc/logrotate.conf.j2 new file mode 100644 index 0000000..487181c --- /dev/null +++ b/templates/etc/logrotate.conf.j2 @@ -0,0 +1,30 @@ +{{ ansible_managed | comment }} + +# see "man logrotate" for details +{% if logrotate_options | length > 0 %} +{% for option in logrotate_options %} +{{ option }} +{% endfor %} +{% else %} +{% for option in logrotate_distribution_options | default([]) %} +{{ option }} +{% endfor %} +{% endif %} + +# packages drop log rotation information into this directory +include {{ logrotate_include_dir }} + +# no packages own wtmp, or btmp -- we'll rotate them here +{{ logrotate_wtmp.logs | join(" ") }} { +{% for option in logrotate_wtmp.options %} + {{ option }} +{% endfor %} +} + +{{ logrotate_btmp.logs | join(" ") }} { +{% for option in logrotate_btmp.options %} + {{ option }} +{% endfor %} +} + +# system-specific logs may be configured here diff --git a/templates/etc/logrotate.d/application.j2 b/templates/etc/logrotate.d/application.j2 new file mode 100644 index 0000000..73ab028 --- /dev/null +++ b/templates/etc/logrotate.d/application.j2 @@ -0,0 +1,37 @@ +{{ ansible_managed | comment }} + +{% for definition in item.definitions %} +{{ definition.logs | join(" ") }} { +{% for option in definition.options %} + {{ option }} +{% endfor %} +{% if definition.postrotate|default([]) %} + postrotate +{% for line in definition.postrotate %} + {{ line }} +{% endfor %} + endscript +{% endif %} +{% if definition.preremove|default([]) %} + preremove +{% for line in definition.preremove %} + {{ line }} +{% endfor %} + endscript +{% endif %} +{% if definition.lastaction|default([]) %} + lastaction +{% for line in definition.lastaction %} + {{ line }} +{% endfor %} + endscript +{% endif %} +{% if definition.firstaction|default([]) %} + firstaction +{% for line in definition.firstaction %} + {{ line }} +{% endfor %} + endscript +{% endif %} +} +{% endfor %} diff --git a/vars/CentOS.yml b/vars/CentOS.yml new file mode 100644 index 0000000..84412dc --- /dev/null +++ b/vars/CentOS.yml @@ -0,0 +1,9 @@ +--- +# vars file for arillso.logrotate + +# List of global options for the different systems. +logrotate_distribution_options: + - weekly + - rotate 4 + - create + - dateext diff --git a/vars/Debian.yml b/vars/Debian.yml new file mode 100644 index 0000000..84412dc --- /dev/null +++ b/vars/Debian.yml @@ -0,0 +1,9 @@ +--- +# vars file for arillso.logrotate + +# List of global options for the different systems. +logrotate_distribution_options: + - weekly + - rotate 4 + - create + - dateext diff --git a/vars/RedHat.yml b/vars/RedHat.yml new file mode 100644 index 0000000..84412dc --- /dev/null +++ b/vars/RedHat.yml @@ -0,0 +1,9 @@ +--- +# vars file for arillso.logrotate + +# List of global options for the different systems. +logrotate_distribution_options: + - weekly + - rotate 4 + - create + - dateext diff --git a/vars/Ubuntu.yml b/vars/Ubuntu.yml new file mode 100644 index 0000000..679b0f0 --- /dev/null +++ b/vars/Ubuntu.yml @@ -0,0 +1,10 @@ +--- +# vars file for arillso.logrotate + +# List of global options for the different systems. +logrotate_distribution_options: + - weekly + - rotate 4 + - create + - dateext + - su root syslog From 82d5bd7d168823abfdb1f95d98b319906424d630 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Thu, 17 Sep 2020 22:23:16 +0200 Subject: [PATCH 2/2] Add check to deploy config if log exists Role will now test if a log exist (and hence the application exists on the host before writing a logrotate config to the server. --- README.md | 7 +++ defaults/main.yml | 58 +++++++++++++++---- ...rotate-application-configuration-files.yml | 22 +++++++ tasks/main.yml | 13 ++--- templates/etc/logrotate.d/application.j2 | 22 ++++--- 5 files changed, 90 insertions(+), 32 deletions(-) create mode 100644 tasks/create-logrotate-application-configuration-files.yml diff --git a/README.md b/README.md index f1b6e5f..b69a4c9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # ansible-logrotate-plusplus Ansible role which installs and configures logrotate +It can test if paths exist before writing a logrotate config to the server. +Load the roles default vars with custom paths and per path parameters, and run the playbook across a dynamic +infrastructure and only write logerotate rules to the appropriate system with the correct paths present. + +This project was based of https://github.com/arillso/ansible.logrotate 1.5.2 +(https://github.com/arillso/ansible.logrotate/commit/038649f7933c21ba9f1f2c8363bfb4d49aaf46f2) + diff --git a/defaults/main.yml b/defaults/main.yml index 239dd3b..6c2fea1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -36,14 +36,50 @@ logrotate_btmp: # An example of multiple log rotate applications with available settings: # logrotate_applications: # - name: name-your-log-rotate-application -# definitions: -# - logs: -# - /var/log/apt/term.log -# - /var/log/apt/history.log -# options: -# - rotate 12 -# - monthly -# - missingok -# - notifempty -# - compress -logrotate_applications: [] +# logs: +# - /var/log/apt/term.log +# - /var/log/apt/history.log +# options: +# - rotate 12 +# - monthly +# - missingok +# - notifempty +# - compress +logrotate_applications: + - name: nextcloud-snap-apache-php_errors + logs: + - /var/snap/nextcloud/current/apache/logs/*.log + options: + - daily + - compress + - rotate 6 + - missingok + - copytruncate + - delaycompress + + - name: nextcloud-snap-apache-error_log" + logs: + - /var/snap/nextcloud/current/apache/logs/error_log + options: + - daily + - compress + - rotate 6 + - missingok + - copytruncate + - delaycompress + + - name: custom + logs: + - /var/log/custom/*_log + options: + - daily + - compress + - rotate 6 + + - name : dpkg + logs: + - /var/log/dpkg.log + options: + - daily + - compress + - rotate 6 diff --git a/tasks/create-logrotate-application-configuration-files.yml b/tasks/create-logrotate-application-configuration-files.yml new file mode 100644 index 0000000..10f2ff9 --- /dev/null +++ b/tasks/create-logrotate-application-configuration-files.yml @@ -0,0 +1,22 @@ +--- +- name: 'check if there exist log files for {{ item.name }}' + shell: + cmd: "ls -l {{ item.logs|join(' ') }}" + changed_when: false + register: _available_logs + check_mode: false + failed_when: false + tags: + - configuration + +- name: 'create logrotate configuration file for {{ item.name }}' + become: true + template: + src: 'etc/logrotate.d/application.j2' + dest: '/etc/logrotate.d/{{ item.name }}' + owner: root + group: root + mode: 0644 + when: _available_logs.stdout_lines|length() > 0 + tags: + - configuration diff --git a/tasks/main.yml b/tasks/main.yml index 6c7621e..a7abfc6 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -44,15 +44,10 @@ - configuration - name: 'create logrotate application configuration files' - become: true - template: - src: 'etc/logrotate.d/application.j2' - dest: '/etc/logrotate.d/{{ item.name }}' - owner: root - group: root - mode: 0644 - with_items: - - '{{ logrotate_applications }}' + include_tasks: create-logrotate-application-configuration-files.yml + loop: '{{ logrotate_applications }}' + loop_control: + label: "{{ item.name }}" tags: - configuration diff --git a/templates/etc/logrotate.d/application.j2 b/templates/etc/logrotate.d/application.j2 index 73ab028..8bafa6e 100644 --- a/templates/etc/logrotate.d/application.j2 +++ b/templates/etc/logrotate.d/application.j2 @@ -1,37 +1,35 @@ {{ ansible_managed | comment }} -{% for definition in item.definitions %} -{{ definition.logs | join(" ") }} { -{% for option in definition.options %} +{{ item.logs | join(" ") }} { +{% for option in item.options %} {{ option }} {% endfor %} -{% if definition.postrotate|default([]) %} +{% if item.postrotate|default([]) %} postrotate -{% for line in definition.postrotate %} +{% for line in item.postrotate %} {{ line }} {% endfor %} endscript {% endif %} -{% if definition.preremove|default([]) %} +{% if item.preremove|default([]) %} preremove -{% for line in definition.preremove %} +{% for line in item.preremove %} {{ line }} {% endfor %} endscript {% endif %} -{% if definition.lastaction|default([]) %} +{% if item.lastaction|default([]) %} lastaction -{% for line in definition.lastaction %} +{% for line in item.lastaction %} {{ line }} {% endfor %} endscript {% endif %} -{% if definition.firstaction|default([]) %} +{% if item.firstaction|default([]) %} firstaction -{% for line in definition.firstaction %} +{% for line in item.firstaction %} {{ line }} {% endfor %} endscript {% endif %} } -{% endfor %}