--- - name: Obtain current state of swap command: swapon --noheadings --show=NAME register: swapon - name: Setup swap if none already, to prevent kernel firing off the OOM killer block: - name: A unique swapfile path is generated command: mktemp --tmpdir=/root swapfile_XXX register: swapfilepath - name: Swap file path is buffered set_fact: swapfilepath: '{{ swapfilepath.stdout | trim }}' - name: Set swap file permissions file: path: "{{ swapfilepath }}" owner: root group: root mode: 0600 - name: Swapfile padded to swapfile_size & timed to help debug any performance problems shell: 'time dd if=/dev/zero of={{ swapfilepath }} bs={{ swapfileGB }}M count=1024' - name: Swap file is formatted command: 'mkswap {{ swapfilepath }}' - name: Write swap entry in fstab mount: path: none src: "{{ swapfilepath }}" fstype: swap opts: sw state: present - name: Mount swap command: "swapon -a" when: not (swapon.stdout_lines | length)