
    Vh                         d dl mZmZmZ eZdZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ  ed	      Zd
 Z G d de      Zy)    )absolute_importdivisionprint_functiona  
    name: json
    short_description: Ansible screen output as JSON
    description:
        - This callback converts all events into JSON output to stdout
    type: stdout
    requirements:
      - Set as stdout in config
    options:
      show_custom_stats:
        name: Show custom stats
        description: 'This adds the custom stats set via the set_stats plugin to the play recap'
        default: False
        env:
          - name: ANSIBLE_SHOW_CUSTOM_STATS
        ini:
          - key: show_custom_stats
            section: defaults
        type: bool
      json_indent:
        name: Use indenting for the JSON output
        description: 'If specified, use this many spaces for indenting in the JSON output. If <= 0, write to a single line.'
        default: 4
        env:
          - name: ANSIBLE_JSON_INDENT
        ini:
          - key: json_indent
            section: defaults
        type: integer
    notes:
      - When using a strategy such as free, host_pinned, or a custom strategy, host results will
        be added to new task results in ``.plays[].tasks[]``. As such, there will exist duplicate
        task objects indicated by duplicate task IDs at ``.plays[].tasks[].task.id``, each with an
        individual host result for the task.
N)partial)Host)to_text)AnsibleJSONEncoder)CallbackBase)lineardebugc                  `    dt         j                   j                         j                         z  S )Nz%sZ)datetimeutcnow	isoformat     g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/callback/json.pycurrent_timer   ;   s%    8$$++-77999r   c                   n     e Zd ZdZdZdZd fd	Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Z xZS )CallbackModuleg       @stdoutzansible.posix.jsonc                     t         t        |   |       g | _        i | _        d| _        | j                          | j                  d      | _        | j                  dk  rd | _        y y )NFjson_indentr   )	superr   __init__results	_task_map_is_lockstepset_options
get_option_json_indent)selfdisplay	__class__s     r   r   zCallbackModule.__init__D   s`    nd,W5! OOM:! $D "r   c                     |j                   t        v | _        |j                         t	        |j
                        t	        |j                               dt               idg dS )Nstartnameidpathduration)playtasks)strategyLOCKSTEP_CALLBACKSr   get_namer   _uuidget_pathr   r"   r,   s     r   	_new_playzCallbackModule._new_playP   sY     MM-?? djj)0\^	 

 
	
r   c                     |j                         t        |j                        t        |j                               dt	               idi dS )Nr&   r'   )taskhosts)r0   r   r1   r2   r   r"   r6   s     r   	_new_taskzCallbackModule._new_task^   sG     djj)0\^	 

 
	
r   c                     |j                         |j                  f}| j                  j                  || j                  d   d   d         S Nr-   )r0   r1   r   getr   )r"   hostr6   keys       r   _find_result_taskz CallbackModule._find_result_taskk   sF    }}

+~~!!LLW%b)
 	
r   c                 X    | j                   j                  | j                  |             y N)r   appendr4   r3   s     r   v2_playbook_on_play_startz(CallbackModule.v2_playbook_on_play_startr   s    DNN401r   c                     | j                   ry |j                         |j                  f}| j                  |      }|| j                  |<   | j
                  d   d   j                  |       y r;   )r   r0   r1   r9   r   r   rC   )r"   r>   r6   r?   task_results        r   v2_runner_on_startz!CallbackModule.v2_runner_on_startu   sZ    }}

+nnT*)sR!((5r   c                 ~    | j                   sy | j                  d   d   j                  | j                  |             y r;   r   r   rC   r9   )r"   r6   is_conditionals      r   v2_playbook_on_task_startz(CallbackModule.v2_playbook_on_task_start}   3      R!(()=>r   c                 ~    | j                   sy | j                  d   d   j                  | j                  |             y r;   rI   r8   s     r   !v2_playbook_on_handler_task_startz0CallbackModule.v2_playbook_on_handler_task_start   rL   r   c                 H    t        |t        f      r|j                         S |S rB   )
isinstancer   r0   )r"   r?   s     r   _convert_host_to_namez$CallbackModule._convert_host_to_name   s    cD7#<<>!
r   c                 &    t        |j                  j                               }i }|D ]  }|j                  |      }|||<    i }i } j	                  d      rh|j
                  r\|j                  t         fd|j
                  j                         D                     |j                  |j                  di               j                  |||d} j                  j                  t        j                  |t         j                   d             y)z&Display info about playbook statisticsshow_custom_statsc              3   J   K   | ]  \  }}j                  |      |f  y wrB   )rQ   ).0kvr"   s      r   	<genexpr>z6CallbackModule.v2_playbook_on_stats.<locals>.<genexpr>   s%     $iDAqd&@&@&CQ%G$is    #_run)playsstatscustom_statsglobal_custom_statsT)clsindent	sort_keysN)sorted	processedkeys	summarizer    customupdatedictitemspopr   _displayr#   jsondumpsr	   r!   )	r"   r[   r7   summaryhsr\   r]   outputs	   `        r   v2_playbook_on_statsz#CallbackModule.v2_playbook_on_stats   s     u++-. 	A"AGAJ	  ??./ELL$iTYT`T`TfTfTh$i ij&&|'7'7'CD \\(#6	
 	djj5GPTPaPamqrsr   c                    |j                   }|j                  }|j                  j                         }|j	                  |       |j
                  |d<   | j                  ||      }||d   |j                  <   t               }||d   d   d<   || j                  d   d   d   d<   | j                  s*|j                         |j                  f}	| j                  |	= yy)	zPThis function is used as a partial to add failed/skipped info in a single methodactionr7   r6   r+   endr<   r,   N)_host_task_resultcopyrf   rs   r@   r(   r   r   r   r0   r1   r   )
r"   on_inforesultkwargsr>   r6   result_copyrF   end_timer?   s
             r   _record_task_resultz"CallbackModule._record_task_result   s    ||||nn))+7# $H,,T48*5GTYY'>19FJ'.6>R ,U3  ==?DJJ/Cs# !r   c                     |dvrt         j                  | |      S |j                  dd      d   }i }|dv rd||<   t        | j                  |      S )zYReturn ``_record_task_result`` partial with a dict containing skipped/failed if necessary)v2_runner_on_okv2_runner_on_failedv2_runner_on_unreachablev2_runner_on_skipped_   )failedskippedT)object__getattribute__rsplitr   r~   )r"   r(   onry   s       r   r   zCallbackModule.__getattribute__   s]    uu**466[[a #&&GBKt//99r   rB   )__name__
__module____qualname__CALLBACK_VERSIONCALLBACK_TYPECALLBACK_NAMEr   r4   r9   r@   rD   rG   rK   rN   rQ   rq   r~   r   __classcell__)r$   s   @r   r   r   ?   sP    M(M
%


26?
?

t4$(:r   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONr   rk   	functoolsr   ansible.inventory.hostr   ansible.module_utils._textr   ansible.parsing.ajsonr	   ansible.plugins.callbackr
   	frozensetr/   r   r   r   r   r   <module>r      sS    C B"H    ' . 4 1 23 :F:\ F:r   