Added swapfile role.
authorLuke Hoersten <luke@hoersten.org>
Sat, 10 Feb 2024 15:41:05 -0600
changeset 230 07f08cc98da0
parent 229 7e3bf48e516a
child 231 e3ebf2a606ea
Added swapfile role.
swapfile/tasks/main.yaml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/swapfile/tasks/main.yaml	Sat Feb 10 15:41:05 2024 -0600
@@ -0,0 +1,38 @@
+---
+
+- name: check if swap file exists
+  stat:
+    path: "{{swapfile_path}}"
+  register: swapfile_check
+
+- name: create swap file
+  become: yes
+  command: "fallocate -l {{swapfile_size}} {{swapfile_path}}"
+  when: not swapfile_check.stat.exists
+
+- name: set swap file permissions
+  become: yes
+  file:
+    path: "{{swapfile_path}}"
+    mode: "0600"
+
+- name: format swap file
+  become: yes
+  command: "mkswap {{swapfile_path}}"
+  when: not swapfile_check.stat.exists
+
+- name: write swap entry in fstab
+  become: yes
+  mount:
+    name: "none"
+    src: "{{swapfile_path}}"
+    fstype: "swap"
+    opts: "sw"
+    passno: 0
+    dump: 0
+    state: "present"
+
+- name: turn on swap
+  become: yes
+  command: "swapon {{swapfile_path}}"
+  when: not swapfile_check.stat.exists