infrastructure-as-code/roles/swap/tasks/main.yml

37 lines
955 B
YAML

---
- 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"