diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index 2efee392c54..c225b2d425e 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -895,7 +895,8 @@ class TaskExecutor: else: try: result = json.loads(to_text(stderr, errors='surrogate_then_replace')) - except json.decoder.JSONDecodeError: + except getattr(json.decoder, 'JSONDecodeError', ValueError): + # JSONDecodeError only available on Python 3.5+ result = {'error': to_text(stderr, errors='surrogate_then_replace')} if 'messages' in result: diff --git a/lib/ansible/plugins/connection/persistent.py b/lib/ansible/plugins/connection/persistent.py index 1e377a75dd0..ac2406f4c1a 100644 --- a/lib/ansible/plugins/connection/persistent.py +++ b/lib/ansible/plugins/connection/persistent.py @@ -115,7 +115,8 @@ class Connection(ConnectionBase): else: try: result = json.loads(to_text(stderr, errors='surrogate_then_replace')) - except json.decoder.JSONDecodeError: + except getattr(json.decoder, 'JSONDecodeError', ValueError): + # JSONDecodeError only available on Python 3.5+ result = {'error': to_text(stderr, errors='surrogate_then_replace')} if 'messages' in result: