diff --git a/swap.yml b/swap.yml new file mode 100644 index 0000000..e2962cc --- /dev/null +++ b/swap.yml @@ -0,0 +1,32 @@ +--- +- name: Create swap file if not present + hosts: all + become: yes + vars: + swap_file: /swapfile + swap_size_mb: 2048 + tasks: + - name: Allocate swap file + command: fallocate -l {{ swap_size_mb }}M {{ swap_file }} + args: + creates: "{{ swap_file }}" + + - name: Secure swap file + file: + path: "{{ swap_file }}" + owner: root + mode: '0600' + + - name: Make swap + command: mkswap {{ swap_file }} + + - name: Enable swap + command: swapon {{ swap_file }} + + - name: Ensure swap on reboot + mount: + name: none + src: "{{ swap_file }}" + fstype: swap + opts: sw + state: present \ No newline at end of file