Debian Codegraph @

ansible_collections/community/general/plugins/modules/kernel_blacklist.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2021, Alexei Znamensky (@russoz) <[email protected]>
4 # Copyright (c) 2013, Matthias Vogelgesang <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: kernel_blacklist
12 author:
13 - Matthias Vogelgesang (@matze)
14 short_description: Blacklist kernel modules
15 description:
16 - Add or remove kernel modules from blacklist.
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: full
24 options:
25 name:
26 type: str
27 description:
28 - Name of kernel module to black- or whitelist.
29 required: true
30 state:
31 type: str
32 description:
33 - Whether the module should be present in the blacklist or absent.
34 choices: [absent, present]
35 default: present
36 blacklist_file:
37 type: str
38 description:
39 - If specified, use this blacklist file instead of C(/etc/modprobe.d/blacklist-ansible.conf).
40 default: /etc/modprobe.d/blacklist-ansible.conf
41 """
42
43 EXAMPLES = r"""
44 - name: Blacklist the nouveau driver module
45 community.general.kernel_blacklist:
46 name: nouveau
47 state: present
48 """
49
50 import os
51 import re
52
53 from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper
54
55
56 class Blacklist(StateModuleHelper):
57 output_params = ("name", "state")
58 module = dict(
59 argument_spec=dict(
60 name=dict(type="str", required=True),
61 state=dict(type="str", default="present", choices=["absent", "present"]),
62 blacklist_file=dict(type="str", default="/etc/modprobe.d/blacklist-ansible.conf"),
63 ),
64 supports_check_mode=True,
65 )
66
67 def __init_module__(self):
68 self.pattern = re.compile(rf"^blacklist\s+{re.escape(self.vars.name)}$")
69 self.vars.filename = self.vars.blacklist_file
70 self.vars.set("file_exists", os.path.exists(self.vars.filename), output=False, change=True)
71 if not self.vars.file_exists:
72 with open(self.vars.filename, "a"):
73 pass
74 self.vars.file_exists = True
75 self.vars.set("lines", [], change=True, diff=True)
76 else:
77 with open(self.vars.filename) as fd:
78 self.vars.set("lines", [x.rstrip() for x in fd.readlines()], change=True, diff=True)
79 self.vars.set("is_blacklisted", self._is_module_blocked(), change=True)
80
81 def _is_module_blocked(self):
82 for line in self.vars.lines:
83 stripped = line.strip()
84 if stripped.startswith("#"):
85 continue
86 if self.pattern.match(stripped):
87 return True
88 return False
89
90 def state_absent(self):
91 if not self.vars.is_blacklisted:
92 return
93 self.vars.is_blacklisted = False
94 self.vars.lines = [line for line in self.vars.lines if not self.pattern.match(line.strip())]
95
96 def state_present(self):
97 if self.vars.is_blacklisted:
98 return
99 self.vars.is_blacklisted = True
100 self.vars.lines = self.vars.lines + [f"blacklist {self.vars.name}"]
101
102 def __quit_module__(self):
103 if self.has_changed() and not self.module.check_mode:
104 bkp = self.module.backup_local(self.vars.filename)
105 with open(self.vars.filename, "w") as fd:
106 fd.writelines([f"{x}\n" for x in self.vars.lines])
107 self.module.add_cleanup_file(bkp)
108
109
110 def main():
111 Blacklist.execute()
112
113
114 if __name__ == "__main__":
115 main()