From 016bc83b5863223eba42b97f5477cc8d0f95c1e2 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 16 Jan 2019 12:36:41 -0500 Subject: [PATCH] ensure we have required systemd env var (#50607) * ensure we have required systemd env var * add clarification about systemd and user scope fixes #50272 (cherry picked from commit a7e81ba5005b8ea3136bcd89e6f8a49fcb93c60e) --- changelogs/fragments/xdg_systemd_fix.yml | 2 ++ lib/ansible/modules/system/systemd.py | 8 ++++++++ 2 files changed, 10 insertions(+) create mode 100644 changelogs/fragments/xdg_systemd_fix.yml diff --git a/changelogs/fragments/xdg_systemd_fix.yml b/changelogs/fragments/xdg_systemd_fix.yml new file mode 100644 index 00000000000..ca8ebc17653 --- /dev/null +++ b/changelogs/fragments/xdg_systemd_fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - ensure we have a XDG_RUNTIME_DIR, as it is not handled correctly by some privilege escalation configurations diff --git a/lib/ansible/modules/system/systemd.py b/lib/ansible/modules/system/systemd.py index 04efacf0dfb..c8edf36a15b 100644 --- a/lib/ansible/modules/system/systemd.py +++ b/lib/ansible/modules/system/systemd.py @@ -59,6 +59,9 @@ options: description: - run systemctl within a given service manager scope, either as the default system scope (system), the current user's scope (user), or the scope of all users (global). + - "For systemd to work with 'user', the executing user must have its own instance of dbus started (systemd requirement). + The user dbus process is normally started during normal login, but not during the run of Ansible tasks. + Otherwise you will probably get a 'Failed to connect to bus: no such file or directory' error." choices: [ system, user, global ] default: 'system' version_added: "2.7" @@ -245,6 +248,8 @@ status: } ''' # NOQA +import os + from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.service import sysv_exists, sysv_is_enabled, fail_if_missing from ansible.module_utils._text import to_native @@ -317,6 +322,9 @@ def main(): systemctl = module.get_bin_path('systemctl', True) + if os.getenv('XDG_RUNTIME_DIR') is None: + os.environ['XDG_RUNTIME_DIR'] = '/run/user/%s' % os.geteuid() + ''' Set CLI options depending on params ''' if module.params['user'] is not None: # handle user deprecation, mutually exclusive with scope