common/tasks/swap.yaml
2025-07-11 19:25:53 +02:00

79 lines
2.1 KiB
YAML

---
# manage swap
- name: "manage swap"
tags: "swap"
block:
# enable or disable swap
- name: "swap - set variable"
ansible.builtin.set_fact:
__swap: "{{ swap | default('true') }}"
# verify swapfile
- name: "swap - verify swapfile"
ansible.builtin.stat:
path: "{{ swap_file_location | default('/swapfile') }}"
register: "swap_file_check"
## create swap
# create swap file
- name: "swap - create swap file"
ansible.builtin.command: "fallocate -l {{ swap_file_size }} {{ swap_file_location }}"
when: "not swap_file_check.stat.exists and __swap"
# set swap file permissions
- name: "swap - set permissions "
ansible.builtin.file:
path: "{{ swap_file_location }}"
owner: "root"
group: "root"
mode: "0600"
when: "__swap"
# 'format' swapfile
- name: "swap - format swap file"
ansible.builtin.command: "mkswap {{ swap_file_location }}"
when: "not swap_file_check.stat.exists and __swap"
# configure fstab
- name: "swap - configure fstab"
ansible.posix.mount:
name: "swapfile"
src: "{{ swap_file_location | default('/swapfile') }}"
fstype: "swap"
opts: "sw"
passno: "0"
dump: "0"
state: "present"
when: "__swap"
# enable swap
- name: "swap - enable swap"
ansible.builtin.command: "swapon -a"
when: "not swap_file_check.stat.exists and __swap"
## delete swap
# disable swap
- name: "swap - disable swap"
ansible.builtin.command: "swapoff -a"
when: "swap_file_check.stat.exists and not __swap"
# delete swap file
- name: "swap - delete swap file"
ansible.builtin.file:
path: "{{ swap_file_location }}"
state: "absent"
when: "swap_file_check.stat.exists and not __swap"
# configure fstab
- name: "swap - configure fstab"
ansible.posix.mount:
name: "swapfile"
src: "{{ swap_file_location | default('/swapfile') }}"
fstype: "swap"
opts: "sw"
passno: "0"
dump: "0"
state: "absent"
when: "not __swap"