Skip to content

systemd_service - Add the machine parameter (#82958) - #82959

Open
jimmy-lt wants to merge 1 commit into
ansible:develfrom
jimmy-lt:feature/#82958-systemd_service-machine-parameter
Open

systemd_service - Add the machine parameter (#82958)#82959
jimmy-lt wants to merge 1 commit into
ansible:develfrom
jimmy-lt:feature/#82958-systemd_service-machine-parameter

Conversation

@jimmy-lt

@jimmy-lt jimmy-lt commented Apr 2, 2024

Copy link
Copy Markdown

Fixes #82958

systemctl's --machine option execute the operation in the context of a local user session or a local container.

This provides a failsafe way to run systemd on behalf of another user than setting the XDG_RUNTIME_DIR variable.

SUMMARY

When trying to run systemd as another user, the following is advised:

- name: Run a user service when XDG_RUNTIME_DIR is not set on remote login
  ansible.builtin.systemd_service:
    name: myservice
    state: started
    scope: user
  environment:
    XDG_RUNTIME_DIR: "/run/user/{{ myuid }}"

However, this may not always work and can fail in various ways, typically:

Failed to connect to bus: Operation not permitted

Since systemd 248, the --machine option is available to to open a connection to the session bus of a specific user.

ISSUE TYPE
  • Feature Pull Request
ADDITIONAL INFORMATION
- name: Run from within a specific user session
  ansible.builtin.systemd_service:
    name: myservice
    state: started
    scope: user
    machine: myuser@.host
"invocation": {
    "module_args": {
        "daemon_reexec": false,
        "daemon_reload": false,
        "enabled": true,
        "force": null,
        "machine": "myuser@.host",
        "masked": null,
        "name": "myservice",
        "no_block": false,
        "scope": "user",
        "state": "started"
    }
}

@ansibot ansibot added feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. module This issue/PR relates to a module. has_issue ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Apr 2, 2024
@ansibot

ansibot commented Apr 2, 2024

Copy link
Copy Markdown
Contributor

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

lib/ansible/modules/systemd_service.py:88:161: E501: line too long (177 > 160 characters)

click here for bot help

@jimmy-lt
jimmy-lt force-pushed the feature/#82958-systemd_service-machine-parameter branch from 8ceea6f to ec6688e Compare April 2, 2024 20:55
@ansibot ansibot removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. ci_verified Changes made in this PR are causing tests to fail. labels Apr 2, 2024
@nitzmahone nitzmahone removed the needs_triage Needs a first human triage before being processed. label Apr 4, 2024
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 11, 2024

@bcoca bcoca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would ask for tests but this seems to be very involved in setting up, I would at least add more verification to the parameter passed.

Comment thread lib/ansible/modules/systemd_service.py Outdated
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Oct 4, 2024
@ansibot ansibot added the stale_pr This PR has not been pushed to for more than one year. label Apr 7, 2025
@chschenk

chschenk commented May 6, 2026

Copy link
Copy Markdown

Hi,
I really would love to see this implemented in ansible. @jimmy-lt are you still working on this PR?
Is there anything I can do to move this PR forward?

@jimmy-lt
jimmy-lt force-pushed the feature/#82958-systemd_service-machine-parameter branch from ec6688e to f398017 Compare May 6, 2026 21:43
@ansibot ansibot added stale_review Updates were made after the last review and the last review is more than 7 days old. and removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html stale_pr This PR has not been pushed to for more than one year. labels May 6, 2026
@ansibot

ansibot commented May 6, 2026

Copy link
Copy Markdown
Contributor

@jimmy-lt All commits must have verified signatures.

Please see this Ansible Forum post for more information.

click here for bot help

@ansibot ansibot removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels May 6, 2026
Comment thread lib/ansible/modules/systemd_service.py
Comment thread lib/ansible/modules/systemd_service.py Outdated
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_review Updates were made after the last review and the last review is more than 7 days old. labels May 7, 2026
@jimmy-lt
jimmy-lt force-pushed the feature/#82958-systemd_service-machine-parameter branch from f398017 to 1da3284 Compare May 7, 2026 20:05
@ansibot ansibot added stale_review Updates were made after the last review and the last review is more than 7 days old. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels May 7, 2026
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label May 21, 2026
@jimmy-lt
jimmy-lt requested review from bcoca and pkingstonxyz May 23, 2026 16:33

@pkingstonxyz pkingstonxyz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me, but I'm torn on the issue of tests. I agree with @bcoca that this seems too convoluted to fully exercise in the integration test suite, but I don't think it's impossible to add something. The diff I pasted with the basic "current user+localhost" case would at least provide a smoke test if machine gets removed from a systemd version/is incompatible with the oldest supported target.

Something like v would do the trick without much overhead, I think.

@@ -112,11 +112,27 @@
     enabled: true
   register: systemd_enable_ssh_2
 
+- name: Disable ssh 2 with machine
+  systemd:
+    name: '{{ ssh_service }}'
+    enabled: false
+    machine: "@.host"
+  register: systemd_disable_ssh_2_machine
+
+- name: Enable ssh 2 with machine
+  systemd:
+    name: '{{ ssh_service }}'
+    enabled: true
+    machine: "@.host"
+  register: systemd_enable_ssh_2_machine
+
 - assert:
     that:
       - systemd_disable_ssh_2 is not changed
       - systemd_enable_ssh_1 is changed
       - systemd_enable_ssh_2 is not changed
+      - systemd_disable_ssh_2_machine is changed
+      - systemd_enable_ssh_2_machine is changed
 
 - import_tasks: test_unit_template.yml
 - import_tasks: test_indirect_service.yml

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_review Updates were made after the last review and the last review is more than 7 days old. labels May 26, 2026
@jimmy-lt
jimmy-lt force-pushed the feature/#82958-systemd_service-machine-parameter branch from 1da3284 to 9ce5902 Compare May 30, 2026 15:42
@ansibot ansibot added stale_review Updates were made after the last review and the last review is more than 7 days old. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels May 30, 2026
@jimmy-lt
jimmy-lt force-pushed the feature/#82958-systemd_service-machine-parameter branch 3 times, most recently from 7a1e193 to daf3322 Compare May 30, 2026 20:40
systemctl's `--machine` option execute the operation in the context of a local
user session or a local container.

This provides a failsafe way to run systemd on behalf of another user than
setting the `XDG_RUNTIME_DIR` variable.

Signed-off-by: Jimmy Thrasibule <dev@jimmy.lt>
@jimmy-lt
jimmy-lt force-pushed the feature/#82958-systemd_service-machine-parameter branch from daf3322 to e75af5d Compare May 30, 2026 21:53
@jimmy-lt

jimmy-lt commented May 31, 2026

Copy link
Copy Markdown
Author

@pkingstonxyz

I've added the tests but for them to pass, dbus-broker must be part of the test containers. As such, I created the pull request distro-test-containers/#137.

@webknjaz webknjaz added the ci_verified Changes made in this PR are causing tests to fail. label Jun 1, 2026
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci_verified Changes made in this PR are causing tests to fail. feature This issue/PR relates to a feature request. has_issue module This issue/PR relates to a module. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

systemd_service: Support for the "machine" parameter

7 participants