Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add swap memory with ansible

I'm working on a project where having swap memory on my servers is a needed to avoid some python long running processes to go out of memory and realized for the first time that my ubuntu vagrant boxes and AWS ubuntu instances didn't already have one set up.

In https://github.com/ansible/ansible/issues/5241 a possible built in solution was discussed but never implemented, so I'm guessing this should be a pretty common task to automatize.

How would you set up a file based swap memory with ansible in an idempotent way? What modules or variables does ansible provide help with this setup (like ansible_swaptotal_mb variable) ?

like image 785
gonz Avatar asked Jul 15 '14 18:07

gonz


1 Answers

This is my current solution:

- name: Create swap file   command: dd if=/dev/zero of={{ swap_file_path }} bs=1024 count={{ swap_file_size_mb }}k            creates="{{ swap_file_path }}"   tags:     - swap.file.create   - name: Change swap file permissions   file: path="{{ swap_file_path }}"         owner=root         group=root         mode=0600   tags:     - swap.file.permissions   - name: "Check swap file type"   command: file {{ swap_file_path }}   register: swapfile   tags:     - swap.file.mkswap   - name: Make swap file   command: "sudo mkswap {{ swap_file_path }}"   when: swapfile.stdout.find('swap file') == -1   tags:     - swap.file.mkswap   - name: Write swap entry in fstab   mount: name=none          src={{ swap_file_path }}          fstype=swap          opts=sw          passno=0          dump=0          state=present   tags:     - swap.fstab   - name: Mount swap   command: "swapon {{ swap_file_path }}"   when: ansible_swaptotal_mb < 1   tags:     - swap.file.swapon 
like image 153
gonz Avatar answered Sep 18 '22 16:09

gonz