1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

seport: minor refactor (#4471) (#4485)

* seport: minor refactor

* added changelog fragment

* Update plugins/modules/system/seport.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/system/seport.py

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 7e6a2453d0)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2022-04-10 18:48:44 +02:00 committed by GitHub
parent 2c106d66a4
commit 34682addb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- seport - minor refactoring (https://github.com/ansible-collections/community.general/pull/4471).

View file

@ -113,7 +113,7 @@ from ansible.module_utils.common.text.converters import to_native
def get_runtime_status(ignore_selinux_state=False):
return True if ignore_selinux_state is True else selinux.is_selinux_enabled()
return ignore_selinux_state or selinux.is_selinux_enabled()
def semanage_port_get_ports(seport, setype, proto):
@ -161,10 +161,7 @@ def semanage_port_get_type(seport, port, proto):
key = (int(ports[0]), int(ports[1]), proto)
records = seport.get_all()
if key in records:
return records[key]
else:
return None
return records.get(key)
def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', sestore=''):
@ -194,18 +191,22 @@ def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', ses
:rtype: bool
:return: True if the policy was changed, otherwise False
"""
change = False
try:
seport = seobject.portRecords(sestore)
seport.set_reload(do_reload)
change = False
ports_by_type = semanage_port_get_ports(seport, setype, proto)
for port in ports:
if port not in ports_by_type:
if port in ports_by_type:
continue
change = True
if module.check_mode:
continue
port_type = semanage_port_get_type(seport, port, proto)
if port_type is None and not module.check_mode:
if port_type is None:
seport.add(port, proto, serange, setype)
elif port_type is not None and not module.check_mode:
else:
seport.modify(port, proto, serange, setype)
except (ValueError, IOError, KeyError, OSError, RuntimeError) as e:
@ -238,10 +239,10 @@ def semanage_port_del(module, ports, proto, setype, do_reload, sestore=''):
:rtype: bool
:return: True if the policy was changed, otherwise False
"""
change = False
try:
seport = seobject.portRecords(sestore)
seport.set_reload(do_reload)
change = False
ports_by_type = semanage_port_get_ports(seport, setype, proto)
for port in ports:
if port in ports_by_type: