diff --git a/ansible/logrotate.yaml b/ansible/logrotate.yaml new file mode 100644 index 0000000..09da61e --- /dev/null +++ b/ansible/logrotate.yaml @@ -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 \ No newline at end of file diff --git a/ansible/roles/logrotate/defaults/main.yml b/ansible/roles/logrotate/defaults/main.yml new file mode 100644 index 0000000..921381b --- /dev/null +++ b/ansible/roles/logrotate/defaults/main.yml @@ -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" diff --git a/ansible/roles/logrotate/tasks/main.yml b/ansible/roles/logrotate/tasks/main.yml new file mode 100644 index 0000000..297b3fe --- /dev/null +++ b/ansible/roles/logrotate/tasks/main.yml @@ -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 }}" \ No newline at end of file diff --git a/ansible/roles/logrotate/tasks/pool.yml b/ansible/roles/logrotate/tasks/pool.yml new file mode 100644 index 0000000..c68c312 --- /dev/null +++ b/ansible/roles/logrotate/tasks/pool.yml @@ -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 diff --git a/ansible/roles/logrotate/templates/entry.j2 b/ansible/roles/logrotate/templates/entry.j2 new file mode 100644 index 0000000..1dd0d09 --- /dev/null +++ b/ansible/roles/logrotate/templates/entry.j2 @@ -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 %} + +} \ No newline at end of file diff --git a/ansible/roles/logrotate/templates/logrotate.conf.j2 b/ansible/roles/logrotate/templates/logrotate.conf.j2 new file mode 100644 index 0000000..e8adca3 --- /dev/null +++ b/ansible/roles/logrotate/templates/logrotate.conf.j2 @@ -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 \ No newline at end of file