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/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 diff --git a/roles/swap/tasks/main.yml b/roles/swap/tasks/main.yml new file mode 100644 index 000000000..6754d0d8f --- /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: "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 + +... 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 1ba19c8d8..98df5b843 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'