From ce819aebdac5acb1b6515f0066288b04f0f881aa Mon Sep 17 00:00:00 2001 From: Maximilian Kratz Date: Thu, 30 Mar 2023 09:08:40 +0200 Subject: [PATCH] Enables swap Fixes wrong path in swapfile --- infra.yaml | 2 ++ inventory/group_vars/all.yml | 7 +++++++ roles/swap/tasks/main.yml | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 roles/swap/tasks/main.yml diff --git a/infra.yaml b/infra.yaml index f710784..17a3e36 100644 --- a/infra.yaml +++ b/infra.yaml @@ -24,3 +24,5 @@ - role: firewall when: "role_config.firewall_enable" become: true + - role: swap + become: true diff --git a/inventory/group_vars/all.yml b/inventory/group_vars/all.yml index 2a922a1..f5e0e00 100644 --- a/inventory/group_vars/all.yml +++ b/inventory/group_vars/all.yml @@ -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" diff --git a/roles/swap/tasks/main.yml b/roles/swap/tasks/main.yml new file mode 100644 index 0000000..0216774 --- /dev/null +++ b/roles/swap/tasks/main.yml @@ -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"