|
| 1 | +--- |
| 2 | +# From https://stackoverflow.com/questions/24765930/add-swap-memory-with-ansible |
| 3 | +- name: Configure swap |
| 4 | + when: swap_configure | bool | default(false) |
| 5 | + block: |
| 6 | + |
| 7 | + - name: Check if swap file exists |
| 8 | + ansible.builtin.stat: |
| 9 | + path: "{{ swap_file_path }}" |
| 10 | + get_checksum: false |
| 11 | + get_md5: false |
| 12 | + get_mime: false |
| 13 | + get_attributes: false |
| 14 | + register: swap_file_check |
| 15 | + changed_when: false |
| 16 | + |
| 17 | + - name: Check if swap is on |
| 18 | + ansible.builtin.shell: |
| 19 | + set -o pipefail; |
| 20 | + swapon --show | grep {{ swap_file_path }} |
| 21 | + args: |
| 22 | + executable: /bin/bash |
| 23 | + register: swap_enabled |
| 24 | + changed_when: false |
| 25 | + failed_when: false |
| 26 | + |
| 27 | + - name: Disable swap |
| 28 | + ansible.builtin.command: swapoff {{ swap_file_path }} |
| 29 | + register: swap_disabled |
| 30 | + changed_when: true |
| 31 | + when: > |
| 32 | + swap_file_check.stat.exists |
| 33 | + and swap_enabled.rc == 0 |
| 34 | + and (not swap_enable |
| 35 | + or (swap_enable and swap_file_check.stat.size != (swap_file_size_mb * 1024 * 1024))) |
| 36 | +
|
| 37 | + - name: Delete the swap file |
| 38 | + ansible.builtin.file: |
| 39 | + path: "{{ swap_file_path }}" |
| 40 | + state: absent |
| 41 | + when: not swap_enable |
| 42 | + |
| 43 | + - name: Configure swap |
| 44 | + when: swap_enable|bool |
| 45 | + block: |
| 46 | + |
| 47 | + - name: Create or change the size of swap file |
| 48 | + ansible.builtin.command: dd if=/dev/zero of={{ swap_file_path }} count={{ swap_file_size_mb }} bs=1MiB |
| 49 | + register: swap_file_created |
| 50 | + changed_when: true |
| 51 | + when: > |
| 52 | + not swap_file_check.stat.exists |
| 53 | + or swap_file_check.stat.size != (swap_file_size_mb * 1024 * 1024) |
| 54 | +
|
| 55 | + - name: Change swap file permissions |
| 56 | + ansible.builtin.file: |
| 57 | + path: "{{ swap_file_path }}" |
| 58 | + owner: root |
| 59 | + group: root |
| 60 | + mode: '0600' |
| 61 | + |
| 62 | + - name: Check if swap is formatted |
| 63 | + ansible.builtin.shell: |
| 64 | + set -o pipefail; |
| 65 | + file {{ swap_file_path }} | grep 'swap file' |
| 66 | + args: |
| 67 | + executable: /bin/bash |
| 68 | + register: swap_file_is_formatted |
| 69 | + changed_when: false |
| 70 | + failed_when: false |
| 71 | + |
| 72 | + - name: Debugger1 |
| 73 | + ansible.builtin.debug: |
| 74 | + var: swap_file_is_formatted |
| 75 | + |
| 76 | + - name: Debugger2 |
| 77 | + ansible.builtin.debug: |
| 78 | + var: swap_file_created |
| 79 | + |
| 80 | + - name: Format swap file if it's not formatted |
| 81 | + ansible.builtin.command: mkswap {{ swap_file_path }} |
| 82 | + when: swap_file_is_formatted.rc > 0 or swap_file_created.changed |
| 83 | + changed_when: true |
| 84 | + |
| 85 | + - name: Add swap entry to fstab |
| 86 | + ansible.posix.mount: |
| 87 | + path: none |
| 88 | + src: "{{ swap_file_path }}" |
| 89 | + fstype: swap |
| 90 | + opts: defaults |
| 91 | + passno: 0 |
| 92 | + dump: 0 |
| 93 | + state: present |
| 94 | + |
| 95 | + - name: Turn on swap |
| 96 | + ansible.builtin.command: swapon -a |
| 97 | + when: swap_enable |
| 98 | + changed_when: false |
| 99 | + # original logic from article. However, let's start it every time. |
| 100 | + # when: swap_enabled.rc != 0 and swap_disabled.changed |
| 101 | + |
| 102 | + - name: Configure swappiness |
| 103 | + ansible.posix.sysctl: |
| 104 | + name: vm.swappiness |
| 105 | + value: "{{ swappiness }}" |
| 106 | + state: present |
0 commit comments