How to enable swap with salt stack?

How can I create and enable permanent 1GB swap file with salt stack that will work after reboot? salt.states.mount.swap does not allow to define swap size. Furthermore I need to define swappiness. Currently I do it with echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf


I currently use this in production, works for me.

community_swap_file:
  cmd.run:
    - name: |
        [ -f /.swapfile ] || dd if=/dev/zero of=/.swapfile bs=1M count=2048
        chmod 0600 /.swapfile
        mkswap /.swapfile
        echo '/.swapfile      none      swap     sw       0       0' >> /etc/fstab
        swapon -a
    - unless: file /.swapfile 2>&1 | grep -q "Linux/i386 swap"

This is what I use on debian-based systems. It's an improved version of Dan's answer. It gets the available memory from a grain, multiplies it by 2, to create the swapfile with the appropriate size. It also makes an entry in /etc/fstab, if non-existent.

coreutils:
  pkg.installed

/swapfile:
  cmd.run:
    - name: |
        [ -f /swapfile ] || dd if=/dev/zero of=/swapfile bs=1M count={{ grains["mem_total"] * 2 }}
        chmod 0600 /swapfile
        mkswap /swapfile
        swapon -a
    - unless:
      - file /swapfile 2>&1 | grep -q "Linux/i386 swap"
  mount.swap:
    - persist: true

Using fallocate is instantaneous compared to dd. Also, the state below will regenerate the swapfile if you change its size in the pillar. This version also omits the superfluous swapon -a, which mount.swap handles for you.

{% set swapfile = salt['pillar.get']('swapfile', {}) %}
{% set size = swapfile.get('size', grains["mem_total"]) %}
{% set path = swapfile.get('path', '/var/swapfile0') %}

{{ path }}:
  cmd.run:
    - name: |
        swapon --show=NAME --noheadings | grep -q "^{{ path }}$" && swapoff {{ path }}
        rm -f {{ path }}
        fallocate -l {{ size }}M {{ path }}
        chmod 0600 {{ path }}
        mkswap {{ path }}
    - unless: bash -c '[[ $(($(stat -c %s {{ path }}) / 1024**2)) = {{ size }} ]]'

  mount.swap:
    - persist: true