Skip to content

Instantly share code, notes, and snippets.

@sajjadG
Forked from devster31/swap.yml
Last active August 8, 2022 06:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajjadG/3a791ca6dfe8c2890de62bc702f78f60 to your computer and use it in GitHub Desktop.
Save sajjadG/3a791ca6dfe8c2890de62bc702f78f60 to your computer and use it in GitHub Desktop.
Ansible role for creating a Swap file
swap_space: 2G
- name: set swap_file variable
set_fact:
swap_file: /{{ swap_space }}.swap
- name: check if swap file exists
stat:
path: "{{ swap_file }}"
register: swap_file_check
- name: create swap file
become: True
command: fallocate -l {{ swap_space }} {{ swap_file }}
when: not swap_file_check.stat.exists
- name: Create swap space
command: dd if=/dev/zero of=/extraswap bs=1M count=512
when: not swap_file_check.stat.exists
- name: set permissions on swap file
become: True
file:
path: "{{ swap_file }}"
mode: 0600
- name: format swap file
become: True
command: mkswap {{ swap_file }}
when: not swap_file_check.stat.exists
- name: add to fstab
become: True
lineinfile:
dest: /etc/fstab
regexp: "{{ swap_file }}"
line: "{{ swap_file }} none swap sw 0 0"
- name: turn on swap
become: True
command: swapon -a
when: not swap_file_check.stat.exists
- name: set swapiness
become: True
sysctl:
name: vm.swappiness
value: "1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment