add swapfile role
This commit is contained in:
39
roles/swapfile/README.md
Normal file
39
roles/swapfile/README.md
Normal 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
|
||||
8
roles/swapfile/defaults/main.yml
Normal file
8
roles/swapfile/defaults/main.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
swap_file_path: /swapfile
|
||||
swap_file_size_mb: '512'
|
||||
swap_swappiness: '60'
|
||||
swap_file_state: present
|
||||
swap_file_create_command: "dd if=/dev/zero of={{ swap_file_path }} bs=1M count={{ swap_file_size_mb }}"
|
||||
|
||||
swap_test_mode: false
|
||||
1
roles/swapfile/handlers/main.yml
Normal file
1
roles/swapfile/handlers/main.yml
Normal file
@@ -0,0 +1 @@
|
||||
---
|
||||
17
roles/swapfile/meta/main.yml
Normal file
17
roles/swapfile/meta/main.yml
Normal 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: []
|
||||
19
roles/swapfile/molecule/default/converge.yml
Normal file
19
roles/swapfile/molecule/default/converge.yml
Normal 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.swapfile
|
||||
swap_file_path: /swapfile
|
||||
swap_file_size_mb: 2000
|
||||
swap_swappiness: 30
|
||||
swap_file_state: present
|
||||
swap_test_mode: true
|
||||
29
roles/swapfile/molecule/default/molecule.yml
Normal file
29
roles/swapfile/molecule/default/molecule.yml
Normal 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 .
|
||||
26
roles/swapfile/molecule/default/verify.yml
Normal file
26
roles/swapfile/molecule/default/verify.yml
Normal 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 }}')"
|
||||
11
roles/swapfile/tasks/disable.yml
Normal file
11
roles/swapfile/tasks/disable.yml
Normal 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: "{{ swap_file_path }}"
|
||||
state: absent
|
||||
47
roles/swapfile/tasks/enable.yml
Normal file
47
roles/swapfile/tasks/enable.yml
Normal 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: this
|
||||
failed_when: not this.stat.exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ swap_file_path }}"
|
||||
get_checksum: false
|
||||
get_mime: false
|
||||
get_attributes: false
|
||||
|
||||
rescue:
|
||||
- name: "Enable | ensure swap file exists"
|
||||
register: swap_file_create
|
||||
ansible.builtin.command:
|
||||
cmd: "{{ swap_file_create_command }}"
|
||||
creates: "{{ swap_file_path }}"
|
||||
|
||||
- name: "Enable | set permissions on swap file"
|
||||
ansible.builtin.file:
|
||||
path: "{{ swap_file_path }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0600"
|
||||
|
||||
- name: "Enable | make swap" # noqa: no-handler no-changed-when
|
||||
register: mkswap_result
|
||||
when: swap_file_create is changed
|
||||
ansible.builtin.command:
|
||||
cmd: mkswap {{ swap_file_path }}
|
||||
|
||||
- name: "Enable | run swapon on the swap file => '{{ swap_file_path }}'"
|
||||
when:
|
||||
- mkswap_result is changed
|
||||
- not swap_test_mode
|
||||
changed_when: false
|
||||
ansible.builtin.command:
|
||||
cmd: swapon {{ swap_file_path }}
|
||||
|
||||
|
||||
- name: "Set swappiness to '{{ swap_swappiness }}'"
|
||||
ansible.posix.sysctl:
|
||||
name: vm.swappiness
|
||||
value: "{{ swap_swappiness }}"
|
||||
state: present
|
||||
18
roles/swapfile/tasks/main.yml
Normal file
18
roles/swapfile/tasks/main.yml
Normal file
@@ -0,0 +1,18 @@
|
||||
---
|
||||
- name: "Manage swap file entry in fstab"
|
||||
ansible.posix.mount:
|
||||
name: none
|
||||
src: "{{ swap_file_path }}"
|
||||
fstype: swap
|
||||
opts: sw
|
||||
state: "{{ swap_file_state }}"
|
||||
|
||||
|
||||
- name: "Include disable swap tasks"
|
||||
when: swap_file_state == 'absent'
|
||||
ansible.builtin.include_tasks: disable.yml
|
||||
|
||||
|
||||
- name: "Include enable swap tasks"
|
||||
when: swap_file_state == 'present'
|
||||
ansible.builtin.include_tasks: enable.yml
|
||||
1
roles/swapfile/vars/main.yml
Normal file
1
roles/swapfile/vars/main.yml
Normal file
@@ -0,0 +1 @@
|
||||
---
|
||||
Reference in New Issue
Block a user