Fix unhandled errors in command module

This commit is contained in:
Brian Coca 2019-06-26 13:37:21 -04:00 committed by Matt Clay
parent 911a2ec6d3
commit 1a9b1d0edd
2 changed files with 12 additions and 3 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- make command module more resilient unicode errors. Also to fs errors.

View file

@ -166,7 +166,7 @@ import os
import shlex import shlex
from ansible.module_utils.basic import AnsibleModule 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 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] args = [to_native(arg, errors='surrogate_or_strict', nonstring='simplerepr') for arg in args]
if chdir: if chdir:
chdir = os.path.abspath(chdir) try:
os.chdir(chdir) 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: if creates:
# do not run the command if the line contains creates=filename # do not run the command if the line contains creates=filename