sysrc: add missing name format check

Also use `self.module.fail_json` through out
This commit is contained in:
David Lundgren 2025-07-21 12:13:51 -05:00
commit fa91ad3ece
No known key found for this signature in database
GPG key ID: 2B78584C498D2A5E
2 changed files with 19 additions and 3 deletions

View file

@ -105,8 +105,7 @@ changed:
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
import errno
import os
import re
class Sysrc(StateModuleHelper):
@ -125,6 +124,10 @@ class Sysrc(StateModuleHelper):
use_old_vardict = False
def __init_module__(self):
# OID style names are not supported
if not re.match('^[a-zA-Z0-9_]+$', self.vars.name):
self.module.fail_json(msg="Name may only contain alpha-numeric and underscore characters")
self.sysrc = self.module.get_bin_path('sysrc', True)
def _contains(self):
@ -165,7 +168,7 @@ class Sysrc(StateModuleHelper):
(rc, out, err) = self.module.run_command(cmd)
if "Permission denied" in err:
raise OSError(errno.EACCES, "Permission denied for %s" % self.vars.path)
self.module.fail_json(msg="Permission denied for %s" % self.vars.path)
return rc, out, err

View file

@ -422,6 +422,19 @@
- >
'k1="v2"' in sysrc_10394_content.stdout_lines
##
## sysrc - additional tests
##
- name: Ensure failure on OID style name since sysrc does not support them
sysrc:
name: not.valid.var
value: test
register: sysrc_name_check
failed_when:
- sysrc_name_check is not failed
- >
'Name may only contain alpha-numeric and underscore characters' != sysrc_name_check.msg
always:
- name: Restore /etc/rc.conf