Skip to content

Instantly share code, notes, and snippets.

@Kalki5
Created June 26, 2019 12:24
Show Gist options
  • Save Kalki5/31b2f89d8c02b594b489564698e308c6 to your computer and use it in GitHub Desktop.
Save Kalki5/31b2f89d8c02b594b489564698e308c6 to your computer and use it in GitHub Desktop.
Add Swap to multiple machines in bulk
sudo su
apt-get update
apt-get install -y ansible
echo ANSIBLE_HOST_KEY_CHECKING=False >> /etc/environment
echo "
[machines]
IP_ADDRESS_1
IP_ADDRESS_2
IP_ADDRESS_3
IP_ADDRESS_4
IP_ADDRESS_5
IP_ADDRESS_6
IP_ADDRESS_7
IP_ADDRESS_8
IP_ADDRESS_9
IP_ADDRESS_10
" >> /etc/ansible/hosts
ansible machines -u admin --private-key=admin.pem -m ping
ansible machines -u admin --private-key=admin.pem -a 'free -m'
ansible machines -u admin --private-key=admin.pem -s -a 'ls -laH /swapfile'
ansible machines -u admin --private-key=admin.pem -s -a 'rm -rf /swapfile'
ansible machines -u admin --private-key=admin.pem -s -a 'ls -laH /swapfile'
ansible machines -u admin --private-key=admin.pem -s -a 'dd if=/dev/zero of=/swapfile count=4096 bs=1MiB'
ansible machines -u admin --private-key=admin.pem -s -a 'chmod 600 /swapfile'
ansible machines -u admin --private-key=admin.pem -s -a 'mkswap /swapfile'
ansible machines -u admin --private-key=admin.pem -s -a 'swapon /swapfile'
ansible machines -u admin --private-key=admin.pem -s -m lineinfile -a "path=/etc/fstab line='/swapfile none swap sw 0 0' insertafter=EOF"
ansible machines -u admin --private-key=admin.pem -s -m shell -a 'free -m | tail -n 1' -o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment