Skip to content

refactor: use module.run_command instead of subprocess#856

Merged
richm merged 1 commit intolinux-system-roles:mainfrom
richm:refactor-use-module-run_command
Apr 2, 2026
Merged

refactor: use module.run_command instead of subprocess#856
richm merged 1 commit intolinux-system-roles:mainfrom
richm:refactor-use-module-run_command

Conversation

@richm
Copy link
Copy Markdown
Contributor

@richm richm commented Apr 2, 2026

Ansible modules should use module.run_command instead of subprocess.
This is flagged by the latest ansible-test

Signed-off-by: Rich Megginson rmeggins@redhat.com

Summary by Sourcery

Enhancements:

  • Replace direct subprocess usage with module.run_command when issuing the busctl call in NetworkManager connection cleanup logic.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 2, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the NetworkManager connection cleanup helper to use Ansible's module.run_command API instead of invoking busctl via subprocess, aligning with ansible-test guidance and enabling better command handling within the module.

Sequence diagram for forget_nm_connection using module_run_command

sequenceDiagram
    actor AnsibleController
    participant NetworkConnectionsModule
    participant AnsibleModule
    participant BusctlProcess

    AnsibleController->>NetworkConnectionsModule: forget_nm_connection(path)
    activate NetworkConnectionsModule
    NetworkConnectionsModule->>AnsibleModule: run_command([busctl, --system, call, org.freedesktop.NetworkManager, /org/freedesktop/NetworkManager, org.freedesktop.NetworkManager, ActivateConnection, as, 1, path], handle_exceptions=False)
    activate AnsibleModule
    AnsibleModule->>BusctlProcess: exec busctl ... path
    activate BusctlProcess
    BusctlProcess-->>AnsibleModule: rc, stdout, stderr
    deactivate BusctlProcess
    AnsibleModule-->>NetworkConnectionsModule: rc, stdout, stderr
    deactivate AnsibleModule
    NetworkConnectionsModule-->>AnsibleController: return (ignores exceptions)
    deactivate NetworkConnectionsModule
Loading

Class diagram for network_connections forget_nm_connection refactor

classDiagram
    class NetworkConnectionsModule {
        +AnsibleModule module
        +forget_nm_connection(path)
    }

    class AnsibleModule {
        +run_command(cmd_list, use_unsafe_shell, encoding, check_rc, close_fds, environ, executable, data, binary_data, cwd, umask, strict, diff, expands, prompt_regex, stdin_add_newline, preserve_rc, path_prefix, host, port, display, timeout, become, become_method, become_user, become_pass, ignore_invalid_certs, suppress_system_warnings, python_interpreter, handle_exceptions)
    }

    NetworkConnectionsModule --> AnsibleModule: uses run_command for busctl call
Loading

File-Level Changes

Change Details Files
Replace direct subprocess invocation with Ansible module.run_command for the busctl call used when forgetting a NetworkManager connection.
  • Remove subprocess.call usage to execute the busctl command in forget_nm_connection.
  • Use self.run_env.module.run_command to execute the same busctl arguments and capture rc, stdout, and stderr.
  • Pass handle_exceptions=False so that run_command surfaces errors instead of converting them to module.fail_json automatically.
  • Retain the outer try/except Exception: pass to silently ignore any failures from the command.
library/network_connections.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The assigned variables rc, stdout, stderr are never used; if you truly don't need them, either drop the assignment or use _/_rc to make the intent to ignore them explicit.
  • Since handle_exceptions=False is now passed to run_command, consider whether the bare except Exception: pass is still appropriate or if you should narrow the exception type or log failures before ignoring them.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The assigned variables `rc, stdout, stderr` are never used; if you truly don't need them, either drop the assignment or use `_`/`_rc` to make the intent to ignore them explicit.
- Since `handle_exceptions=False` is now passed to `run_command`, consider whether the bare `except Exception: pass` is still appropriate or if you should narrow the exception type or log failures before ignoring them.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread library/network_connections.py Fixed
Comment thread library/network_connections.py Fixed
Comment thread library/network_connections.py Fixed
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 2, 2026

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 43.16%. Comparing base (1b57520) to head (7d67e2c).
⚠️ Report is 83 commits behind head on main.

Files with missing lines Patch % Lines
library/network_connections.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #856      +/-   ##
==========================================
+ Coverage   43.11%   43.16%   +0.05%     
==========================================
  Files          12       12              
  Lines        3124     3125       +1     
==========================================
+ Hits         1347     1349       +2     
+ Misses       1777     1776       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Apr 2, 2026

[citest]

Ansible modules should use module.run_command instead of subprocess.
This is flagged by the latest ansible-test

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
@richm richm force-pushed the refactor-use-module-run_command branch from 71b24b1 to 7d67e2c Compare April 2, 2026 15:42
@richm
Copy link
Copy Markdown
Contributor Author

richm commented Apr 2, 2026

Hey - I've left some high level feedback:

* The assigned variables `rc, stdout, stderr` are never used; if you truly don't need them, either drop the assignment or use `_`/`_rc` to make the intent to ignore them explicit.

done

Comment thread library/network_connections.py Dismissed
Comment thread library/network_connections.py Dismissed
Comment thread library/network_connections.py Dismissed
@richm
Copy link
Copy Markdown
Contributor Author

richm commented Apr 2, 2026

[citest]

@richm richm merged commit d56563d into linux-system-roles:main Apr 2, 2026
37 of 41 checks passed
@richm richm deleted the refactor-use-module-run_command branch April 2, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants