If you are not familiar with ansible, you might want to take a look at our previous post. So here’s a problem. If you have, say, Amazon EC2 instance or Rackspace Cloud server, you might want to add swap memory. You can do do it by running the following commands:
sudo dd if=/dev/zero of=/mnt/4GB.swap bs=4096 count=1048576 sudo chmod 600 /mnt/4GBB.swap sudo mkswap /mnt/4GB.swap swapon /mnt/4GB.swap
and add this line to /etc/fstab:
/mnt/512MiB.swap none swap sw 0 0
If you have one server it’s acceptable, but if you have a lot of them, it would be much easier to use ansible. We will use the how-to by Rackspace on how to create swap file. (We assume you have a user account ‘devops’ which is able to log in using SSH key as well as to run commands as root via sudo. If need need check our blog entry about ansible mentioned earlier).
- hosts: web001 tasks: - name: create the file to be used for swap command: fallocate -l 4G /mnt/4GB.swap - name: format the file for swap command: mkswap /mnt/4GB.swap - name: change swap file permissions file: path=/mnt/4GB.swap owner=root group=root mode=0600 - name: add the file to the system as a swap file command: swapon /mnt/4GB.swap - name: write swap entry in fstab mount: name=none src=/mnt/4GB.swap fstype=swap opts=sw passno=0 dump=0 state=present
If you get this error message:
fallocate: /mnt/4GB.swap: fallocate failed: Operation not supported
use dd instead:
sudo dd if=/dev/zero of=./4GB.swap bs=4096 count=1048576
Run create a swap file run:
ansible-playbook add_swap.yml -u devops -s -v --check
Remove –check parameter to apply changes. And that’s it.