Skip to content

Instantly share code, notes, and snippets.

@jyunderwood
Created September 9, 2014 13:23
Show Gist options
  • Save jyunderwood/717a09c03f65eaaa5354 to your computer and use it in GitHub Desktop.
Save jyunderwood/717a09c03f65eaaa5354 to your computer and use it in GitHub Desktop.
---
- hosts: all
sudo: yes
tasks:
- name: "test for swap partition"
shell: 'sudo swapon -s | grep -E "^/"'
register: swapfile
ignore_errors: yes
- name: "create swapfile"
when: swapfile|failed
shell: "sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k"
- name: "set swapfile permissions"
when: swapfile|failed
file: path=/swapfile
owner=root
group=root
mode=0600
- name: "prepare swapfile"
when: swapfile|failed
shell: "sudo mkswap /swapfile"
- name: "enable swap"
when: swapfile|failed
shell: "sudo swapon /swapfile"
- name: "add swapfile"
when: swapfile|failed
lineinfile: dest=/etc/fstab
regexp="^/swapfile"
state=present
line="/swapfile none swap sw 0 0"
- name: "set swappiness (temporarily)"
when: swapfile|failed
shell: "echo 1 | sudo tee /proc/sys/vm/swappiness"
- name: "set swappiness (permanent)"
when: swapfile|failed
lineinfile: dest=/etc/sysctl.conf
regexp="^vm.swappiness"
state=present
line="vm.swappiness = 1"
@jyunderwood
Copy link
Author

This is an Ansible playbook to add swap to a server based on a DigitalOcean tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment