Created
August 27, 2021 17:29
-
-
Save sharanpeetani/4b3042523b70771ce2bc0270ea31be9b to your computer and use it in GitHub Desktop.
Swapfile creation on Debian servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## PLaybook is used to create or delete swapfile | |
## Author: Maithili Peetani | |
--- | |
- hosts: all | |
gather_facts: yes | |
become: yes | |
vars_files: | |
- swapfile_vars.yml | |
tasks: | |
- name: Install fallocate package | |
package: | |
name: util-linux | |
state: present | |
- name: Configure swap file swappiness | |
sysctl: | |
name: "{{ item.key }}" | |
value: "{{ item.value }}" | |
state: present | |
reload: yes | |
with_dict: "{{ swapfile_sysctl }}" | |
- name: Disable swap | |
block: | |
- name: Check if swap file exists | |
stat: | |
path: "{{ swapfile_path }}" | |
register: swap_file_check | |
changed_when: false | |
- name: Check if swap is on | |
shell: swapon --show | grep {{ swapfile_path }} | |
register: swap_enabled | |
changed_when: false | |
failed_when: false | |
- name: Disable swap | |
command: swapoff {{ swapfile_path }} | |
register: swap_disabled | |
when: > | |
( swap_file_check.stat.exists ) and | |
( swap_enabled.rc == 0 ) | |
- name: Delete the swap file | |
file: | |
path: "{{ swapfile_path }}" | |
state: absent | |
when: swap_disabled is changed | |
when: > | |
( swap_pre_check | bool | default(false)) and | |
( swapfile_disable ) | |
- name: Configure swap | |
block: | |
- name: "Check if fallocate is installed" | |
package_facts: | |
manager: "auto" | |
- name: Create or change the size of swap file | |
command: | | |
{% if swapfile_fallocate %} | |
fallocate -l {{ ((swapfile_size) | int * 1024 * 1024) }} {{ swapfile_path }} | |
{% else %} | |
dd if=/dev/zero of={{ swapfile_path }} bs=1M count={{ swapfile_size }} | |
{% endif %} | |
args: | |
creates: "{{ swapfile_path }}" | |
register: swapfile_register_create | |
when: > | |
( "'util-linux' in ansible_facts.packages" ) and | |
( not swap_file_check.stat.exists ) and | |
( not swapfile_disable ) | |
- name: Set swap file permissions | |
file: | |
path: "{{ swapfile_path }}" | |
state: "file" | |
owner: "root" | |
group: "root" | |
mode: "0600" | |
when: (not swapfile_disable and not ansible_check_mode) | |
- name: Check if swap is formatted | |
shell: file {{ swapfile_path }} | grep 'swap file' | |
register: swap_file_is_formatted | |
changed_when: false | |
failed_when: false | |
- name: Initialize swap file | |
command: mkswap {{ swapfile_path }} | |
register: swap_mkswap | |
when: > | |
( swapfile_register_create is changed and not swapfile_disable ) and | |
( swap_file_is_formatted.rc > 0 or swapfile_register_create.changed ) | |
- name: Enable swap file | |
command: swapon {{ swapfile_path }} | |
when: > | |
( swapfile_register_create.changed ) and | |
( not swapfile_disable ) | |
- name: Manage swap file in /etc/fstab | |
mount: | |
src: "{{ swapfile_path }}" | |
name: "none" | |
fstype: "swap" | |
opts: "sw,nofail" | |
dump: "0" | |
passno: "0" | |
state: "{{ 'absent' if swapfile_disable else 'present' }}" | |
- name: Turn on swap | |
shell: swapon -a | |
when: swap_mkswap.changed | |
when: swapfile_create | bool | default(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
swap_pre_check: true | |
swapfile_create: true | |
# Default to double your system's RAM capacity | |
swapfile_size: "{{ ((ansible_memtotal_mb | int * 2) | |
if (ansible_memtotal_mb | int <= 2048) | |
else '2048') }}" | |
swapfile_fallocate: true | |
swapfile_path: "/mnt/swapfile" | |
swapfile_swappiness: 60 | |
swapfile_vfs_cache_pressure: 100 | |
swapfile_sysctl: | |
"vm.swappiness": "{{ swapfile_swappiness }}" | |
"vm.vfs_cache_pressure": "{{ swapfile_vfs_cache_pressure }}" | |
swapfile_disable: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment