diff --git a/files/usr/local/lib/systemd/system/zramswap.service b/files/usr/local/lib/systemd/system/zramswap.service new file mode 100644 index 0000000000000000000000000000000000000000..c5d9a4b3d77b081edfe235c811c82540fce4c8c2 --- /dev/null +++ b/files/usr/local/lib/systemd/system/zramswap.service @@ -0,0 +1,13 @@ +[Unit] +Description=Swap with zram +After=multi-user.target + +[Service] +Type=oneshot +RemainAfterExit=true +ExecStartPre=/sbin/mkswap /dev/zram0 +ExecStart=/sbin/swapon /dev/zram0 +ExecStop=/sbin/swapoff /dev/zram0 + +[Install] +WantedBy=multi-user.target diff --git a/handlers/main.yml b/handlers/main.yml index e975c979b522f45ec0d0c2d5ff1980c8f3c97e4e..0f715641790daf5554c32ec00606fdbd7ec1cce5 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -64,5 +64,11 @@ when: ansible_os_family == "Debian" changed_when: false +- name: restart zramswap + ansible.builtin.systemd: + name: "zramswap.service" + state: restarted + daemon_reload: true + - name: udev-Regel bekannt machen ansible.builtin.command: "udevadm control --reload" diff --git a/tasks/configure_swap.yml b/tasks/configure_swap.yml index 6e7d7db04f9ab0f901e087c3413bc95bfe589be8..caebbbff7dcfd3deb189d7fc034ba594f9afa784 100644 --- a/tasks/configure_swap.yml +++ b/tasks/configure_swap.yml @@ -1,23 +1,77 @@ --- -- name: check if swap is active - ansible.builtin.command: swapon -s - register: swap_active - changed_when: false +- name: configure zram based swap (Debian) + block: + - name: install zram + ansible.builtin.package: + name: "zram-tools" + state: latest + - name: configure zram + ansible.builtin.blockinfile: + path: "/etc/default/zramswap" + block: | + ALGO=lz4 + PERCENT=50 + notify: restart zramswap + when: ansible_os_family == "Debian" -# https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings -- name: switch off swap (immediate result for running server, not reboot persistent) - ansible.builtin.command: swapoff -va - when: - - ansible_hostname is not search("validate") - - swap_active.stdout | length == 0 - register: disable_swap - changed_when: disable_swap.stdout not in "swapoff LABEL=swap" +# RHEL part is based on https://www.techrepublic.com/article/how-to-enable-zram-rocky-linux/ +# Note: all that udev stuff from that guide doesn't work. We'll use CLI commands. +# More docu on zram at https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html +- name: configure zram based swap (RedHat) + block: + - name: disable swapping first, otherwise zram will not work + ansible.builtin.command: "swapoff -a" + changed_when: false + - name: switch off swap (no result for running server, reboot persistent) + ansible.posix.mount: + path: "none" + fstype: "swap" + state: "absent" -- name: switch off swap (no result for running server, reboot persistent) - ansible.posix.mount: - path: "none" - fstype: "swap" - state: "absent" - when: - - ansible_hostname is not search("validate") - - swap_active.stdout | length == 0 + - name: load zram Kernel module + ansible.builtin.lineinfile: + path: "/etc/modules-load.d/zram.conf" + create: true + line: "zram" + mode: "0644" + # Use `zramctl -f` to show available zram devices. + - name: configure zram Kernel module. This will create a zram device. + ansible.builtin.lineinfile: + path: "/etc/modprobe.d/zram.conf" + create: true + line: "options zram num_devices=1" + mode: "0644" + - name: find out if zram Kernel module has been loaded already + ansible.builtin.command: "lsmod" + register: kernel_modules + changed_when: false + - name: load zram Kernel module + ansible.builtin.command: "modprobe zram" + when: "not 'zram' in kernel_modules.stdout" + - name: find existing zram devices + ansible.builtin.command: "zramctl --noheadings" + register: "zram_devices" + changed_when: false + - name: setup zram device + ansible.builtin.command: "zramctl -a lzo -s {{ ( ansible_facts.memtotal_mb / 2 ) | round | int }}M /dev/zram0" + changed_when: false + - name: create SystemD unit directory + ansible.builtin.file: + path: "/usr/local/lib/systemd/system/" + state: directory + mode: "0755" + - name: create Systemd unit for zram + ansible.builtin.copy: + src: "usr/local/lib/systemd/system/zramswap.service" + dest: "/usr/local/lib/systemd/system/zramswap.service" + mode: "0644" + notify: restart zramswap + when: ansible_os_family == "RedHat" + +- name: configure zram based swap (common) + block: + - name: start zram service + ansible.builtin.systemd: + name: "zramswap.service" + state: started + enabled: true diff --git a/tasks/main.yml b/tasks/main.yml index 785d766ddd4f12e58e8738282ff897b4a1a30daa..fe6f7a79ee58d86bd5a1deb8b7ebd89ebbd9f6b5 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -112,9 +112,8 @@ import_tasks: configure_sudoers.yml tags: [sudo] -- name: Disable swap +- name: Configure swap import_tasks: configure_swap.yml - when: ansible_hostname != "sdvlzavalidate" tags: [swap, vm] - name: sar konfigurieren