Merge pull request #41 from corvus-migratorius/role-swapfile

add swapfile role
This commit is contained in:
Fogucoco
2025-12-18 11:33:57 +03:00
committed by GitHub
13 changed files with 218 additions and 1 deletions

View File

@@ -23,6 +23,7 @@
- [rustdesk](roles/rustdesk/README.md)
- [sftp_share](roles/sftp_share/README.md)
- [smartctl_exporter](roles/smartctl_exporter/README.md)
- [swapfile](roles/swapfile/README.md)
- [ufw](roles/ufw/README.md)
- [wg_hub](roles/wg_hub/README.md)
- [wg_spoke](roles/wg_spoke/README.md)

View File

@@ -1,7 +1,7 @@
---
namespace: genlab
name: common
version: 0.21.0
version: 0.22.0
readme: README.md
authors:
- Alexander Gorelyshev (corvus-migratorius@proton.me)

39
roles/swapfile/README.md Normal file
View File

@@ -0,0 +1,39 @@
ansible-swapfile
=========
Create/destroy a swapfile and run swapon/-off on it.
Massively borrowing from https://github.com/geerlingguy/ansible-role-swap.
Requirements
------------
None
Role Variables
--------------
None
Dependencies
------------
None
Example Playbook
----------------
```yaml
roles:
- role: genlab.swapfile
```
License
-------
BSD
Author Information
------------------
corvus-migratorius@proton.me

View File

@@ -0,0 +1,8 @@
---
swapfile_path: /swapfile
swapfile_size_mb: '512'
swapfile_swappiness: '60'
swapfile_state: present
swapfile_create_command: "dd if=/dev/zero of={{ swapfile_path }} bs=1M count={{ swapfile_size_mb }}"
swapfile_test_mode: false

View File

@@ -0,0 +1 @@
---

View File

@@ -0,0 +1,17 @@
---
galaxy_info:
role_name: "swapfile"
namespace: genlab
author: "Alexander Gorelyshev"
company: "Genlab, LLC"
description: ""
license: "MIT"
min_ansible_version: "2.1"
platforms:
- name: "Ubuntu"
versions: ["focal", "jammy"]
galaxy_tags: []
dependencies: []

View File

@@ -0,0 +1,19 @@
---
- name: Converge
hosts: all
become: true
pre_tasks:
- name: "Update apt cache"
when: ansible_os_family == 'Debian'
ansible.builtin.apt:
update_cache: true
cache_valid_time: 600
roles:
- role: genlab.common.swapfile
swapfile_path: /swapfile
swapfile_size_mb: 2000
swapfile_swappiness: 30
swapfile_state: present
swapfile_test_mode: true

View File

@@ -0,0 +1,29 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2204}-ansible:latest
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
privileged: true
pre_build_image: true
provisioner:
name: ansible
playbooks:
converge: ${MOLECULE_PLAYBOOK:-converge.yml}
verifier:
name: ansible
lint: |
set -e
yamllint .
ansible-lint .

View File

@@ -0,0 +1,26 @@
---
- name: Verify
hosts: all
gather_facts: false
any_errors_fatal: true
vars:
swapfile_path: "/swapfile"
pre_tasks:
- name: "Ensure 'file' exists"
ansible.builtin.package:
name: [file]
state: present
tasks:
- name: "Check swapfile file type"
register: swapfile_info
changed_when: false
ansible.builtin.command:
cmd: "file {{ swapfile_path }}"
- name: "Verify correct file type"
ansible.builtin.assert:
that: "'swap file' in swapfile_info.stdout"
success_msg: "{{ swapfile_path }}: Indeed, a Linux swap file"
fail_msg: "{{ swapfile_path }}: not a Linux swap file ('{{ swapfile_info.stdout }}')"

View File

@@ -0,0 +1,11 @@
---
- name: "Disable | disable swap"
changed_when: false
ansible.builtin.command:
cmd: swapoff -a
- name: "Disable | ensure swap file doesn't exist"
ansible.builtin.file:
path: "{{ swapfile_path }}"
state: absent

View File

@@ -0,0 +1,47 @@
---
# https://justinmontgomery.com/speed-up-stat-command-in-ansible
- name: "Enable | create the swap file"
block:
- name: "Enable | stat the swap file"
register: swapfile_this
failed_when: not swapfile_this.stat.exists
ansible.builtin.stat:
path: "{{ swapfile_path }}"
get_checksum: false
get_mime: false
get_attributes: false
rescue:
- name: "Enable | ensure swap file exists"
register: swapfile_create
ansible.builtin.command:
cmd: "{{ swapfile_create_command }}"
creates: "{{ swapfile_path }}"
- name: "Enable | set permissions on swap file"
ansible.builtin.file:
path: "{{ swapfile_path }}"
owner: root
group: root
mode: "0600"
- name: "Enable | make swap" # noqa: no-handler no-changed-when
register: swapfile_mkswap_result
when: swapfile_create is changed
ansible.builtin.command:
cmd: mkswap {{ swapfile_path }}
- name: "Enable | run swapon on the swap file => '{{ swapfile_path }}'"
when:
- swapfile_mkswap_result is changed
- not swapfile_test_mode
changed_when: false
ansible.builtin.command:
cmd: swapon {{ swapfile_path }}
- name: "Set swappiness to '{{ swapfile_swappiness }}'"
ansible.posix.sysctl:
name: vm.swappiness
value: "{{ swapfile_swappiness }}"
state: present

View File

@@ -0,0 +1,18 @@
---
- name: "Manage swap file entry in fstab"
ansible.posix.mount:
name: none
src: "{{ swapfile_path }}"
fstype: swap
opts: sw
state: "{{ swapfile_state }}"
- name: "Include disable swap tasks"
when: swapfile_state == 'absent'
ansible.builtin.include_tasks: disable.yml
- name: "Include enable swap tasks"
when: swapfile_state == 'present'
ansible.builtin.include_tasks: enable.yml

View File

@@ -0,0 +1 @@
---