Skip to content

Instantly share code, notes, and snippets.

@robbmanes
Last active February 18, 2024 19:10
Show Gist options
  • Save robbmanes/757088092a1fd621744fb7198edaeaaf to your computer and use it in GitHub Desktop.
Save robbmanes/757088092a1fd621744fb7198edaeaaf to your computer and use it in GitHub Desktop.
systemd user test within Ansible playbook
- name: Verify if a user can run `systemctl` commands as a non-root user
hosts: localhost
vars:
rootless_user: "rmanes"
rootless_uid: "1000"
become_method: machinectl
tasks:
- name: Check if DBUS socket exists for session user
stat:
path: "/run/user/{{ rootless_uid }}/bus"
register: dbus_bus
become_user: "{{ rootless_user }}"
- name: Report if dbus bus exists for session user
debug:
msg: "There is no dbus socket present for the user!"
when: not dbus_bus.stat.exists
become_user: "{{ rootless_user }}"
- name: Run a user-based systemctl command to test connection to DBUS
ansible.builtin.systemd:
name: init.scope
state: started
daemon_reload: true
scope: "user"
become_user: "{{ rootless_user }}"
environment:
XDG_RUNTIME_DIR: "/run/user/{{ rootless_uid }}"
DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/{{ rootless_uid }}/bus"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment