From e7fe95af886772a9f826957461d008a93c93a1f0 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 27 Jan 2019 04:44:24 +0000 Subject: [PATCH] issue #477: fix sudo_args selection. --- ansible_mitogen/transport_config.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index 8ef49ebb..92569c57 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -89,6 +89,16 @@ def optional_secret(value): return mitogen.core.Secret(value) +def first_true(it, default=None): + """ + Return the first truthy element from `it`. + """ + for elem in it: + if elem: + return elem + return default + + class Spec(with_metaclass(abc.ABCMeta, object)): """ A source for variables that comprise a connection configuration. @@ -350,15 +360,15 @@ class PlayContextSpec(Spec): def sudo_args(self): return [ mitogen.core.to_text(term) - for s in ( - self._play_context.sudo_flags, - self._play_context.become_flags, - # Ansible 2.3. - getattr(C, 'DEFAULT_BECOME_FLAGS', ''), - getattr(C, 'DEFAULT_SUDO_FLAGS', ''), - + for term in ansible.utils.shlex.shlex_split( + first_true(( + self._play_context.become_flags, + self._play_context.sudo_flags, + # Ansible 2.3. + getattr(C, 'DEFAULT_BECOME_FLAGS', ''), + getattr(C, 'DEFAULT_SUDO_FLAGS', '') + ), default='') ) - for term in ansible.utils.shlex.shlex_split(s or '') ] def mitogen_via(self):