--- - when: not (swap_enabled | bool) or swap_size | int > (ansible_swaptotal_mb | int) + 1 block: - name: Disable swap ansible.builtin.shell: "test -f {{ swap_path }} && swapoff {{ swap_path }} || true" changed_when: False - name: Unmount swap file ansible.posix.mount: src: "{{ swap_path }}" path: none fstype: swap state: absent - name: Remove swap file ansible.builtin.file: path: "{{ swap_path }}" state: absent - when: - swap_enabled | bool - swap_size | int > (ansible_swaptotal_mb | int) + 1 block: - name: Create swap file ansible.builtin.command: "fallocate -l {{ swap_size }}M {{ swap_path }}" - name: Format swap file ansible.builtin.command: "mkswap {{ swap_path }}" - when: swap_enabled | bool block: - name: Fix file permissions ansible.builtin.file: path: "{{ swap_path }}" owner: root group: root mode: "0600" - name: Mount swap file ansible.posix.mount: src: "{{ swap_path }}" path: none fstype: swap state: present - name: Enable swap ansible.builtin.command: "swapon -a" changed_when: False