From 5f22b4f8addf43e65d642e7195604cfa000615a4 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Thu, 28 Sep 2017 09:02:26 -0700 Subject: [PATCH] Reduce chance of mistakes with unsafe_shell check during refactor Code like this: if cond1 and cond2: pass elif cond1: pass Has a hidden dependency on the order that the conditions are checked. This makes them fragile and subject to breakage during refactors. Rewrite the code like this: if cond1: if cond2: pass else: pass The nested structure makes the ordering explicit and less likely for someone to break the code when they refactor. --- lib/ansible/module_utils/basic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index d4e4beaa38..8124eb9a70 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -2606,10 +2606,10 @@ class AnsibleModule(object): if use_unsafe_shell: args = " ".join([shlex_quote(x) for x in args]) shell = True - elif isinstance(args, (binary_type, text_type)) and use_unsafe_shell: - shell = True elif isinstance(args, (binary_type, text_type)): - if not use_unsafe_shell: + if use_unsafe_shell: + shell = True + else: # On python2.6 and below, shlex has problems with text type # On python3, shlex needs a text type. if PY2: