-
-
Save devster31/74e48cc1c8e73c637bc7 to your computer and use it in GitHub Desktop.
Ansible role for creating a Swap file
This file contains 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
- 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 | |
sudo: yes | |
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 | |
sudo: yes | |
file: | |
path: "{{ swap_file }}" | |
mode: 0600 | |
- name: format swap file | |
sudo: yes | |
command: mkswap {{ swap_file }} | |
when: not swap_file_check.stat.exists | |
- name: add to fstab | |
sudo: yes | |
lineinfile: | |
dest: /etc/fstab | |
regexp: "{{ swap_file }}" | |
line: "{{ swap_file }} none swap sw 0 0" | |
- name: turn on swap | |
sudo: yes | |
command: swapon -a | |
- name: set swapiness | |
sudo: yes | |
sysctl: | |
name: vm.swappiness | |
value: "1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my tests, the value for
vm.swappiness
will be persisted on/etc/sysctl.conf
. I rebooted my instance and the value still in `/etc/sysctl.conf,