--- - name: Ensure swap exists command: fallocate -l {{ swap_size | default('2G') }} /swapfile args: creates: /swapfile - name: Set swap permissions file: path: /swapfile mode: '0600' - name: Make swap command: mkswap /swapfile args: creates: /swapfile.swap - name: Mark swapfile as initialized file: path: /swapfile.swap state: touch - name: Enable swap command: swapon /swapfile register: swap_on failed_when: false - name: Add swap to fstab lineinfile: path: /etc/fstab line: "/swapfile none swap sw 0 0" state: present