94 lines
2 KiB
YAML
94 lines
2 KiB
YAML
---
|
|
|
|
# enable or disable swap
|
|
- name: "swap - set variable"
|
|
ansible.builtin.set_fact:
|
|
__swap: "{{ swap | default('true') }}"
|
|
tags:
|
|
- "swap"
|
|
|
|
# verify swapfile
|
|
- name: "swap - verify swapfile"
|
|
ansible.builtin.stat:
|
|
path: "{{ swap_file_location | default('/swapfile') }}"
|
|
register: "swap_file_check"
|
|
tags:
|
|
- "swap"
|
|
|
|
## 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"
|
|
tags:
|
|
- "swap"
|
|
|
|
# set swap file permissions
|
|
- name: "swap - set permissions "
|
|
ansible.builtin.file:
|
|
path: "{{ swap_file_location }}"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: "0600"
|
|
when: "__swap"
|
|
tags:
|
|
- "swap"
|
|
|
|
# 'format' swapfile
|
|
- name: "swap - format swap file"
|
|
ansible.builtin.command: "mkswap {{ swap_file_location }}"
|
|
when: "not swap_file_check.stat.exists and __swap"
|
|
tags:
|
|
- "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"
|
|
tags:
|
|
- "swap"
|
|
|
|
# enable swap
|
|
- name: "swap - enable swap"
|
|
ansible.builtin.command: "swapon -a"
|
|
when: "not swap_file_check.stat.exists and __swap"
|
|
tags:
|
|
- "swap"
|
|
|
|
## delete swap
|
|
# disable swap
|
|
- name: "swap - disable swap"
|
|
ansible.builtin.command: "swapoff -a"
|
|
when: "swap_file_check.stat.exists and not __swap"
|
|
tags:
|
|
- "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"
|
|
tags:
|
|
- "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"
|
|
tags:
|
|
- "swap"
|