Add swap memory with ansible

I based my take on the great Ashley's answer (please upvote it!) and added the following extra features:

  • global flag to manage the swap or not,
  • allow changing the swap size,
  • allow disabling swap,

...plus these technical improvements:

  • full idempotency (only ok & skipping when nothing is changed, otherwise some changed),
  • use dd instead of fallocate for compatibility with XFS fs (see this answer for more info),

Limitations:

  • changes the existing swapfile works correctly only if you provide its path as swap_file_path.

Tested on Centos 7.7 with Ansible 2.9 and Rocky 8 with Ansible 4.8.0.

Requires using privilege escalation as many of the commands need to run as root. (The easiest way to do it is to add --become to ansible-playbook arguments or to set the appropriate value in your ansible.cfg).

Parameters:

swap_configure: true # or false
swap_enable: true # or false
swap_file_path: /swapfile
swap_file_size_mb: 4096
swappiness: 1

Code:


- name: Configure swap
  when: swap_configure | bool
  block:

    - name: Check if swap file exists
      stat:
        path: "{{swap_file_path}}"
        get_checksum: false
        get_md5: false
      register: swap_file_check
      changed_when: false

    - name: Set variable for existing swap file size
      set_fact:
        swap_file_existing_size_mb: "{{ (swap_file_check.stat.size / 1024 / 1024) | int }}"
      when: swap_file_check.stat.exists

    - name: Check if swap is on
      shell: swapon --show | grep {{swap_file_path}}
      register: swap_is_enabled
      changed_when: false
      failed_when: false

    - name: Disable swap
      command: swapoff {{swap_file_path}}
      register: swap_disabled
      when: >
        swap_file_check.stat.exists
        and 'rc' in swap_is_enabled and swap_is_enabled.rc == 0
        and (not swap_enable or (swap_enable and swap_file_existing_size_mb != swap_file_size_mb))

    - name: Delete the swap file
      file:
        path: "{{swap_file_path}}"
        state: absent
      when: not swap_enable

    - name: Remove swap entry from fstab
      mount:
        name: none
        src: "{{swap_file_path}}"
        fstype: swap
        opts: sw
        passno: '0'
        dump: '0'
        state: present
      when: not swap_enable

    - name: Configure swap
      when: swap_enable | bool
      block:

        - name: Create or change the size of swap file
          command: dd if=/dev/zero of={{swap_file_path}} count={{swap_file_size_mb}} bs=1MiB
          register: swap_file_created
          when: >
            not swap_file_check.stat.exists
            or swap_file_existing_size_mb != swap_file_size_mb

        - name: Change swap file permissions
          file:
            path: "{{swap_file_path}}"
            mode: 0600

        - name: Check if swap is formatted
          shell: file {{swap_file_path}} | grep 'swap file'
          register: swap_file_is_formatted
          changed_when: false
          failed_when: false

        - name: Format swap file if it's not formatted
          command: mkswap {{swap_file_path}}
          when: >
            ('rc' in swap_file_is_formatted and swap_file_is_formatted.rc > 0)
            or swap_file_created.changed

        - name: Add swap entry to fstab
          mount:
            name: none
            src: "{{swap_file_path}}"
            fstype: swap
            opts: sw
            passno: '0'
            dump: '0'
            state: present

        - name: Turn on swap
          shell: swapon -a
          # if swap was disabled from the start
          # or has been disabled to change its params
          when: >
            ('rc' in swap_is_enabled and swap_is_enabled.rc != 0)
            or swap_disabled.changed

        - name: Configure swappiness
          sysctl:
            name: vm.swappiness
            value: "{{ swappiness|string }}"
            state: present


I'm not able to reply to Greg Dubicki answer so I add my 2 cents here:

I think adding casting to int when doing calculation on swap_file_size_mb * 1024 * 1024 to transform it into swap_file_size_mb | int * 1024 * 1024 will make the code a bit smarter in case you want to use a fact to extract the final swap size based on the amount of RAM installed like:

swap_file_size_mb: "{{ ansible_memory_mb['real']['total'] * 2 }}"

else it will always resize the swap file even if the size it's the same.


This is my current solution:

- name: Create swap file
  command: dd if=/dev/zero of={{ swap_file_path }} bs=1024 count={{ swap_file_size_mb }}k
           creates="{{ swap_file_path }}"
  tags:
    - swap.file.create


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


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


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


- 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


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

I tried the answer above but "Check swap file type" always came back as changed and therefore isn't idempotent which is encouraged as a best practice when writing Ansible tasks.

The role below has been tested on Ubuntu 14.04 Trusty and doesn't require gather_facts to be enabled.

- name: Set swap_file variable
  set_fact:
    swap_file: "{{swap_file_path}}"
  tags:
    - swap.set.file.path

- name: Check if swap file exists
  stat:
    path: "{{swap_file}}"
  register: swap_file_check
  tags:
    - swap.file.check

- name: Create swap file
  command: fallocate -l {{swap_file_size}} {{swap_file}}
  when: not swap_file_check.stat.exists
  tags:
    - swap.file.create

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

- name: Format swap file
  sudo: yes
  command: "mkswap {{swap_file}}"
  when: not swap_file_check.stat.exists
  tags:
    - swap.file.mkswap

- name: Write swap entry in fstab
  mount: name=none
         src={{swap_file}}
         fstype=swap
         opts=sw
         passno=0
         dump=0
         state=present
  tags:
    - swap.fstab

- name: Turn on swap
  sudo: yes
  command: swapon -a
  when: not swap_file_check.stat.exists
  tags:
    - swap.turn.on

- name: Set swappiness
  sudo: yes
  sysctl:
    name: vm.swappiness
    value: "{{swappiness}}"
  tags:
    - swap.set.swappiness

As of Ansible 2.9, sudo declarations should be become:

- name: Set swap_file variable
  set_fact:
    swap_file: "{{swap_file_path}}"
  tags:
    - swap.set.file.path

- name: Check if swap file exists
  stat:
    path: "{{swap_file}}"
  register: swap_file_check
  tags:
    - swap.file.check

- name: Create swap file
  command: fallocate -l {{swap_file_size}} {{swap_file}}
  when: not swap_file_check.stat.exists
  tags:
    - swap.file.create

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

- name: Format swap file
  become: yes
  command: "mkswap {{swap_file}}"
  when: not swap_file_check.stat.exists
  tags:
    - swap.file.mkswap

- name: Write swap entry in fstab
  mount: name=none
         src={{swap_file}}
         fstype=swap
         opts=sw
         passno=0
         dump=0
         state=present
  tags:
    - swap.fstab

- name: Turn on swap
  become: yes
  command: swapon -a
  when: not swap_file_check.stat.exists
  tags:
    - swap.turn.on

- name: Set swappiness
  become: yes
  sysctl:
    name: vm.swappiness
    value: "{{swappiness}}"
  tags:
    - swap.set.swappiness

Vars required:

swap_file_path: /swapfile
# Use any of the following suffixes
# c=1
# w=2
# b=512
# kB=1000
# K=1024
# MB=1000*1000
# M=1024*1024
# xM=M
# GB=1000*1000*1000
# G=1024*1024*1024
swap_file_size: 4G
swappiness: 1