Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(logrotate-ansible): playbook to add logrotate to missing svc #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ansible/logrotate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- hosts: k0s
name: Logrotate k0s systemd unit logs
become: yes
gather_facts: yes
roles:
- role: logrotate
vars:
logrotate_frequency: daily
logrotate_keep: 7
logrotate_compress: yes
logrotate_entries:
- name: k0s
path: "/var/log/k0s/*"
missingok: yes
frequency: 7
create: yes
create_mode: "0660"
create_user: root
create_group: utmp
keep: 1
24 changes: 24 additions & 0 deletions ansible/roles/logrotate/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
# defaults file for logrotate

# How often to rotate logs, either daily, weekly or monthly.
logrotate_frequency: weekly

# How many files to keep.
logrotate_keep: 4

# Should rotated logs be compressed??
logrotate_compress: yes

# User/Group for rotated log files (Loaded by OS-Specific vars if found, or and can be set manually)
logrotate_user: "{{ _logrotate_user[ansible_distribution] | default(_logrotate_user['default'] ) }}"
logrotate_group: "{{ _logrotate_group[ansible_distribution] | default(_logrotate_group['default'] ) }}"

logrotate_packages:
- logrotate

logrotate_config_directory: /etc

logrotate_config_file: logrotate.conf

logrotate_confd_directory: "{{ logrotate_config_directory }}/logrotate.d"
29 changes: 29 additions & 0 deletions ansible/roles/logrotate/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
# tasks file for logrotate

- name: import assert.yml
ansible.builtin.import_tasks: assert.yml
run_once: yes
delegate_to: localhost

- name: install logrotate
ansible.builtin.package:
name: "{{ logrotate_packages }}"
state: present

- name: configure logrotate
ansible.builtin.template:
src: "{{ logrotate_config_file }}.j2"
dest: "{{ logrotate_config_directory }}/{{ logrotate_config_file }}"
mode: "0644"

- name: generate logrotate.d files
ansible.builtin.template:
src: entry.j2
dest: "{{ logrotate_confd_directory }}/{{ item.name }}"
mode: "0644"
loop: "{{ logrotate_entries }}"
when:
- logrotate_entries is defined
loop_control:
label: "{{ item.name }}"
88 changes: 88 additions & 0 deletions ansible/roles/logrotate/tasks/pool.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
- name: manage_zfs | checking existing zpool(s)
shell: "zpool list | awk 'FNR >1' | awk '{print $1}'"
changed_when: false
register: zpools
when: zfs_pools is defined
ignore_errors: "{{ ansible_check_mode }}"

- name: manage_zfs | Gather ZPool Status
shell: zpool status
changed_when: false
register: zpool_devices
when: zfs_pools is defined
ignore_errors: "{{ ansible_check_mode }}"

- name: manage_zfs | creating basic zpool(s)
command: "zpool create {{ item.options | join (' ') if item.options is defined else '' }} {{ item.name }} {{ item.devices|join (' ') }}"
register: zpool_created
with_items: "{{ zfs_pools }}"
when: >
zfs_pools is defined and
zfs_create_pools and
(item.type == "basic" and
item.name not in zpools.stdout and
item.state == "present") and
item.devices[0] not in zpool_devices.stdout and
item.action|lower == "create"
ignore_errors: "{{ ansible_check_mode }}"

- name: manage_zfs | adding basic zpool(s)
command: "zpool add {{ '-o' if zfs_pool_options else '' }} {{ zfs_pool_options | join(' -o ') }} {{ item.name }} {{ item.devices|join (' ') }}"
with_items: "{{ zfs_pools }}"
when: >
zfs_pools is defined and
zfs_create_pools and
(item.type == "basic" and
item.state == "present") and
(item.devices[0]|basename) not in zpool_devices.stdout and
item.action|lower == "add" and
(zpool_created.changed or item.name in zpools.stdout)
ignore_errors: "{{ ansible_check_mode }}"

- name: manage_zfs | creating mirror/raidz zpool(s)
command: "zpool create {{ '-o' if zfs_pool_options else '' }} {{ item.name }} {{ item.type }} {{ item.devices|join (' ') }}"
with_items: "{{ zfs_pools }}"
register: zpool_created
when: >
zfs_pools is defined and
zfs_create_pools and
(item.type != "basic" and
item.name not in zpools.stdout and
item.state == "present") and
item.devices[0] not in zpool_devices.stdout and
item.action|lower == "create"
ignore_errors: "{{ ansible_check_mode }}"

- name: manage_zfs | adding mirror/zraid zpool(s)
command: "zpool add {{ '-o' if zfs_pool_options else '' }} {{ zfs_pool_options | join(' -o ') }} {{ item.name }} {{ item.type }} {{ item.devices|join (' ') }}"
with_items: "{{ zfs_pools }}"
when: >
zfs_pools is defined and
zfs_create_pools and
(item.type != "basic" and
item.state == "present") and
(item.devices[0]|basename) not in zpool_devices.stdout and
item.action|lower == "add" and
(zpool_created.changed or item.name in zpools.stdout)
ignore_errors: "{{ ansible_check_mode }}"

- name: manage_zfs | managing pools
community.general.zfs:
name: "{{ item.name }}"
extra_zfs_properties:
atime: "{{ item.atime|default(omit) }}"
compression: "{{ item.compression|default(omit) }}"
dedup: "{{ item.dedup|default(omit) }}"
logbias: "{{ item.logbias|default(omit) }}"
mountpoint: "{{ item.mountpoint|default(omit) }}"
primarycache: "{{ item.primarycache|default(omit) }}"
quota: "{{ item.quota|default(omit) }}"
recordsize: "{{ item.recordsize|default(omit) }}"
sharenfs: "{{ item.sharenfs|default(omit) }}"
sync: "{{ item.sync|default(omit) }}"
snapdev: "{{ item.snapdev|default('hidden')}}"
snapdir: "{{ item.snapdir|default('hidden')}}"
state: "{{ item.state }}"
with_items: "{{ zfs_pools }}"
when: zfs_create_pools
27 changes: 27 additions & 0 deletions ansible/roles/logrotate/templates/entry.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


{{ ansible_managed | comment }}

{{ item.path }} {

{% if item.frequency is defined %} {{ item.frequency }}{% endif %}

{% if item.compress is defined and item.compress %} compress{% endif %}

{% if item.keep is defined %} rotate {{ item.keep }}{% endif %}

{% if item.minsize is defined %} minsize {{ item.minsize }}{% endif %}

{% if item.missingok is defined and item.missingok %} missingok{% endif %}

{% if item.notifempty is defined and item.notifempty %} notifempty{% endif %}

{% if item.create is defined and item.create %} create{% if item.create_mode is defined %} {{ item.create_mode }}{% endif %}{% if item.create_user is defined %} {{ item.create_user }}{% endif %}{% if item.create_group is defined %} {{ item.create_group }}{% endif %}{% endif %}

{% if item.sharedscripts is defined and item.sharedscripts %} sharedscripts{% endif %}

{% if item.postrotate is defined %} postrotate
{{ item.postrotate }}
endscript{% endif %}

}
27 changes: 27 additions & 0 deletions ansible/roles/logrotate/templates/logrotate.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{ ansible_managed | comment }}

# see "man logrotate" for details
# rotate log files weekly
{{ logrotate_frequency }}

# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su {{ logrotate_user }} {{ logrotate_group }}

# keep 4 weeks worth of backlogs
rotate {{ logrotate_keep }}

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
{% if logrotate_compress %}
compress
{% else %}
#compress
{% endif %}

# packages drop log rotation information into this directory
include /etc/logrotate.d

# system-specific logs may be configured here