2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2021, Alexei Znamensky (@russoz) <russoz@gmail.com>
|
|
|
|
# Copyright (c) 2013, Matthias Vogelgesang <matthias.vogelgesang@gmail.com>
|
|
|
|
# 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
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: kernel_blacklist
|
|
|
|
author:
|
2023-02-20 17:28:47 +01:00
|
|
|
- Matthias Vogelgesang (@matze)
|
2020-03-09 10:11:07 +01:00
|
|
|
short_description: Blacklist kernel modules
|
|
|
|
description:
|
|
|
|
- Add or remove kernel modules from blacklist.
|
2023-02-20 17:28:47 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: full
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Name of kernel module to black- or whitelist.
|
|
|
|
required: true
|
|
|
|
state:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Whether the module should be present in the blacklist or absent.
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
|
|
|
blacklist_file:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- If specified, use this blacklist file instead of
|
|
|
|
C(/etc/modprobe.d/blacklist-ansible.conf).
|
2021-09-20 19:39:35 +02:00
|
|
|
default: /etc/modprobe.d/blacklist-ansible.conf
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Blacklist the nouveau driver module
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.kernel_blacklist:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: nouveau
|
|
|
|
state: present
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2021-09-20 19:39:35 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-09-20 19:39:35 +02:00
|
|
|
class Blacklist(StateModuleHelper):
|
|
|
|
output_params = ('name', 'state')
|
|
|
|
module = dict(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
blacklist_file=dict(type='str', default='/etc/modprobe.d/blacklist-ansible.conf'),
|
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-09-20 19:39:35 +02:00
|
|
|
def __init_module__(self):
|
|
|
|
self.pattern = re.compile(r'^blacklist\s+{0}$'.format(re.escape(self.vars.name)))
|
|
|
|
self.vars.filename = self.vars.blacklist_file
|
|
|
|
self.vars.set('file_exists', os.path.exists(self.vars.filename), output=False, change=True)
|
|
|
|
if not self.vars.file_exists:
|
|
|
|
with open(self.vars.filename, 'a'):
|
|
|
|
pass
|
|
|
|
self.vars.file_exists = True
|
|
|
|
self.vars.set('lines', [], change=True, diff=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-09-20 19:39:35 +02:00
|
|
|
with open(self.vars.filename) as fd:
|
|
|
|
self.vars.set('lines', [x.rstrip() for x in fd.readlines()], change=True, diff=True)
|
|
|
|
self.vars.set('is_blacklisted', self._is_module_blocked(), change=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-09-20 19:39:35 +02:00
|
|
|
def _is_module_blocked(self):
|
|
|
|
for line in self.vars.lines:
|
2020-03-09 10:11:07 +01:00
|
|
|
stripped = line.strip()
|
|
|
|
if stripped.startswith('#'):
|
|
|
|
continue
|
2021-09-20 19:39:35 +02:00
|
|
|
if self.pattern.match(stripped):
|
2020-03-09 10:11:07 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-09-20 19:39:35 +02:00
|
|
|
def state_absent(self):
|
|
|
|
if not self.vars.is_blacklisted:
|
|
|
|
return
|
|
|
|
self.vars.is_blacklisted = False
|
|
|
|
self.vars.lines = [line for line in self.vars.lines if not self.pattern.match(line.strip())]
|
|
|
|
|
|
|
|
def state_present(self):
|
|
|
|
if self.vars.is_blacklisted:
|
|
|
|
return
|
|
|
|
self.vars.is_blacklisted = True
|
|
|
|
self.vars.lines = self.vars.lines + ['blacklist %s' % self.vars.name]
|
|
|
|
|
|
|
|
def __quit_module__(self):
|
|
|
|
if self.has_changed() and not self.module.check_mode:
|
2023-10-18 20:53:31 +02:00
|
|
|
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)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-09-20 19:39:35 +02:00
|
|
|
Blacklist.execute()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|