Enables swap

Fixes wrong path in swapfile
This commit is contained in:
Maximilian Kratz 2023-03-30 09:08:40 +02:00
parent c389eb67c0
commit ce819aebda
3 changed files with 45 additions and 0 deletions

View File

@ -24,3 +24,5 @@
- role: firewall
when: "role_config.firewall_enable"
become: true
- role: swap
become: true

View File

@ -33,3 +33,10 @@ migration_url: "s3-mig.forgejo.dev"
forgejo_ssh_port: "22"
forgejo_release: "1.19.0-2"
#
# Swap config
#
swap_size: "2048"
swap_file: "/swapfile"

36
roles/swap/tasks/main.yml Normal file
View File

@ -0,0 +1,36 @@
---
- name: Check if the swap file exists
ansible.builtin.stat:
path: "{{ swap_file }}"
register: swap_file_check
- name: Create the swap file
ansible.builtin.command: fallocate -l {{ swap_size }}M {{ swap_file }}
when: not swap_file_check.stat.exists
- name: Create the wap space
ansible.builtin.command: dd if=/dev/zero of={{ swap_file }} bs=1M count={{ swap_size }}
when: not swap_file_check.stat.exists
- name: Set permissions on swap file
ansible.builtin.file:
path: "{{ swap_file }}"
mode: 0600
- name: Format the swap file
ansible.builtin.command: mkswap {{ swap_file }}
when: not swap_file_check.stat.exists
- name: Add swap to fstab
ansible.builtin.lineinfile:
dest: /etc/fstab
regexp: "{{ swap_file }}"
line: "{{ swap_file }} none swap sw 0 0"
- name: Turn on swap
ansible.builtin.command: swapon -a
- name: Set swapiness level
ansible.posix.sysctl:
name: vm.swappiness
value: "60"