
    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	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: jsonl
    short_description: Ansible screen output as JSONL (lines in json format)
    description:
        - This callback converts all events into JSON output to stdout
        - This callback in contrast with ansible.posix.json uses less memory, because it doesn't store results.
    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 not specified or <= 0, write to a single line.'
        default: 0
        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     h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/callback/jsonl.pycurrent_timer   =   s%    8$$++-77999r   c                   t     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d Z xZS )CallbackModuleg       @stdoutzansible.posix.jsonlc                     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__F   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_playR   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"   r5   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"   hostr5   keys       r   _find_result_taskz CallbackModule._find_result_taskm   sF    }}

+~~!!LLW%b)
 	
r   c                     | j                  |      }| j                  j                  |       | j                  d|       y )Nv2_playbook_on_play_start)r3   r   append_write_event)r"   r,   play_results      r   r?   z(CallbackModule.v2_playbook_on_play_startt   s3    nnT*K(5{Cr   c                     | j                   ry |j                         |j                  f}| j                  |      }|| j                  |<   | j
                  d   d   j                  |       | j                  d|       y )Nr9   r-   v2_runner_on_start)r   r0   r1   r7   r   r   r@   rA   )r"   r;   r5   r<   task_results        r   rD   z!CallbackModule.v2_runner_on_starty   sk    }}

+nnT*)sR!((5.<r   c                     | j                   sy | j                  |      }| j                  d   d   j                  |       | j	                  d|       y )Nr9   r-   v2_playbook_on_task_startr   r7   r   r@   rA   )r"   r5   is_conditionalrE   s       r   rG   z(CallbackModule.v2_playbook_on_task_start   sI      nnT*R!((55{Cr   c                     | j                   sy | j                  |      }| j                  d   d   j                  |       | j	                  d|       y )Nr9   r-   !v2_playbook_on_handler_task_startrH   )r"   r5   rE   s      r   rK   z0CallbackModule.v2_playbook_on_handler_task_start   sI      nnT*R!((5={Kr   c                 H    t        |t        f      r|j                         S |S N)
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              |||d} j                  d|       y)z&Display info about playbook statisticsshow_custom_statsc              3   J   K   | ]  \  }}j                  |      |f  y wrM   )rO   ).0kvr"   s      r   	<genexpr>z6CallbackModule.v2_playbook_on_stats.<locals>.<genexpr>   s%     $iDAqd&@&@&CQ%G$is    #_run)statscustom_statsglobal_custom_statsv2_playbook_on_statsN)sorted	processedkeys	summarizer    customupdatedictitemspoprA   )	r"   rX   r6   summaryhsrY   rZ   outputs	   `        r   r[   z#CallbackModule.v2_playbook_on_stats   s     u++-. 	A"AGAJ	  ??./ELL$iTYT`T`TfTfTh$i ij&&|'7'7'CD (#6
 	0&9r   c           	          ||d<   t               |d<   | j                  j                  t        j                  |t
        | j                  dd             y )N_event
_timestampz,:T)clsindent
separators	sort_keys)r   _displayr#   jsondumpsr	   r!   )r"   
event_namerh   s      r   rA   zCallbackModule._write_event   sO    %x+~|djj5GPTPaPanr  C  D  	Er   c                    |j                   }|j                  }|j                  j                         }|j	                  |       |j
                  |d<   | j                  ||      }t               }	|	|d   d   d<   |	| j                  d   d   d   d<   t        j                  |      }
||
d   |j                  <   | j                  s)|j                         |j                  f}| j                  |= | j                  ||
       y)	zPThis function is used as a partial to add failed/skipped info in a single methodactionr5   r+   endr9   r,   r6   N)_host_task_resultcopyra   ru   r=   r   r   deepcopyr(   r   r0   r1   r   rA   )r"   rs   on_inforesultkwargsr;   r5   result_copyrE   end_timetask_result_copyr<   s               r   _record_task_resultz"CallbackModule._record_task_result   s    ||||nn))+7# $H,,T48>19FJ'.6>R ,U3==5/:!$)),  ==?DJJ/Cs#*&67r   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(   onr|   s       r   r   zCallbackModule.__getattribute__   s_    uu**466[[a #&&GBKt//w??r   rM   )__name__
__module____qualname__CALLBACK_VERSIONCALLBACK_TYPECALLBACK_NAMEr   r3   r7   r=   r?   rD   rG   rK   rO   r[   rA   r   r   __classcell__)r$   s   @r   r   r   A   sY    M)M
%


D
=DL
:2E
80@r   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONr   rq   rz   	functoolsr   ansible.inventory.hostr   ansible.module_utils._textr   ansible.parsing.ajsonr	   ansible.plugins.callbackr
   	frozensetr/   r   r   r   r   r   <module>r      sX    C B#J     ' . 4 1 23 :U@\ U@r   