Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add role "swap" - configure swap space (if not already exists) #160

Merged
merged 5 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions add_pgnode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
- role: add-repository
- role: packages
- role: sudo
- role: swap
- role: sysctl
- role: transparent_huge_pages
- role: pam_limits
Expand Down
1 change: 1 addition & 0 deletions deploy_pgcluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
- role: add-repository
- role: packages
- role: sudo
- role: swap
- role: sysctl
- role: transparent_huge_pages
- role: pam_limits
Expand Down
1 change: 1 addition & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- name: Set variables for molecule
set_fact:
firewall_enable_ipv6: false # Added to prevent test failures in CI.
swap_file_create: false # Added to prevent test failures in CI.
sysctl_set: false # Added to prevent test failures in CI.
nameservers: ["8.8.8.8", "9.9.9.9"]
with_haproxy_load_balancing: true
Expand Down
1 change: 1 addition & 0 deletions molecule/postgrespro/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- name: Set variables for molecule
set_fact:
firewall_enable_ipv6: false # Added to prevent test failures in CI.
swap_file_create: false # Added to prevent test failures in CI.
sysctl_set: false # Added to prevent test failures in CI.
nameservers: ["8.8.8.8", "9.9.9.9"]
with_haproxy_load_balancing: true
Expand Down
68 changes: 68 additions & 0 deletions roles/swap/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
# yamllint disable rule:line-length

- name: Ensure swap exists
command: swapon --show=SIZE --bytes --noheadings
register: swap_exists
changed_when: false
when: swap_file_create|bool
tags: swap, swap_create, swap_remove

- name: Swap exists
debug:
msg: "swap_size_mb: {{ (swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int }}"
when: swap_exists.stdout is defined and swap_exists.stdout | length > 1
tags: swap, swap_create, swap_remove

# if the swap exists and the size is not equal to swap_file_size_mb
- block:
- name: Disable all existing swaps
command: swapoff --all

- name: Remove swap from /etc/fstab
lineinfile:
path: /etc/fstab
state: absent
regexp: ' swap '

- name: Remove swap file (if exists)
file:
path: "{{ swap_file_path }}"
state: absent
when: (swap_exists.stdout is defined and swap_exists.stdout | length > 1) and
((swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int != swap_file_size_mb|int)
tags: swap, swap_remove

# if the swap does not exist
- block:
- name: Create swap file
command: >
dd if=/dev/zero of={{ swap_file_path }} bs=1M count={{ swap_file_size_mb }}
creates='{{ swap_file_path }}'

- name: Set permissions on swap file
file:
path: "{{ swap_file_path }}"
owner: root
group: root
mode: 0600

- name: Make swap file if necessary
command: mkswap {{ swap_file_path }}
register: mkswap_result

- name: Run swapon on the swap file
command: swapon {{ swap_file_path }}

- name: Manage swap file entry in fstab
mount:
name: none
src: "{{ swap_file_path }}"
fstype: swap
opts: sw
state: present
when: (swap_exists.stdout is defined and swap_exists.stdout | length < 1) or
(swap_exists.stdout_lines is defined and (swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int != swap_file_size_mb|int)
tags: swap, swap_create

...
3 changes: 3 additions & 0 deletions tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
- hostname
- dns, nameservers
- etc_hosts
- swap
- - swap_create
- - swap_remove
- sysctl, kernel
- disable_thp
- limits
Expand Down
4 changes: 4 additions & 0 deletions vars/system.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ locale_gen:
# Set system locale (LANG,LC_ALL)
locale: "en_US.utf-8"

# Configure swap space (if not already exists)
swap_file_create: true # or 'false'
swap_file_path: /swapfile
swap_file_size_mb: '2048' # change this value for your system

# Kernel parameters
sysctl_set: true # or 'false'
Expand Down