diff --git a/changelogs/fragments/command_uni_fixes.yml b/changelogs/fragments/command_uni_fixes.yml new file mode 100644 index 00000000000..6c63d1ffe98 --- /dev/null +++ b/changelogs/fragments/command_uni_fixes.yml @@ -0,0 +1,2 @@ +bugfixes: + - make command module more resilient unicode errors. Also to fs errors. diff --git a/lib/ansible/modules/commands/command.py b/lib/ansible/modules/commands/command.py index 66ba77017e9..d561855c2ac 100644 --- a/lib/ansible/modules/commands/command.py +++ b/lib/ansible/modules/commands/command.py @@ -166,7 +166,7 @@ import os import shlex from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils._text import to_native +from ansible.module_utils._text import to_native, to_bytes, to_text from ansible.module_utils.common.collections import is_iterable @@ -259,8 +259,15 @@ def main(): args = [to_native(arg, errors='surrogate_or_strict', nonstring='simplerepr') for arg in args] if chdir: - chdir = os.path.abspath(chdir) - os.chdir(chdir) + try: + chdir = to_bytes(os.path.abspath(chdir), errors='surrogate_or_strict') + except ValueError as e: + module.jail_json('Unable to use supplied chdir: %s' % to_text(e)) + + try: + os.chdir(chdir) + except (IOError, OSError) as e: + module.fail_json('Unable to change directory before execution: %s' % to_text(e)) if creates: # do not run the command if the line contains creates=filename