├── LICENSE ├── README.md ├── defaults └── main.yml ├── handlers └── main.yml ├── meta └── main.yml └── tasks └── main.yml /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Kamal Nasser 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 | ### This repo is no longer maintained. You can use https://github.com/Oefenweb/ansible-swapfile or any of the other forks—people have done great work there 🙂 2 | 3 | --- 4 | 5 | ansible-swapfile 6 | ================ 7 | 8 | This role configures a swapfile (/swapfile) with the (default) size of 512MB. 9 | 10 | ## Dependencies 11 | 12 | None. 13 | 14 | ## Variables 15 | 16 | * `swapfile_use_dd` [default: `False`]: if set to False, `fallocate` is used to create the swapfile, otherwise, `dd` is used. You may need to set this to True if your filesystem does not support `fallocate` -- see Issue #3. 17 | 18 | * `swapfile_size` [default: `512MB`]: the size of the swapfile to create in the format that `fallocate` expects: 19 | 20 | The length and offset arguments may be followed by binary (2^N) suffixes KiB, MiB, GiB, TiB, PiB and EiB (the "iB" is optional, e.g. "K" has the same meaning as "KiB") or decimal (10^N) suffixes KB, MB, GB, PB and EB. 21 | 22 | If `swapfile_use_dd` is set to True, `swapfile_size` must be set to the amount of megabytes to write, e.g. `512`. 23 | 24 | * `swapfile_location` [default: `/swapfile`]: the location of where the swapfile will be created 25 | 26 | ### Optional 27 | 28 | The following variables are set to `False` by default and will not have any effect on your hosts. Setting them to any value other than `False` will update your hosts' sysctl.conf file. 29 | 30 | * `swapfile_swappiness` [default: `False`]: the swappiness percentage (vm.swappiness) -- the lower it is, the less your system swaps memory pages 31 | 32 | * `swapfile_vfs_cache_pressure` [default: `False`]: "this percentage value controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects." 33 | 34 | ## Usage 35 | 36 | ```yaml 37 | - hosts: all 38 | roles: 39 | - kamaln7.swapfile 40 | ``` 41 | 42 | or: 43 | 44 | ```yaml 45 | - hosts: all 46 | roles: 47 | - { role: kamaln7.swapfile, swapfile_size: 1GB, swapfile_swappiness: 10, swapfile_location: /mnt/swapfile } 48 | ``` 49 | 50 | You can also set the variables described above in `group_vars` or `host_vars` (see `defaults/main.yml`). 51 | 52 | ## License 53 | 54 | The MIT License (MIT) 55 | 56 | Copyright (c) 2014 Kamal Nasser 57 | 58 | Permission is hereby granted, free of charge, to any person obtaining a copy 59 | of this software and associated documentation files (the "Software"), to deal 60 | in the Software without restriction, including without limitation the rights 61 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 62 | copies of the Software, and to permit persons to whom the Software is 63 | furnished to do so, subject to the following conditions: 64 | 65 | The above copyright notice and this permission notice shall be included in all 66 | copies or substantial portions of the Software. 67 | 68 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 69 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 70 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 71 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 72 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 73 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 74 | SOFTWARE. 75 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | swapfile_location: /swapfile 3 | swapfile_size: 512MB 4 | swapfile_swappiness: False 5 | swapfile_vfs_cache_pressure: False 6 | swapfile_use_dd: False 7 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Reload sysctl 3 | command: sysctl -p 4 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: "Kamal Nasser" 4 | description: swapfile 5 | license: MIT 6 | min_ansible_version: 1.4 7 | version: 0.4 8 | categories: 9 | - system 10 | dependencies: [] 11 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Write swapfile 3 | command: | 4 | {% if swapfile_use_dd %} 5 | dd if=/dev/zero of={{ swapfile_location }} bs=1M count={{ swapfile_size }} creates={{ swapfile_location }} 6 | {% else %} 7 | fallocate -l {{ swapfile_size }} {{ swapfile_location }} creates={{ swapfile_location }} 8 | {% endif %} 9 | register: write_swapfile 10 | when: swapfile_size != false 11 | 12 | - name: Set swapfile permissions 13 | file: path={{ swapfile_location }} mode=600 14 | when: swapfile_size != false 15 | 16 | - name: Create swapfile 17 | command: mkswap {{ swapfile_location }} 18 | environment: 19 | PATH: "{{ (ansible_env|default({})).PATH|default('') }}:/usr/local/sbin:/usr/sbin:/sbin" 20 | register: create_swapfile 21 | when: swapfile_size != false and write_swapfile.changed 22 | 23 | - name: Enable swapfile 24 | command: swapon {{ swapfile_location }} 25 | environment: 26 | PATH: "{{ (ansible_env|default({})).PATH|default('') }}:/usr/local/sbin:/usr/sbin:/sbin" 27 | when: swapfile_size != false and create_swapfile.changed 28 | 29 | - name: Add swapfile to /etc/fstab 30 | lineinfile: dest=/etc/fstab line="{{ swapfile_location }} none swap sw 0 0" state=present 31 | when: swapfile_size != false 32 | 33 | - name: Configure vm.swappiness 34 | lineinfile: dest=/etc/sysctl.conf line="vm.swappiness = {{ swapfile_swappiness }}" regexp="^vm.swappiness[\s]?=" state=present 35 | notify: Reload sysctl 36 | when: swapfile_swappiness != false 37 | 38 | - name: Configure vm.vfs_cache_pressure 39 | lineinfile: dest=/etc/sysctl.conf line="vm.vfs_cache_pressure = {{ swapfile_vfs_cache_pressure }}" regexp="^vm.vfs_cache_pressure[\s]?=" state=present 40 | notify: Reload sysctl 41 | when: swapfile_vfs_cache_pressure != false 42 | --------------------------------------------------------------------------------