Skip to content

sysrc: refactor #10417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

dlundgren
Copy link
Contributor

@dlundgren dlundgren commented Jul 14, 2025

SUMMARY

Refactors the sysrc module based on comments from prior PR's. Also fixes #10394.

Closes #10400.

ISSUE TYPE
  • Refactoring Pull Request
COMPONENT NAME

sysrc

ADDITIONAL INFORMATION

Refactored the sysrc module.

@ansibullbot ansibullbot added integration tests/integration module module plugins plugin (any type) tests tests labels Jul 14, 2025
@dlundgren
Copy link
Contributor Author

I had been sitting on the actual code for a month, and just finished up with the tests this morning. I wanted to get this off my plate before I forgot about it again.

Sorry for a PR that competes with another one.

@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Jul 14, 2025
@dlundgren
Copy link
Contributor Author

dlundgren commented Jul 14, 2025

What are the differences between FreeBSD 14.2 - 1, FreeBSD 14.2 - 2 and FreeBSD 14.2 - 3?

Never mind, this was my fault on the test for not updating the correct file.

@dlundgren
Copy link
Contributor Author

@vbotka Would you mind checking my logic on the way load_sysrc_value works?

I'm fairly certain that using -c is the correct answer here so that we don't have to parse the variables. Since we are using -n self.name to get the values when appending/subtracting, I figured it would work better to consolidate the value loading to one place.

@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Jul 14, 2025
@dlundgren dlundgren force-pushed the dl/sysrc-refactor branch from a9435ae to 880a640 Compare July 15, 2025 03:33
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-11 Automatically create a backport for the stable-10 branch labels Jul 15, 2025
@vbotka
Copy link
Contributor

vbotka commented Jul 15, 2025

using -c is the correct answer

Yes, it is. load_sysrc_value works fine. This is better than config parsing.

When it is that far, it would be good to understand how sysrc updates rc.conf/rc.conf.d. I'm not sure -f works in all corner cases. I'll test it.

@felixfontein
Copy link
Collaborator

One potential problem is that this is a larger refactoring, which needs a very good reason to be backported to stable-10 as well (by default we only backport new features and refactorings to the current stable branch, which is currently stable-11). It is not clear to me how large this refactoring is, and whether it is safe enough to backport to stable-10 as well. Or should we apply #10400 to stable-10 and this PR to main and stable-11?

@felixfontein
Copy link
Collaborator

(Also please add a changelog fragment :-) )

@vbotka
Copy link
Contributor

vbotka commented Jul 15, 2025

whether it is safe enough

IMO, it is as safe as before.

The error handling is missing (as before). For example, insufficient permissions are silently ignored in run_sysrc

-> return (rc, out, err)
(Pdb) p rc
1
(Pdb) p out
''
(Pdb) p err
'/usr/sbin/sysrc: cannot create /etc/rc.conf: Permission denied\n'

As a result, the task is reported changed, but the file has no changes.

I would like to proceed on top of this refactoring.

@vbotka
Copy link
Contributor

vbotka commented Jul 15, 2025

I'm not sure -f works in all corner cases. I'll test it.

IMO, there are no problems. This PR should be merged as is. The error handling may be added later.

@dlundgren
Copy link
Contributor Author

dlundgren commented Jul 15, 2025

The error handling is missing (as before). For example, insufficient permissions are silently ignored in run_sysrc

'/usr/sbin/sysrc: cannot create /etc/rc.conf: Permission denied\n'

As a result, the task is reported changed, but the file has no changes.

Hopefully the permission check I added works to prevent this.

It did locally, but I have molecule using a vagrant and so I can easily create a test that has become: false. Within the Azure pipeline it connects as root, so I can't easily write a test for checking permissions.

@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Jul 15, 2025
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Jul 15, 2025
@vbotka
Copy link
Contributor

vbotka commented Jul 16, 2025

add tests for the use case this PR fixes

There is no reason to test the parsing. After refactoring, the module uses only "sysrc" to parse the configuration files. The sysrc output is not transformed in the module; hence, testing parsing would mean testing the sysrc binary, not the module.

@vbotka
Copy link
Contributor

vbotka commented Jul 16, 2025

I want to propose this patch

@@ -152,7 +152,11 @@ class Sysrc(object):
             return changed(out.split(' -> ')[1].strip().split(self.delim))

     def present(self):
-        if self.load_sysrc_value() == self.value:
+        value = self.load_sysrc_value()
+        if value == self.value:
+            return False
+        if self.value is None:
+            self.value = value
             return False

         if not self.module.check_mode:
@@ -232,6 +236,7 @@ def main():
     try:
         sysrc = Sysrc(module, name, result['value'], result['path'], result['delim'], result['jail'])
         result['changed'] = getattr(sysrc, result['state'])()
+        result['value'] = getattr(sysrc, 'value')
     except OSError as err:
         module.fail_json(msg=str(err))

The option value is not required. If value is not used, this patch makes the module return the current value if state: present(default). This is how the sysrc binary behaves. For example,

shell> sysrc k1
k1: v1

Without this patch, the module replaces the current value, e.g. k1="None", if name is present.

@felixfontein
Copy link
Collaborator

Instead of getattr(sysrc, 'value') please use sysrc.value :)

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dlundgren thanks for the contribution!

Couple of comments on it.

@ansibullbot ansibullbot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Jul 18, 2025
@ansibullbot ansibullbot removed the needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI label Jul 18, 2025
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Jul 18, 2025
@vbotka
Copy link
Contributor

vbotka commented Jul 21, 2025

Without the name validity test, the error name contains characters not allowed in shell is silently ignored. As a result, the module reports changed, but the file has no changes.

felixfontein added a commit to vbotka/community.general that referenced this pull request Jul 21, 2025
felixfontein added a commit to vbotka/community.general that referenced this pull request Jul 21, 2025
felixfontein added a commit to vbotka/community.general that referenced this pull request Jul 21, 2025
@dlundgren
Copy link
Contributor Author

@vbotka thanks for finding that! I added the check back in and a test for it as well.

felixfontein added a commit that referenced this pull request Jul 21, 2025
* Fix #10394 Use configparser.

* Fix #10394 configparser is in Python3 only.

* Fix #10394 return condition.

* Fix #10394 Use ConfigParser with Python2.

* Fix #10394 import configparser from ansible.module_utils.six.moves

* Add changelog fragment.

* Update changelogs/fragments/10400-sysrc.yml

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

* Fix #10394 use shlex instead of configparser.

* Update fragment.

* Update changelogs/fragments/10400-sysrc.yml

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

* Update plugins/modules/sysrc.py

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

* Apply suggestions from code review.

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Copy tests from #10417.

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
patchback bot pushed a commit that referenced this pull request Jul 21, 2025
* Fix #10394 Use configparser.

* Fix #10394 configparser is in Python3 only.

* Fix #10394 return condition.

* Fix #10394 Use ConfigParser with Python2.

* Fix #10394 import configparser from ansible.module_utils.six.moves

* Add changelog fragment.

* Update changelogs/fragments/10400-sysrc.yml

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

* Fix #10394 use shlex instead of configparser.

* Update fragment.

* Update changelogs/fragments/10400-sysrc.yml

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

* Update plugins/modules/sysrc.py

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

* Apply suggestions from code review.

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Copy tests from #10417.

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
(cherry picked from commit 0be6e61)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-11 Automatically create a backport for the stable-10 branch check-before-release PR will be looked at again shortly before release and merged if possible. integration tests/integration module module plugins plugin (any type) tests tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

community.general.sysrc fails to parse "sysrc -a" output
5 participants