--- - name: Create swap file if not present hosts: all become: yes vars: swap_file: /swapfile swap_size_mb: 2048 tasks: - name: Allocate swap file command: fallocate -l {{ swap_size_mb }}M {{ swap_file }} args: creates: "{{ swap_file }}" - name: Secure swap file file: path: "{{ swap_file }}" owner: root mode: '0600' - name: Make swap command: mkswap {{ swap_file }} - name: Enable swap command: swapon {{ swap_file }} - name: Ensure swap on reboot mount: name: none src: "{{ swap_file }}" fstype: swap opts: sw state: present