- name: Check if swap file exists stat: path: /swapfile register: swap_file_check - name: create file for swap shell: /usr/bin/fallocate -l 1G /swapfile when: not swap_file_check.stat.exists - name: format swap file shell: /sbin/mkswap /swapfile when: not swap_file_check.stat.exists - name: set permission to swap file file: path=/swapfile owner=root group=root mode=0600 - name: add swap file to system shell: /sbin/swapon /swapfile when: not swap_file_check.stat.exists - name: write changes to fstab mount: name=none src=/swapfile fstype=swap opts=sw passno=0 dump=0 state=present