Skip to content

Commit

Permalink
Merge pull request #160 from vitabaks/dev
Browse files Browse the repository at this point in the history
add role "swap" -  configure swap space (if not already exists)
  • Loading branch information
vitabaks authored Apr 19, 2022
2 parents 661fd0c + 95d8ff1 commit d17a520
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 0 deletions.
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

0 comments on commit d17a520

Please sign in to comment.