@@ -373,7 +373,10 @@ def _execute_module(self, module_name=None, module_args=None, tmp=None,
373373 self ._connection .context = None
374374
375375 self ._connection ._connect ()
376- result = ansible_mitogen .planner .invoke (
376+
377+ # Ansible <= 13 (ansible-core <= 2.20): dict
378+ # Ansible >= 14 (ansible-core >= 2.21): UnifiedTaskResult
379+ task_result = ansible_mitogen .planner .invoke (
377380 ansible_mitogen .planner .Invocation (
378381 action = self ,
379382 connection = self ._connection ,
@@ -393,59 +396,56 @@ def _execute_module(self, module_name=None, module_args=None, tmp=None,
393396 self ._remove_tmp_path (tmp )
394397
395398 # prevents things like discovered_interpreter_* or ansible_discovered_interpreter_* from being set
396- ansible .vars .clean .remove_internal_keys (result )
399+ try :
400+ task_result .remove_internal_keys ()
401+ except AttributeError :
402+ ansible .vars .clean .remove_internal_keys (task_result )
397403
398404 # taken from _execute_module of ansible 2.8.6
399405 # propagate interpreter discovery results back to the controller
400406 if self ._discovered_interpreter_key :
401- if result .get ('ansible_facts' ) is None :
402- result ['ansible_facts' ] = {}
403-
404407 # only cache discovered_interpreter if we're not running a rediscovery
405408 # rediscovery happens in places like docker connections that could have different
406409 # python interpreters than the main host
407410 if not self ._mitogen_rediscovered_interpreter :
408- result ['ansible_facts' ][self ._discovered_interpreter_key ] = self ._discovered_interpreter
411+ di_key = self ._discovered_interpreter_key
412+ di_val = self ._discovered_interpreter
413+ try :
414+ task_result .set_fact (di_key , di_val )
415+ except AttributeError :
416+ task_result .setdefault ('ansible_facts' , {})[di_key ] = di_val
409417
410418 discovery_warnings = getattr (self , '_discovery_warnings' , [])
411419 if discovery_warnings :
412- if result .get ('warnings' ) is None :
413- result ['warnings' ] = []
414- result ['warnings' ].extend (discovery_warnings )
420+ try :
421+ task_result ._extend_warnings (discovery_warnings )
422+ except AttributeError :
423+ task_result .setdefault ('warnings' , []).extend (discovery_warnings )
415424
416425 discovery_deprecation_warnings = getattr (self , '_discovery_deprecation_warnings' , [])
417426 if discovery_deprecation_warnings :
418- if result .get ('deprecations' ) is None :
419- result ['deprecations' ] = []
420- result ['deprecations' ].extend (discovery_deprecation_warnings )
427+ try :
428+ task_result ._extend_deprecations (discovery_deprecation_warnings )
429+ except AttributeError :
430+ task_result .setdefault ('deprecations' , []).extend (discovery_deprecation_warnings )
421431
422- return ansible .utils .unsafe_proxy .wrap_var (result )
432+ if ansible_mitogen .utils .ansible_version [:2 ] >= (2 , 21 ):
433+ task_result = task_result .as_result_dict (for_round_trip = True )
434+ return ansible .utils .unsafe_proxy .wrap_var (task_result )
423435
424436 def _postprocess_response (self , result ):
425- """
426- Apply fixups mimicking ActionBase._execute_module(); this is copied
427- verbatim from action/__init__.py, the guts of _parse_returned_data are
428- garbage and should be removed or reimplemented once tests exist.
429-
430- :param dict result:
431- Dictionary with format::
432-
433- {
434- "rc": int,
435- "stdout": "stdout data",
436- "stderr": "stderr data"
437- }
438- """
439437 if ansible_mitogen .utils .ansible_version [:2 ] >= (2 , 19 ):
440438 data = self ._parse_returned_data (result , profile = 'legacy' )
441439 else :
442440 data = self ._parse_returned_data (result )
443441
444- # Cutpasted from the base implementation.
445- if 'stdout' in data and 'stdout_lines' not in data :
446- data ['stdout_lines' ] = (data ['stdout' ] or u'' ).splitlines ()
447- if 'stderr' in data and 'stderr_lines' not in data :
448- data ['stderr_lines' ] = (data ['stderr' ] or u'' ).splitlines ()
442+ # ansible-core >= 2.21: done in UnifiedTaskResult.as_result_dict()
443+ if ansible_mitogen .utils .ansible_version [:2 ] <= (2 , 20 ):
444+ # Cutpasted from the base implementation.
445+ if 'stdout' in data and 'stdout_lines' not in data :
446+ data ['stdout_lines' ] = (data ['stdout' ] or u'' ).splitlines ()
447+ if 'stderr' in data and 'stderr_lines' not in data :
448+ data ['stderr_lines' ] = (data ['stderr' ] or u'' ).splitlines ()
449449
450450 return data
451451
0 commit comments