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

@@ -163,32 +163,31 @@ from ansible.module_utils.common.text.converters import to_native
class Sudoers:
FILE_MODE = 0o440
def __init__(self, module):
self.module = module
self.check_mode = module.check_mode
self.name = module.params['name']
self.user = module.params['user']
self.group = module.params['group']
self.state = module.params['state']
self.noexec = module.params['noexec']
self.nopassword = module.params['nopassword']
self.setenv = module.params['setenv']
self.host = module.params['host']
self.runas = module.params['runas']
self.sudoers_path = module.params['sudoers_path']
self.name = module.params["name"]
self.user = module.params["user"]
self.group = module.params["group"]
self.state = module.params["state"]
self.noexec = module.params["noexec"]
self.nopassword = module.params["nopassword"]
self.setenv = module.params["setenv"]
self.host = module.params["host"]
self.runas = module.params["runas"]
self.sudoers_path = module.params["sudoers_path"]
self.file = os.path.join(self.sudoers_path, self.name)
self.commands = module.params['commands']
self.validation = module.params['validation']
self.commands = module.params["commands"]
self.validation = module.params["validation"]
def write(self):
if self.check_mode:
return
with open(self.file, 'w') as f:
with open(self.file, "w") as f:
f.write(self.content())
os.chmod(self.file, self.FILE_MODE)
@@ -203,7 +202,7 @@ class Sudoers:
return os.path.exists(self.file)
def matches(self):
with open(self.file, 'r') as f:
with open(self.file, "r") as f:
content_matches = f.read() == self.content()
current_mode = os.stat(self.file).st_mode & 0o777
@@ -215,31 +214,33 @@ class Sudoers:
if self.user:
owner = self.user
elif self.group:
owner = f'%{self.group}'
owner = f"%{self.group}"
commands_str = ', '.join(self.commands)
noexec_str = 'NOEXEC:' if self.noexec else ''
nopasswd_str = 'NOPASSWD:' if self.nopassword else ''
setenv_str = 'SETENV:' if self.setenv else ''
runas_str = f'({self.runas})' if self.runas is not None else ''
commands_str = ", ".join(self.commands)
noexec_str = "NOEXEC:" if self.noexec else ""
nopasswd_str = "NOPASSWD:" if self.nopassword else ""
setenv_str = "SETENV:" if self.setenv else ""
runas_str = f"({self.runas})" if self.runas is not None else ""
return f"{owner} {self.host}={runas_str}{noexec_str}{nopasswd_str}{setenv_str} {commands_str}\n"
def validate(self):
if self.validation == 'absent':
if self.validation == "absent":
return
visudo_path = self.module.get_bin_path('visudo', required=self.validation == 'required')
visudo_path = self.module.get_bin_path("visudo", required=self.validation == "required")
if visudo_path is None:
return
check_command = [visudo_path, '-c', '-f', '-']
check_command = [visudo_path, "-c", "-f", "-"]
rc, stdout, stderr = self.module.run_command(check_command, data=self.content())
if rc != 0:
self.module.fail_json(msg=f'Failed to validate sudoers rule:\n{stdout or stderr}', stdout=stdout, stderr=stderr)
self.module.fail_json(
msg=f"Failed to validate sudoers rule:\n{stdout or stderr}", stdout=stdout, stderr=stderr
)
def run(self):
if self.state == 'absent':
if self.state == "absent":
if self.exists():
self.delete()
return True
@@ -257,54 +258,51 @@ class Sudoers:
def main():
argument_spec = {
'commands': {
'type': 'list',
'elements': 'str',
"commands": {
"type": "list",
"elements": "str",
},
'group': {},
'name': {
'required': True,
"group": {},
"name": {
"required": True,
},
'noexec': {
'type': 'bool',
'default': False,
"noexec": {
"type": "bool",
"default": False,
},
'nopassword': {
'type': 'bool',
'default': True,
"nopassword": {
"type": "bool",
"default": True,
},
'setenv': {
'type': 'bool',
'default': False,
"setenv": {
"type": "bool",
"default": False,
},
'host': {
'type': 'str',
'default': 'ALL',
"host": {
"type": "str",
"default": "ALL",
},
'runas': {
'type': 'str',
'default': None,
"runas": {
"type": "str",
"default": None,
},
'sudoers_path': {
'type': 'str',
'default': '/etc/sudoers.d',
"sudoers_path": {
"type": "str",
"default": "/etc/sudoers.d",
},
'state': {
'default': 'present',
'choices': ['present', 'absent'],
},
'user': {},
'validation': {
'default': 'detect',
'choices': ['absent', 'detect', 'required']
"state": {
"default": "present",
"choices": ["present", "absent"],
},
"user": {},
"validation": {"default": "detect", "choices": ["absent", "detect", "required"]},
}
module = AnsibleModule(
argument_spec=argument_spec,
mutually_exclusive=[['user', 'group']],
mutually_exclusive=[["user", "group"]],
supports_check_mode=True,
required_if=[('state', 'present', ['commands'])],
required_if=[("state", "present", ["commands"])],
)
sudoers = Sudoers(module)
@@ -316,5 +314,5 @@ def main():
module.fail_json(msg=to_native(e))
if __name__ == '__main__':
if __name__ == "__main__":
main()