- name: Create swap file
  command: fallocate -l {{ swap_file_size_mb }}M {{ swap_file_path }}
           creates="{{ swap_file_path }}"
  tags:
    - swap.file.create
  become: yes


- name: Change swap file permissions
  file: path="{{ swap_file_path }}"
        owner=root
        group=root
        mode=0600
  tags:
    - swap.file.permissions
  become: yes


- name: "Check swap file type"
  command: file {{ swap_file_path }}
  register: swapfile
  tags:
    - swap.file.mkswap
  become: yes


- name: Make swap file
  command: "sudo mkswap {{ swap_file_path }}"
  when: swapfile.stdout.find('swap file') == -1
  tags:
    - swap.file.mkswap
  become: yes


- name: Write swap entry in fstab
  mount:
    name: none
    src: "{{ swap_file_path }}"
    fstype: swap
    opts: sw
    passno: '0'
    dump: '0'
    state: present
  tags:
    - swap.fstab
  become: yes


- name: Mount swap
  command: "swapon {{ swap_file_path }}"
  when: ansible_swaptotal_mb < 1
  tags:
    - swap.file.swapon
  become: yes
