1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-06 12:07:14 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -89,32 +89,31 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
RELEASE_VER = platform.release()
MODULES_LOAD_LOCATION = '/etc/modules-load.d'
PARAMETERS_FILES_LOCATION = '/etc/modprobe.d'
MODULES_LOAD_LOCATION = "/etc/modules-load.d"
PARAMETERS_FILES_LOCATION = "/etc/modprobe.d"
class Modprobe:
def __init__(self, module):
self.module = module
self.modprobe_bin = module.get_bin_path('modprobe', True)
self.modprobe_bin = module.get_bin_path("modprobe", True)
self.check_mode = module.check_mode
self.desired_state = module.params['state']
self.name = module.params['name']
self.params = module.params['params']
self.persistent = module.params['persistent']
self.desired_state = module.params["state"]
self.name = module.params["name"]
self.params = module.params["params"]
self.persistent = module.params["persistent"]
self.changed = False
self.re_find_module = re.compile(rf'^ *{self.name} *(?:[#;].*)?\n?\Z')
self.re_find_params = re.compile(rf'^options {self.name} \w+=\S+ *(?:[#;].*)?\n?\Z')
self.re_get_params_and_values = re.compile(rf'^options {self.name} (\w+=\S+) *(?:[#;].*)?\n?\Z')
self.re_find_module = re.compile(rf"^ *{self.name} *(?:[#;].*)?\n?\Z")
self.re_find_params = re.compile(rf"^options {self.name} \w+=\S+ *(?:[#;].*)?\n?\Z")
self.re_get_params_and_values = re.compile(rf"^options {self.name} (\w+=\S+) *(?:[#;].*)?\n?\Z")
def load_module(self):
command = [self.modprobe_bin]
if self.check_mode:
command.append('-n')
command.append("-n")
command.extend([self.name] + shlex.split(self.params))
rc, out, err = self.module.run_command(command)
@ -126,7 +125,7 @@ class Modprobe:
self.changed = True
else:
rc, stdout, stderr = self.module.run_command(
[self.modprobe_bin, '-n', '--first-time', self.name] + shlex.split(self.params)
[self.modprobe_bin, "-n", "--first-time", self.name] + shlex.split(self.params)
)
if rc != 0:
self.module.warn(stderr)
@ -161,26 +160,23 @@ class Modprobe:
return params
def create_module_file(self):
file_path = os.path.join(MODULES_LOAD_LOCATION,
f"{self.name}.conf")
file_path = os.path.join(MODULES_LOAD_LOCATION, f"{self.name}.conf")
if not self.check_mode:
with open(file_path, 'w') as file:
with open(file_path, "w") as file:
file.write(f"{self.name}\n")
@property
def module_options_file_content(self):
file_content = '\n'.join([f'options {self.name} {param}' for param in self.params.split()])
file_content = "\n".join([f"options {self.name} {param}" for param in self.params.split()])
return f"{file_content}\n"
def create_module_options_file(self):
new_file_path = os.path.join(PARAMETERS_FILES_LOCATION,
f"{self.name}.conf")
new_file_path = os.path.join(PARAMETERS_FILES_LOCATION, f"{self.name}.conf")
if not self.check_mode:
with open(new_file_path, 'w') as file:
with open(new_file_path, "w") as file:
file.write(self.module_options_file_content)
def disable_old_params(self):
for modprobe_file in self.modprobe_files:
with open(modprobe_file) as file:
file_content = file.readlines()
@ -192,11 +188,10 @@ class Modprobe:
content_changed = True
if not self.check_mode and content_changed:
with open(modprobe_file, 'w') as file:
file.write('\n'.join(file_content))
with open(modprobe_file, "w") as file:
file.write("\n".join(file_content))
def disable_module_permanent(self):
for module_file in self.modules_files:
with open(module_file) as file:
file_content = file.readlines()
@ -208,11 +203,10 @@ class Modprobe:
content_changed = True
if not self.check_mode and content_changed:
with open(module_file, 'w') as file:
file.write('\n'.join(file_content))
with open(module_file, "w") as file:
file.write("\n".join(file_content))
def load_module_permanent(self):
if not self.module_is_loaded_persistently:
self.create_module_file()
self.changed = True
@ -235,22 +229,22 @@ class Modprobe:
def modules_files(self):
if not os.path.isdir(MODULES_LOAD_LOCATION):
return []
modules_paths = [os.path.join(MODULES_LOAD_LOCATION, path)
for path in os.listdir(MODULES_LOAD_LOCATION)]
modules_paths = [os.path.join(MODULES_LOAD_LOCATION, path) for path in os.listdir(MODULES_LOAD_LOCATION)]
return [path for path in modules_paths if os.path.isfile(path)]
@property
def modprobe_files(self):
if not os.path.isdir(PARAMETERS_FILES_LOCATION):
return []
modules_paths = [os.path.join(PARAMETERS_FILES_LOCATION, path)
for path in os.listdir(PARAMETERS_FILES_LOCATION)]
modules_paths = [
os.path.join(PARAMETERS_FILES_LOCATION, path) for path in os.listdir(PARAMETERS_FILES_LOCATION)
]
return [path for path in modules_paths if os.path.isfile(path)]
def module_loaded(self):
is_loaded = False
try:
with open('/proc/modules') as modules:
with open("/proc/modules") as modules:
module_name = f"{self.name.replace('-', '_')} "
for line in modules:
if line.startswith(module_name):
@ -259,7 +253,7 @@ class Modprobe:
if not is_loaded:
module_file = f"/{self.name}.ko"
builtin_path = os.path.join('/lib/modules/', RELEASE_VER, 'modules.builtin')
builtin_path = os.path.join("/lib/modules/", RELEASE_VER, "modules.builtin")
with open(builtin_path) as builtins:
for line in builtins:
if line.rstrip().endswith(module_file):
@ -271,9 +265,9 @@ class Modprobe:
return is_loaded
def unload_module(self):
command = [self.modprobe_bin, '-r', self.name]
command = [self.modprobe_bin, "-r", self.name]
if self.check_mode:
command.append('-n')
command.append("-n")
rc, out, err = self.module.run_command(command)
if rc != 0:
@ -284,20 +278,20 @@ class Modprobe:
@property
def result(self):
return {
'changed': self.changed,
'name': self.name,
'params': self.params,
'state': self.desired_state,
"changed": self.changed,
"name": self.name,
"params": self.params,
"state": self.desired_state,
}
def build_module():
return AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
state=dict(type='str', default='present', choices=['absent', 'present']),
params=dict(type='str', default=''),
persistent=dict(type='str', default='disabled', choices=['disabled', 'present', 'absent']),
name=dict(type="str", required=True),
state=dict(type="str", default="present", choices=["absent", "present"]),
params=dict(type="str", default=""),
persistent=dict(type="str", default="disabled", choices=["disabled", "present", "absent"]),
),
supports_check_mode=True,
)
@ -308,18 +302,18 @@ def main():
modprobe = Modprobe(module)
if modprobe.desired_state == 'present' and not modprobe.module_loaded():
if modprobe.desired_state == "present" and not modprobe.module_loaded():
modprobe.load_module()
elif modprobe.desired_state == 'absent' and modprobe.module_loaded():
elif modprobe.desired_state == "absent" and modprobe.module_loaded():
modprobe.unload_module()
if modprobe.persistent == 'present' and not (modprobe.module_is_loaded_persistently and modprobe.params_is_set):
if modprobe.persistent == "present" and not (modprobe.module_is_loaded_persistently and modprobe.params_is_set):
modprobe.load_module_permanent()
elif modprobe.persistent == 'absent' and (modprobe.module_is_loaded_persistently or modprobe.permanent_params):
elif modprobe.persistent == "absent" and (modprobe.module_is_loaded_persistently or modprobe.permanent_params):
modprobe.unload_module_permanent()
module.exit_json(**modprobe.result)
if __name__ == '__main__':
if __name__ == "__main__":
main()