From df89012081f12b44bf41708d840460f1ce6ad214 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:15:28 +0200 Subject: [PATCH] [PR #7382/33133f3b backport][stable-7] kernel_blacklist: bugfix (#7409) kernel_blacklist: bugfix (#7382) * kernel_blacklist: bugfix * add fix + changelog frag * skip aix,freebsd,macos,osx in integration test * Update changelogs/fragments/7382-kernel-blacklist-bugfix.yml Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit 33133f3ba9864e965f7b7ce002485f867606b009) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../7382-kernel-blacklist-bugfix.yml | 2 + plugins/modules/kernel_blacklist.py | 15 ++--- .../targets/kernel_blacklist/aliases | 4 ++ .../kernel_blacklist/handlers/main.yml | 10 ++++ .../targets/kernel_blacklist/tasks/main.yml | 56 +++++++++++++++---- 5 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 changelogs/fragments/7382-kernel-blacklist-bugfix.yml create mode 100644 tests/integration/targets/kernel_blacklist/handlers/main.yml diff --git a/changelogs/fragments/7382-kernel-blacklist-bugfix.yml b/changelogs/fragments/7382-kernel-blacklist-bugfix.yml new file mode 100644 index 0000000000..6303034d5c --- /dev/null +++ b/changelogs/fragments/7382-kernel-blacklist-bugfix.yml @@ -0,0 +1,2 @@ +bugfixes: + - kernel_blacklist - simplified the mechanism to update the file, fixing the error (https://github.com/ansible-collections/community.general/pull/7382, https://github.com/ansible-collections/community.general/issues/7362). diff --git a/plugins/modules/kernel_blacklist.py b/plugins/modules/kernel_blacklist.py index 1b40999cae..b5bd904036 100644 --- a/plugins/modules/kernel_blacklist.py +++ b/plugins/modules/kernel_blacklist.py @@ -53,7 +53,6 @@ EXAMPLES = ''' import os import re -import tempfile from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper @@ -106,16 +105,10 @@ class Blacklist(StateModuleHelper): def __quit_module__(self): if self.has_changed() and not self.module.check_mode: - dummy, tmpfile = tempfile.mkstemp() - try: - os.remove(tmpfile) - self.module.preserved_copy(self.vars.filename, tmpfile) # ensure right perms/ownership - with open(tmpfile, 'w') as fd: - fd.writelines(["{0}\n".format(x) for x in self.vars.lines]) - self.module.atomic_move(tmpfile, self.vars.filename) - finally: - if os.path.exists(tmpfile): - os.remove(tmpfile) + bkp = self.module.backup_local(self.vars.filename) + with open(self.vars.filename, "w") as fd: + fd.writelines(["{0}\n".format(x) for x in self.vars.lines]) + self.module.add_cleanup_file(bkp) def main(): diff --git a/tests/integration/targets/kernel_blacklist/aliases b/tests/integration/targets/kernel_blacklist/aliases index afda346c4e..b85ae64190 100644 --- a/tests/integration/targets/kernel_blacklist/aliases +++ b/tests/integration/targets/kernel_blacklist/aliases @@ -3,3 +3,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later azp/posix/1 +skip/aix +skip/freebsd +skip/osx +skip/macos diff --git a/tests/integration/targets/kernel_blacklist/handlers/main.yml b/tests/integration/targets/kernel_blacklist/handlers/main.yml new file mode 100644 index 0000000000..814c9c51a8 --- /dev/null +++ b/tests/integration/targets/kernel_blacklist/handlers/main.yml @@ -0,0 +1,10 @@ +--- +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +- name: Remove modprobe.d + ansible.builtin.file: + path: /etc/modprobe.d + state: absent + \ No newline at end of file diff --git a/tests/integration/targets/kernel_blacklist/tasks/main.yml b/tests/integration/targets/kernel_blacklist/tasks/main.yml index 45136e119c..48cd38a937 100644 --- a/tests/integration/targets/kernel_blacklist/tasks/main.yml +++ b/tests/integration/targets/kernel_blacklist/tasks/main.yml @@ -46,12 +46,12 @@ - name: assert file is unchanged assert: that: - - bl_test_1 is not changed - - bl_test_1a is not changed - - orig_stat.stat.size == stat_test_1.stat.size - - orig_stat.stat.checksum == stat_test_1.stat.checksum - - orig_stat.stat.mtime == stat_test_1.stat.mtime - - stat_test_1.stat.checksum == expected_content | checksum + - bl_test_1 is not changed + - bl_test_1a is not changed + - orig_stat.stat.size == stat_test_1.stat.size + - orig_stat.stat.checksum == stat_test_1.stat.checksum + - orig_stat.stat.mtime == stat_test_1.stat.mtime + - stat_test_1.stat.checksum == expected_content | checksum vars: expected_content: | # Copyright (c) Ansible Project @@ -65,7 +65,7 @@ - name: test deprecation assert: that: - - "'deprecations' not in bl_test_1 or (ansible_version.major == 2 and ansible_version.minor == 12)" + - "'deprecations' not in bl_test_1 or (ansible_version.major == 2 and ansible_version.minor == 12)" - name: add new item to list community.general.kernel_blacklist: @@ -82,8 +82,8 @@ - name: assert element is added assert: that: - - bl_test_2 is changed - - slurp_test_2.content|b64decode == content + - bl_test_2 is changed + - slurp_test_2.content|b64decode == content vars: content: | # Copyright (c) Ansible Project @@ -107,11 +107,11 @@ src: '{{ bl_file }}' register: slurp_test_3 -- name: assert element is added +- name: assert element is removed assert: that: - - bl_test_3 is changed - - slurp_test_3.content|b64decode == content + - bl_test_3 is changed + - slurp_test_3.content|b64decode == content vars: content: | # Copyright (c) Ansible Project @@ -121,3 +121,35 @@ blacklist aaaa blacklist cccc blacklist dddd + +############################################################################################################################################ +# +# Issue 7362 +# + +- name: Create /etc/modprobe.d + ansible.builtin.file: + path: /etc/modprobe.d + state: directory + mode: '0755' + owner: root + group: root + notify: Remove modprobe.d + +- name: Create cls_rsvp file + ansible.builtin.copy: + dest: /etc/modprobe.d/cls_rsvp-blacklist.conf + content: | + blacklist cls_rsvp + mode: '0644' + +- name: Block potentially affected (and unused) modules (7362) + community.general.kernel_blacklist: + name: "{{ line_item }}" + state: present + blacklist_file: "/etc/modprobe.d/{{ line_item }}-blacklist.conf" + with_items: + - cifs + - cls_rsvp + loop_control: + loop_var: line_item