- name: set swap_file variable become: true set_fact: swap_file: /swapfile - name: check if swap file exists become: true stat: path: /swapfile register: swap_file_check - name: create swap file become: true command: fallocate -l {{ swap_space }} /swapfile when: not swap_file_check.stat.exists - name: set permissions on swap file become: true file: path: /swapfile mode: 0600 - name: format swap file become: true command: mkswap /swapfile when: not swap_file_check.stat.exists - name: add to fstab become: true lineinfile: dest: /etc/fstab regexp: /swapfile line: "/swapfile none swap sw 0 0" - name: turn on swap become: true command: swapon -a - name: set swapiness become: true sysctl: name: vm.swappiness value: "1"