--- - name: "Add a swap file of {{ swap_file_size }} MBytes" become: true become_user: root shell: cmd: | # does the swap file already exist? if grep -q "swapfile" /etc/fstab; then echo 'swapfile found. No changes made.' exit 0 fi echo 'swapfile not found. Adding swapfile...' set -ex fallocate -l '{{ swap_file_size }}M' /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo '/swapfile none swap defaults 0 0' >> /etc/fstab echo 'swapfile added.' exit 0 register: add_swap_file changed_when: >- 'swapfile added' in add_swap_file.stdout