--- - when: swap_size | int > (ansible_swaptotal_mb | int) + 1 or not (swap_enabled | bool) block: - name: Disable active swap ansible.builtin.shell: "test -f {{ swap_path }} && swapoff {{ swap_path }} || true" - name: Remove swap file ansible.builtin.file: path: "{{ swap_path }}" state: absent - when: 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: swap fstype: swap state: present - name: Enable swap file ansible.builtin.command: "swapon -a" changed_when: False