From 83318c36aaf3f0e88362ac9d8a8d5ba7a4bfead2 Mon Sep 17 00:00:00 2001 From: leko Date: Sun, 14 Jul 2024 20:00:00 +0800 Subject: [PATCH] fix: sudosu not working on some BSD machines (#8214) * fix: sudosu not working on some BSD machines * fix: sudosu: added a flag (`alt_method`) to enhance compatibility with more versions of `su` * Update changelogs/fragments/8214-sudosu-not-working-on-some-BSD-machines.yml Co-authored-by: Felix Fontein * Update plugins/become/sudosu.py Co-authored-by: Felix Fontein * Update plugins/become/sudosu.py Co-authored-by: Felix Fontein * Update plugins/become/sudosu.py Co-authored-by: Felix Fontein * Update plugins/become/sudosu.py Co-authored-by: Felix Fontein * fix: sudosu: lint --------- Co-authored-by: Felix Fontein --- ...udosu-not-working-on-some-BSD-machines.yml | 2 ++ plugins/become/sudosu.py | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/8214-sudosu-not-working-on-some-BSD-machines.yml diff --git a/changelogs/fragments/8214-sudosu-not-working-on-some-BSD-machines.yml b/changelogs/fragments/8214-sudosu-not-working-on-some-BSD-machines.yml new file mode 100644 index 0000000000..411ba8e868 --- /dev/null +++ b/changelogs/fragments/8214-sudosu-not-working-on-some-BSD-machines.yml @@ -0,0 +1,2 @@ +minor_changes: + - sudosu become plugin - added an option (``alt_method``) to enhance compatibility with more versions of ``su`` (https://github.com/ansible-collections/community.general/pull/8214). diff --git a/plugins/become/sudosu.py b/plugins/become/sudosu.py index 60bb2aa517..2b009db4b7 100644 --- a/plugins/become/sudosu.py +++ b/plugins/become/sudosu.py @@ -55,6 +55,21 @@ DOCUMENTATION = """ ini: - section: sudo_become_plugin key: password + alt_method: + description: + - Whether to use an alternative method to call C(su). Instead of running C(su -l user /path/to/shell -c command), + it runs C(su -l user -c command). + - Use this when the default one is not working on your system. + required: false + type: boolean + ini: + - section: community.general.sudosu + key: alternative_method + vars: + - name: ansible_sudosu_alt_method + env: + - name: ANSIBLE_SUDOSU_ALT_METHOD + version_added: 9.2.0 """ @@ -89,4 +104,7 @@ class BecomeModule(BecomeBase): if user: user = '%s' % (user) - return ' '.join([becomecmd, flags, prompt, 'su -l', user, self._build_success_command(cmd, shell)]) + if self.get_option('alt_method'): + return ' '.join([becomecmd, flags, prompt, "su -l", user, "-c", self._build_success_command(cmd, shell, True)]) + else: + return ' '.join([becomecmd, flags, prompt, 'su -l', user, self._build_success_command(cmd, shell)])