--- # adapted from https://stackoverflow.com/a/24765946 - name: Create swap files command: dd if=/dev/zero of={{ '%s%d' | format(swap_file_path, item) }} bs=1024 count={{ swap_file_size }}K creates="{{ '%s%d' | format(swap_file_path, item) }}" loop: "{{ range(1, swap_file_count + 1) }}" - name: Change swap file permissions file: path="{{ '%s%d' | format(swap_file_path, item) }}" owner=root group=root mode=0600 loop: "{{ range(1, swap_file_count + 1) }}" - name: Check swap file types command: file {{ '%s%d' | format(swap_file_path, item) }} register: swapfile loop: "{{ range(1, swap_file_count + 1) }}" - name: Make swap files command: "mkswap {{ '%s%d' | format(swap_file_path, item) }}" become: true when: swapfile.results[item - 1].stdout.find('swap file') == -1 loop: "{{ range(1, swap_file_count + 1) }}" - name: Write swap entries in fstab mount: name=none src={{ '%s%d' | format(swap_file_path, item) }} fstype=swap opts=sw passno=0 dump=0 state=present loop: "{{ range(1, swap_file_count + 1) }}" - name: Mount swap command: "swapon --all" become: true