├── .gitignore ├── tests └── test.yml ├── CHANGELOG.md ├── meta └── main.yml ├── .travis.yml ├── defaults └── main.yml ├── LICENSE ├── tasks └── main.yml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | */**.DS_Store 3 | ._* 4 | .*.sw* 5 | *~ 6 | .idea/ 7 | .vscode/ 8 | *.retry 9 | -------------------------------------------------------------------------------- /tests/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - hosts: "all" 4 | 5 | # Travis doesn't allow swap files, so there are no role specific tests. 6 | 7 | roles: 8 | - "role_under_test" 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Next release 4 | 5 | *Released: TBD* 6 | 7 | ## v0.2.0 8 | 9 | *Released: November 15th 2019* 10 | 11 | - Compatibility with Ansible 2.9.x 12 | 13 | ## v0.1.1 14 | 15 | *Released: January 25th 2018* 16 | 17 | - Add proper tests and support for Ubuntu 16, Debian Stretch and Debian Jessie 18 | - Update format and style consistencies 19 | 20 | ## v0.1.0 21 | 22 | *Released: October 30th 2016* 23 | 24 | - Initial release 25 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | galaxy_info: 4 | author: "Nick Janetakis" 5 | description: "Create and configure a swap file." 6 | company: 7 | license: "license (MIT)" 8 | min_ansible_version: 2.4 9 | 10 | platforms: 11 | - name: "Ubuntu" 12 | versions: 13 | - "xenial" 14 | - name: "Debian" 15 | versions: 16 | - "jessie" 17 | - "stretch" 18 | 19 | categories: 20 | - "system" 21 | 22 | dependencies: [] 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | services: "docker" 4 | 5 | env: 6 | - distro: "ubuntu1604" 7 | - distro: "debian8" 8 | - distro: "debian9" 9 | 10 | script: 11 | # Download test shim. 12 | - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/nickjj/d12353b5b601e33cd62fda111359957a/raw 13 | - chmod +x ${PWD}/tests/test.sh 14 | 15 | # Run tests. Leave the container around after the test because cleaning it up 16 | # will make Docker angry. 17 | - cleanup=false ${PWD}/tests/test.sh 18 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | swapfile_size: "{{ ((ansible_memtotal_mb | int * 2) 4 | if (ansible_memtotal_mb | int <= 2048) 5 | else '512') }}" 6 | 7 | swapfile_fallocate: True 8 | swapfile_path: "/swapfile-{{ swapfile_size }}" 9 | 10 | swapfile_swappiness: 60 11 | swapfile_vfs_cache_pressure: 100 12 | 13 | swapfile_sysctl: 14 | "vm.swappiness": "{{ swapfile_swappiness }}" 15 | "vm.vfs_cache_pressure": "{{ swapfile_vfs_cache_pressure }}" 16 | 17 | swapfile_delete: False 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Nick Janetakis nick.janetakis@gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Configure swap file swappiness and cache pressure 4 | sysctl: 5 | name: "{{ item.key }}" 6 | value: "{{ item.value }}" 7 | state: "present" 8 | reload: True 9 | with_dict: "{{ swapfile_sysctl }}" 10 | 11 | - name: Disable swap file 12 | shell: test -f {{ swapfile_path }} && swapoff {{ swapfile_path }} || true 13 | changed_when: False 14 | when: swapfile_delete 15 | 16 | - name: Create swap file 17 | command: | 18 | {% if swapfile_fallocate %} 19 | fallocate -l {{ ((swapfile_size) | int * 1024 * 1024) }} {{ swapfile_path }} 20 | {% else %} 21 | dd if=/dev/zero of={{ swapfile_path }} bs=1M count={{ swapfile_size }} 22 | {% endif %} 23 | args: 24 | creates: "{{ swapfile_path }}" 25 | register: swapfile_register_create 26 | when: not swapfile_delete 27 | 28 | - name: Set swap file permissions 29 | file: 30 | path: "{{ swapfile_path }}" 31 | state: "file" 32 | owner: "root" 33 | group: "root" 34 | mode: "0600" 35 | when: (not swapfile_delete and not ansible_check_mode) 36 | 37 | - name: Initialize swap file 38 | command: mkswap {{ swapfile_path }} 39 | when: (swapfile_register_create is changed and not swapfile_delete) 40 | 41 | - name: Enable swap file 42 | command: swapon {{ swapfile_path }} 43 | when: (swapfile_register_create is changed and not swapfile_delete) 44 | 45 | - name: Manage swap file in /etc/fstab 46 | mount: 47 | src: "{{ swapfile_path }}" 48 | name: "none" 49 | fstype: "swap" 50 | opts: "sw,nofail" 51 | dump: "0" 52 | passno: "0" 53 | state: "{{ 'absent' if swapfile_delete else 'present' }}" 54 | 55 | - name: Remove swap file 56 | file: 57 | path: "{{ swapfile_path }}" 58 | state: "absent" 59 | when: swapfile_delete 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What is ansible-swapfile? [![Build Status](https://secure.travis-ci.org/nickjj/ansible-swapfile.png)](http://travis-ci.org/nickjj/ansible-swapfile) 2 | 3 | It is an [Ansible](http://www.ansible.com/home) role to: 4 | 5 | - Create a swap file 6 | - Delete a swap file 7 | 8 | ## Why would you want to use this role? 9 | 10 | Having a swap file is useful. It will help protect you from the OOM killer going 11 | berserk and shutting down services, but it also gives the Linux kernel a place 12 | to dump less frequently accessed memory pages, so it can provide more memory to 13 | highly accessed memory pages. Basically it can improve overall system performance. 14 | 15 | ## Supported platforms 16 | 17 | - Ubuntu 16.04 LTS (Xenial) 18 | - Debian 8 (Jessie) 19 | - Debian 9 (Stretch) 20 | 21 | ## Role variables 22 | 23 | ``` 24 | # Default to double your system's RAM capacity if you have 2GB or less. 25 | swapfile_size: "{{ ((ansible_memtotal_mb | int * 2) 26 | if (ansible_memtotal_mb | int <= 2048) 27 | else '512') }}" 28 | 29 | # Using fallocate is quite a bit faster than dd, so we'll default to using that. 30 | # If your file system does not support fallocate, set this to False. 31 | # 32 | # If you're using one of the supported platforms you have it, so don't worry. 33 | swapfile_fallocate: True 34 | 35 | # Where should the swap file be created? 36 | swapfile_path: "/swapfile-{{ swapfile_size }}" 37 | 38 | # Settings to configure your swap file. Google them for more details. 39 | swapfile_swappiness: 60 40 | swapfile_vfs_cache_pressure: 100 41 | 42 | # Default settings for configuring your swap file, you should not edit this 43 | # directly. Instead use the variables above. This allows you to customize just 44 | # 1 of them without having to bring along the entire dictionary. 45 | swapfile_sysctl: 46 | "vm.swappiness": "{{ swapfile_swappiness }}" 47 | "vm.vfs_cache_pressure": "{{ swapfile_vfs_cache_pressure }}" 48 | 49 | # When set to True, the swap file will be disabled and deleted. 50 | swapfile_delete: False 51 | ``` 52 | 53 | ## Example usage 54 | 55 | For the sake of this example let's assume you have a group called **app** and 56 | you have a typical `site.yml` file. 57 | 58 | To use this role edit your `site.yml` file to look something like this: 59 | 60 | ``` 61 | --- 62 | 63 | - name: "Configure app server(s)" 64 | hosts: "app" 65 | become: True 66 | 67 | roles: 68 | - { role: "nickjj.swapfile", tags: "swapfile" } 69 | ``` 70 | 71 | Let's say you want to set a 4GB swap file and customize the swappiness, you can 72 | do this by opening or creating `group_vars/app.yml` which is located relative 73 | to your `inventory` directory and then making it look like this: 74 | 75 | ``` 76 | --- 77 | 78 | swapfile_size: 4096 79 | 80 | swapfile_swappiness: 20 81 | 82 | ``` 83 | 84 | Now you would run `ansible-playbook -i inventory/hosts site.yml -t swapfile`. 85 | 86 | ## Installation 87 | 88 | `$ ansible-galaxy install nickjj.swapfile` 89 | 90 | ## Ansible Galaxy 91 | 92 | You can find it on the official 93 | [Ansible Galaxy](https://galaxy.ansible.com/nickjj/swapfile/) if you want to 94 | rate it. 95 | 96 | ## License 97 | 98 | MIT 99 | --------------------------------------------------------------------------------