├── .gitignore ├── LICENSE ├── README.md ├── VERSION ├── group_vars └── swap.yml ├── roles └── swap │ ├── handlers │ └── main.yml │ └── tasks │ └── main.yml └── site.yml /.gitignore: -------------------------------------------------------------------------------- 1 | site.retry 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 tribou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ansible-swap 2 | 3 | Configure a swap file on a new Ubuntu or CentOS host. 4 | 5 | #### Quick Start 6 | 7 | ```sh 8 | ansible-playbook site.yml -i 123.123.123.123, --extra-vars "target=123.123.123.123" 9 | ``` 10 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.1.1 2 | -------------------------------------------------------------------------------- /group_vars/swap.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # All ansible-swap variables 3 | # Override any in your local ansible group_vars/swap.yml 4 | 5 | # Location of swapfile 6 | swap_path: "/swapfile" 7 | 8 | # Hosts with low RAM may need to use a small bs size 9 | dd_bs_size_mb: 256 10 | 11 | # Total size for swapfile 12 | # Count of how many passes dd should make at bs size 13 | swap_count: 16 14 | 15 | # 0 to 100, how often swap is utilized 16 | # 60 is good for desktops, 10 is good for VPS 17 | swappiness: 10 18 | 19 | # How often inode info is removed from cache 20 | # Usually a frequent and costly lookup if not cached 21 | vfs_cache_pressure: 50 22 | -------------------------------------------------------------------------------- /roles/swap/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: sysctl 4 | command: sysctl -p 5 | -------------------------------------------------------------------------------- /roles/swap/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Gathering facts 4 | setup: 5 | 6 | - name: Create swapfile 7 | command: "dd if=/dev/zero of={{ swap_path }} bs={{ dd_bs_size_mb }}M count={{ swap_count }} creates={{ swap_path }}" 8 | register: write_swapfile 9 | 10 | - name: Set swapfile permissions 11 | file: path="{{ swap_path }}" mode=600 12 | 13 | - name: Build swapfile 14 | command: "mkswap {{ swap_path }}" 15 | register: create_swapfile 16 | when: write_swapfile.changed 17 | 18 | - name: Enable swapfile 19 | command: "swapon {{ swap_path }}" 20 | when: create_swapfile.changed 21 | 22 | - name: Add swapfile to /etc/fstab 23 | lineinfile: dest=/etc/fstab line="{{ swap_path }} none swap sw 0 0" state=present 24 | 25 | - name: Configure swappiness 26 | lineinfile: dest=/etc/sysctl.conf line="vm.swappiness = {{ swappiness }}" regexp="^vm.swappiness[\s]?=" state=present 27 | notify: sysctl 28 | when: swappiness != false 29 | 30 | - name: Configure vm.vfs_cache_pressure 31 | lineinfile: dest=/etc/sysctl.conf line="vm.vfs_cache_pressure = {{ vfs_cache_pressure }}" regexp="^vm.vfs_cache_pressure[\s]?=" state=present 32 | notify: sysctl 33 | when: vfs_cache_pressure != false 34 | 35 | - name: Restart server 36 | shell: sleep 2 && shutdown -r now "Ansible updates triggered" 37 | async: 1 38 | poll: 0 39 | ignore_errors: true 40 | 41 | - name: Wait for server to restart successfully 42 | local_action: wait_for 43 | host="{{ target }}" 44 | port=22 45 | delay=1 46 | timeout=30 47 | 48 | - name: Get last two reboots 49 | command: /usr/bin/last reboot | head -2 50 | register: last 51 | 52 | - name: Show last two reboots 53 | debug: var=last.stdout_lines 54 | -------------------------------------------------------------------------------- /site.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: '{{ target }}' 3 | 4 | remote_user: root 5 | 6 | gather_facts: no 7 | 8 | pre_tasks: 9 | - name: 'Install python2 Debian/Ubuntu' 10 | raw: apt-get -y install python-simplejson 11 | ignore_errors: true 12 | 13 | roles: 14 | - swap 15 | 16 | vars: 17 | # Location of swapfile 18 | swap_path: "/swapfile" 19 | 20 | # Hosts with low RAM may need to use a small bs size 21 | dd_bs_size_mb: 256 22 | 23 | # Total size for swapfile 24 | # Count of how many passes dd should make at bs size 25 | swap_count: 16 26 | 27 | # 0 to 100, how often swap is utilized 28 | # 60 is good for desktops, 10 is good for VPS 29 | swappiness: 10 30 | 31 | # How often inode info is removed from cache 32 | # Usually a frequent and costly lookup if not cached 33 | vfs_cache_pressure: 50 34 | --------------------------------------------------------------------------------