Skip to content

Instantly share code, notes, and snippets.

@neofob
Last active January 15, 2026 18:41
Show Gist options
  • Select an option

  • Save neofob/a01571b39108416af19082407dfc67f4 to your computer and use it in GitHub Desktop.

Select an option

Save neofob/a01571b39108416af19082407dfc67f4 to your computer and use it in GitHub Desktop.
zram Ansible playbook
---
- name: Manual ZRAM Setup
hosts: your_servers
become: yes
tasks:
- name: Install zram tools (Debian/Ubuntu example)
ansible.builtin.apt:
name: zram-tools
state: present
- name: Configure zram-generator
ansible.builtin.copy:
content: |
[zram0]
zram-fraction = 0.5
max-zram-size = 1024 # Example: 1GB
dest: /etc/systemd/zram-generator.conf
notify: Restart zram service
- name: Configure sysctl for memory management
ansible.builtin.copy:
content: |
vm.swappiness=100
vm.vfs_cache_pressure=500
vm.oom_kill_allocating_task=1
dest: /etc/sysctl.d/99-zram-memory.conf
notify: Apply sysctl settings
- name: Ensure zram service is running and enabled (if applicable)
ansible.builtin.systemd:
name: systemd-zram-setup.service
state: started
enabled: yes
ignore_errors: yes # May not exist on all systems
handlers:
- name: Restart zram service
ansible.builtin.systemd:
name: systemd-zram-setup.service
state
---
# source: https://git.slub-dresden.de/digital-preservation/ansible_lza_install_common/-/blob/e05d20c9e7cea515d3bc2b3016e6c16829bb5a97/tasks/configure_swap.yml
- name: configure zram based swap (Debian)
when: ansible_os_family == "Debian"
block:
- name: install zram
ansible.builtin.package:
name: "zram-tools"
state: latest
- name: configure zram based swap (Ubuntu)
ansible.builtin.package:
name: [ "linux-image-generic" ]
state: latest
when: ansible_distribution == "Ubuntu"
notify: reboot
- name: >
Reboot instantly if linux-modules-extra-*-generic were newly installed.
Otherwise, Ubuntu cannot load the zram.ko kernel module.
ansible.builtin.meta: flush_handlers
- name: configure zram
ansible.builtin.blockinfile:
path: "/etc/default/zramswap"
block: |
ALGO=lz4
PERCENT=50
notify: restart zramswap
# RHEL part is based on https://www.techrepublic.com/article/how-to-enable-zram-rocky-linux/
# More docu on zram at https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html
- name: configure zram based swap (RedHat)
when: ansible_os_family == "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: 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: configure udev to initialise zram device on bootup
ansible.builtin.lineinfile:
path: "/etc/udev/rules.d/99-zram.rules"
create: true
line: "KERNEL==\"zram0\", ATTR{disksize}=\"{{ (ansible_facts.memtotal_mb / 2) | round | int }}M\",TAG+=\"systemd\""
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"
changed_when: false
- 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
- name: configure zram based swap (common)
block:
- name: start zram service
ansible.builtin.systemd_service:
name: "zramswap.service"
daemon_reload: true
state: started
enabled: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment