diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index 59f9e33656a..8dc17f76b88 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -913,7 +913,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: