|
|
|
@@ -1,4 +1,62 @@
|
|
|
|
|
---
|
|
|
|
|
- name: Check current swap file size
|
|
|
|
|
stat:
|
|
|
|
|
path: "{{ swap_file_path }}"
|
|
|
|
|
register: swap_stat
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Check if swap is active
|
|
|
|
|
command: swapon --show
|
|
|
|
|
register: swapon_status
|
|
|
|
|
changed_when: false
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Turn off swap before resize
|
|
|
|
|
command: "swapoff {{ swap_file_path }}"
|
|
|
|
|
when: swap_stat.stat.exists and (swap_stat.stat.size != (swap_file_size_gb | int * 1024 * 1024 * 1024)) and (swap_file_path in swapon_status.stdout)
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Create or resize swap file
|
|
|
|
|
command: "fallocate -l {{ swap_file_size_gb }}G {{ swap_file_path }}"
|
|
|
|
|
when: not swap_stat.stat.exists or (swap_stat.stat.size != (swap_file_size_gb | int * 1024 * 1024 * 1024))
|
|
|
|
|
become: yes
|
|
|
|
|
register: swap_resized
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Set permissions on swap file
|
|
|
|
|
file:
|
|
|
|
|
path: "{{ swap_file_path }}"
|
|
|
|
|
mode: '0600'
|
|
|
|
|
owner: root
|
|
|
|
|
group: root
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Format swap file (if resized or new)
|
|
|
|
|
command: "mkswap {{ swap_file_path }}"
|
|
|
|
|
when: swap_resized.changed
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Enable swap file
|
|
|
|
|
command: "swapon {{ swap_file_path }}"
|
|
|
|
|
when: swap_resized.changed
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Ensure swap is in fstab
|
|
|
|
|
mount:
|
|
|
|
|
name: none
|
|
|
|
|
src: "{{ swap_file_path }}"
|
|
|
|
|
fstype: swap
|
|
|
|
|
opts: defaults
|
|
|
|
|
passno: 0
|
|
|
|
|
dump: 0
|
|
|
|
|
state: present
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [swap, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Optimize kernel parameters
|
|
|
|
|
sysctl:
|
|
|
|
|
name: "{{ item.name }}"
|
|
|
|
@@ -9,6 +67,9 @@
|
|
|
|
|
- { name: 'vm.swappiness', value: '10' }
|
|
|
|
|
- { name: 'fs.inotify.max_user_watches', value: '524288' }
|
|
|
|
|
- { name: 'vm.vfs_cache_pressure', value: '50' }
|
|
|
|
|
- { name: 'vm.dirty_ratio', value: '10' }
|
|
|
|
|
- { name: 'vm.dirty_background_ratio', value: '5' }
|
|
|
|
|
- { name: 'vm.watermark_scale_factor', value: '100' }
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [optimization, sysctl]
|
|
|
|
|
|
|
|
|
@@ -18,4 +79,68 @@
|
|
|
|
|
regexp: '^#?KillUserProcesses='
|
|
|
|
|
line: 'KillUserProcesses=no'
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [systemd, optimization]
|
|
|
|
|
tags: [systemd, optimization]
|
|
|
|
|
|
|
|
|
|
- name: Ensure systemd directories exist
|
|
|
|
|
file:
|
|
|
|
|
path: "{{ item }}"
|
|
|
|
|
state: directory
|
|
|
|
|
with_items:
|
|
|
|
|
- /etc/systemd/system/user.slice.d
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [oomd, systemd, stability]
|
|
|
|
|
|
|
|
|
|
- name: Configure ManagedOOM for user.slice
|
|
|
|
|
template:
|
|
|
|
|
src: 10-oomd-user.slice.conf.j2
|
|
|
|
|
dest: /etc/systemd/system/user.slice.d/10-oomd-user.slice.conf
|
|
|
|
|
become: yes
|
|
|
|
|
notify: Reload systemd
|
|
|
|
|
tags: [oomd, systemd, stability]
|
|
|
|
|
|
|
|
|
|
- name: Enable systemd-oomd
|
|
|
|
|
systemd:
|
|
|
|
|
name: systemd-oomd
|
|
|
|
|
enabled: yes
|
|
|
|
|
state: started
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [systemd, oomd, stability]
|
|
|
|
|
|
|
|
|
|
- name: Configure kernel features in GRUB (Safe script)
|
|
|
|
|
shell: |
|
|
|
|
|
# Extract current cmdline
|
|
|
|
|
CURRENT_CMDLINE=$(grep '^GRUB_CMDLINE_LINUX_DEFAULT=' /etc/default/grub | cut -d'"' -f2)
|
|
|
|
|
|
|
|
|
|
# Remove our target parameters to avoid duplicates
|
|
|
|
|
NEW_CMDLINE=$(echo "$CURRENT_CMDLINE" | sed -E 's/\bzswap\.(enabled|max_pool_percent)=[0-9]+\b//g' | sed -E 's/\btransparent_hugepage=[a-z]+\b//g' | xargs)
|
|
|
|
|
|
|
|
|
|
# Append the desired parameters
|
|
|
|
|
FINAL_CMDLINE="$NEW_CMDLINE zswap.enabled=1 zswap.max_pool_percent=30 transparent_hugepage=madvise"
|
|
|
|
|
|
|
|
|
|
# Update the file if changed
|
|
|
|
|
if [ "$CURRENT_CMDLINE" != "$FINAL_CMDLINE" ]; then
|
|
|
|
|
sed -i "s|^GRUB_CMDLINE_LINUX_DEFAULT=.*|GRUB_CMDLINE_LINUX_DEFAULT=\"$FINAL_CMDLINE\"|" /etc/default/grub
|
|
|
|
|
echo "changed"
|
|
|
|
|
fi
|
|
|
|
|
become: yes
|
|
|
|
|
register: grub_cmdline_safe
|
|
|
|
|
changed_when: grub_cmdline_safe.stdout == "changed"
|
|
|
|
|
tags: [grub, boot, optimization, stability]
|
|
|
|
|
|
|
|
|
|
- name: Apply GRUB changes (Update GRUB)
|
|
|
|
|
command: update-grub
|
|
|
|
|
when: grub_cmdline_safe.changed
|
|
|
|
|
become: yes
|
|
|
|
|
tags: [grub, boot, optimization, stability]
|
|
|
|
|
|
|
|
|
|
- name: Set runtime Transparent Hugepages to madvise
|
|
|
|
|
shell: echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
|
|
|
|
|
become: yes
|
|
|
|
|
changed_when: false
|
|
|
|
|
tags: [optimization, thp]
|
|
|
|
|
|
|
|
|
|
- name: Set runtime zswap max_pool_percent
|
|
|
|
|
shell: echo 30 > /sys/module/zswap/parameters/max_pool_percent
|
|
|
|
|
become: yes
|
|
|
|
|
changed_when: false
|
|
|
|
|
tags: [optimization, zswap]
|