From 2c3ea84e6f0662f069704eee6e657deb4818ca9d Mon Sep 17 00:00:00 2001
From: Max Gautier <mg@max.gautier.name>
Date: Thu, 7 Dec 2023 13:26:21 +0100
Subject: [PATCH] Use systemd for disabling swap when it's used (#10587)

* Mask systemd swap.target do disable swap

This is a more generic way to disable swap, since it pulls .swap units
in systemd distributions; fstab is only one way to generate .swap units.

* Unconditionally disable swap

We only care to disable it (the "swapon" registered variable is not used
anywhere else.
This allows to get rid of the ignore_errors, since this was added
because swapon.stdout does not exist in check_mode (see issue #6642).

* Don't explicitly disable swapOnZram

We're already masking the swap.target, which would pull the zram unit,
hence no need to handle zram-generator specifically.
---
 .../preinstall/tasks/0010-swapoff.yml         | 28 +++----------------
 1 file changed, 4 insertions(+), 24 deletions(-)

diff --git a/roles/kubernetes/preinstall/tasks/0010-swapoff.yml b/roles/kubernetes/preinstall/tasks/0010-swapoff.yml
index c603f37b1..53bcf2d12 100644
--- a/roles/kubernetes/preinstall/tasks/0010-swapoff.yml
+++ b/roles/kubernetes/preinstall/tasks/0010-swapoff.yml
@@ -1,28 +1,8 @@
 ---
-- name: Remove swapfile from /etc/fstab
-  ansible.posix.mount:
-    name: "{{ item }}"
-    fstype: swap
-    state: absent
-  loop:
-    - swap
-    - none
-
-# kubelet fails even if ansible_swaptotal_mb = 0
-- name: Check swap
-  command: /sbin/swapon -s
-  register: swapon
-  changed_when: no
+- name: Mask swap.target (persist swapoff)
+  ansible.builtin.systemd_service:
+    name: swap.target
+    masked: true
 
 - name: Disable swap
   command: /sbin/swapoff -a
-  when:
-    - swapon.stdout
-  ignore_errors: "{{ ansible_check_mode }}"  # noqa ignore-errors
-
-- name: Disable swapOnZram for Fedora
-  command: touch /etc/systemd/zram-generator.conf
-  when:
-    - swapon.stdout
-    - ansible_distribution in ['Fedora']
-  ignore_errors: "{{ ansible_check_mode }}"  # noqa ignore-errors
-- 
GitLab