From e678b7ee4f8fc5ad2ceba965dbca92ba2f76f0be Mon Sep 17 00:00:00 2001 From: Vitaliy Kukharik Date: Tue, 19 Apr 2022 14:11:39 +0300 Subject: [PATCH 1/5] add role "swap" - configure swap space (if not already exists) Variables: swap_file_create, swap_file_path, swap_file_size_mb --- add_pgnode.yml | 1 + deploy_pgcluster.yml | 1 + roles/swap/tasks/main.yml | 68 +++++++++++++++++++++++++++++++++++++++ tags.md | 3 ++ vars/system.yml | 4 +++ 5 files changed, 77 insertions(+) create mode 100644 roles/swap/tasks/main.yml diff --git a/add_pgnode.yml b/add_pgnode.yml index d8bee43d5..46606e2c2 100644 --- a/add_pgnode.yml +++ b/add_pgnode.yml @@ -79,6 +79,7 @@ - role: add-repository - role: packages - role: sudo + - role: swap - role: sysctl - role: transparent_huge_pages - role: pam_limits diff --git a/deploy_pgcluster.yml b/deploy_pgcluster.yml index 0dbdaebad..d2afe6764 100644 --- a/deploy_pgcluster.yml +++ b/deploy_pgcluster.yml @@ -97,6 +97,7 @@ - role: add-repository - role: packages - role: sudo + - role: swap - role: sysctl - role: transparent_huge_pages - role: pam_limits diff --git a/roles/swap/tasks/main.yml b/roles/swap/tasks/main.yml new file mode 100644 index 000000000..6adb238f0 --- /dev/null +++ b/roles/swap/tasks/main.yml @@ -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: "Size {{ (swap_exists.stdout|int / 1024 / 1024)|round|int }}" + when: swap_file_create|bool + 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_file_create|bool and swap_exists.stdout | length > 1) + and ((swap_exists.stdout|int / 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_file_create|bool and swap_exists.stdout | length < 1) or + ((swap_exists.stdout|int / 1024 / 1024)|round|int != swap_file_size_mb|int) + tags: swap, swap_create + +... diff --git a/tags.md b/tags.md index ab00d06e5..d049c6478 100644 --- a/tags.md +++ b/tags.md @@ -13,6 +13,9 @@ - hostname - dns, nameservers - etc_hosts +- swap +- - swap_create +- - swap_remove - sysctl, kernel - disable_thp - limits diff --git a/vars/system.yml b/vars/system.yml index c0a73f061..1521dafbb 100644 --- a/vars/system.yml +++ b/vars/system.yml @@ -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' From 81ade454519c45d7a89f5c30ca94fd7b53d196ec Mon Sep 17 00:00:00 2001 From: Vitaliy Kukharik Date: Tue, 19 Apr 2022 14:23:58 +0300 Subject: [PATCH 2/5] Molecule: set "swap_file_create: false" (to prevent test failures in CI) --- molecule/default/converge.yml | 1 + molecule/postgrespro/converge.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index c876d41a2..93bf8e299 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -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 diff --git a/molecule/postgrespro/converge.yml b/molecule/postgrespro/converge.yml index 560964739..5d8559a66 100644 --- a/molecule/postgrespro/converge.yml +++ b/molecule/postgrespro/converge.yml @@ -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 From 85fac76d7c3f93e16265583b0ba3c7fedf786204 Mon Sep 17 00:00:00 2001 From: Vitaliy Kukharik Date: Tue, 19 Apr 2022 15:20:29 +0300 Subject: [PATCH 3/5] swap: fix the condition --- roles/swap/tasks/main.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/roles/swap/tasks/main.yml b/roles/swap/tasks/main.yml index 6adb238f0..4090a0f40 100644 --- a/roles/swap/tasks/main.yml +++ b/roles/swap/tasks/main.yml @@ -11,7 +11,7 @@ - name: Swap exists debug: msg: "Size {{ (swap_exists.stdout|int / 1024 / 1024)|round|int }}" - when: swap_file_create|bool + 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 @@ -29,7 +29,7 @@ file: path: "{{ swap_file_path }}" state: absent - when: (swap_file_create|bool and swap_exists.stdout | length > 1) + when: (swap_exists.stdout is defined and swap_exists.stdout | length > 1) and ((swap_exists.stdout|int / 1024 / 1024)|round|int != swap_file_size_mb|int) tags: swap, swap_remove @@ -61,8 +61,8 @@ fstype: swap opts: sw state: present - when: (swap_file_create|bool and swap_exists.stdout | length < 1) or - ((swap_exists.stdout|int / 1024 / 1024)|round|int != swap_file_size_mb|int) + when: (swap_exists.stdout is defined and swap_exists.stdout | length < 1) or + (swap_exists.stdout is defined and (swap_exists.stdout|int / 1024 / 1024)|round|int != swap_file_size_mb|int) tags: swap, swap_create ... From 71d3e100d7684ed0f001ec9ea68cc81552ba9a3d Mon Sep 17 00:00:00 2001 From: Vitaliy Kukharik Date: Tue, 19 Apr 2022 16:16:56 +0300 Subject: [PATCH 4/5] swap role: the handle multiple existing swaps Example: [root@host-05 ~]# swapon --show=SIZE --noheadings 1000M 20M Result: TASK [swap : Swap exists] ******************** ok: [10.128.64.157] => { "msg": "swap_size_mb: 1020" } --- roles/swap/tasks/main.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/roles/swap/tasks/main.yml b/roles/swap/tasks/main.yml index 4090a0f40..a3d4454e8 100644 --- a/roles/swap/tasks/main.yml +++ b/roles/swap/tasks/main.yml @@ -10,7 +10,7 @@ - name: Swap exists debug: - msg: "Size {{ (swap_exists.stdout|int / 1024 / 1024)|round|int }}" + 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 @@ -29,8 +29,8 @@ file: path: "{{ swap_file_path }}" state: absent - when: (swap_exists.stdout is defined and swap_exists.stdout | length > 1) - and ((swap_exists.stdout|int / 1024 / 1024)|round|int != swap_file_size_mb|int) + 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 @@ -62,7 +62,7 @@ opts: sw state: present when: (swap_exists.stdout is defined and swap_exists.stdout | length < 1) or - (swap_exists.stdout is defined and (swap_exists.stdout|int / 1024 / 1024)|round|int != swap_file_size_mb|int) + ((swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int != swap_file_size_mb|int) tags: swap, swap_create ... From 95d8ff12bb1412b15f44989e5fa86e1b5d9ac277 Mon Sep 17 00:00:00 2001 From: Vitaliy Kukharik Date: Tue, 19 Apr 2022 16:31:05 +0300 Subject: [PATCH 5/5] swap role: fix the condition --- roles/swap/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/swap/tasks/main.yml b/roles/swap/tasks/main.yml index a3d4454e8..6754d0d8f 100644 --- a/roles/swap/tasks/main.yml +++ b/roles/swap/tasks/main.yml @@ -62,7 +62,7 @@ opts: sw state: present when: (swap_exists.stdout is defined and swap_exists.stdout | length < 1) or - ((swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int != swap_file_size_mb|int) + (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 ...