
    VhXA                        d dl mZ dZd dlZd dlmZ d dlmZm	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 d dlmZ d dlmZ  e       Z G d de      Zy)    )annotationsa  
    name: free
    short_description: Executes tasks without waiting for all hosts
    description:
        - Task execution is as fast as possible per batch as defined by C(serial) (default all).
          Ansible will not wait for other hosts to finish the current task before queuing more tasks for other hosts.
          All hosts are still attempted for the current task, but it prevents blocking new tasks for hosts that have already finished.
        - With the free strategy, unlike the default linear strategy, a host that is slow or stuck on a specific task
          won't hold up the rest of the hosts and tasks.
    version_added: "2.0"
    author: Ansible Core Team
N)	constants)AnsibleErrorAnsibleParserError)Handler)IncludedFile)action_loader)StrategyBase)Templar)to_text)Displayc                  ,     e Zd ZdZ fdZ fdZ xZS )StrategyModuleFc                :    t         t        |   |       d| _        y )NF)superr   __init___host_pinned)selftqm	__class__s     M/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/strategy/free.pyr   zStrategyModule.__init__4   s    nd,S1!    c                x   d}| j                   j                  }t        | j                        }| j	                  |j
                         |j
                  j                  t        j                  d       d}|	rJ| j                   j                  	s3| j                  |      }t        |      dk(  r| j                   j                  d       d}nd}|}g }	d}
	 ||   }t        j                  d|z         |j                         }|j                  |d      \  }}t        j                  d	|z  |
       t        j                  d|z  |
       |s| j                  j!                  |d      r?| j                   j"                  j!                  |d      st        j                  d|
       d}| j                   j"                  j!                  |d      sG|rD| j                  j!                  |d      st        j                  d|
       | j$                  j'                  |j
                  ||| j(                  | j*                        }| j-                  ||j
                         t/        | j0                  |      }t        j                  d|
       	 t3        |j5                  |j6                              }|dkD  rd}| j                  D ]?  }|s|j?                         s|j@                  jB                  |jB                  k(  s;|dz  }A t        j                  d|j                         |fz         ||k\  rnd| j                  |<   |jE                  |jF                  |       tI        |tJ              r|jM                  |       	 tO        j                   |jP                  d|jR                        }	 tW        |j5                  |jF                  d      d      |_#        t        j                  d|
       |j5                  |jX                        xs |xr t[        |dd      }|rH|r1t[        |dd      r$t;        d|jP                  z  |j<                        t        j                  d       tI        |tJ              s|j\                  rx| j_                  ||j
                        }|ja                  |      rK|j\                  jb                  jd                  du r)t        j                  d |z  |
       | j                  |= /|jP                  tf        jh                  v r;| jj                  r
|
dz  }
|dz  }| jm                  ||||!       d| j                  |<   n| jn                  r| jq                  ||      r|jr                  rt        j                  d"       tI        |tJ              r| j                   j                  d#|       n| j                   j                  d$|d%       | ju                  ||||       |dz  }~nt        j                  d&|z         | jj                  r
|dk(  r|r|}n |dz  }|t        |      dz
  kD  rd}||k(  rnx| jw                  |      }|	jy                  |       |t        |      |
z   z  }| j{                  |       t}        j~                  |	|| j0                  | j$                  '      }t        |      dkD  rt        d( |D              }t               }|D ]  }t        j                  d)|z         d}	 |j                  rG| j                  |      }|j                  |j
                  | j$                  | j0                  *      \  }} n/tI        |j@                  tJ              }| j                  |||d+      }|j
                  j                  D !"cg c]  }!|!j                  D ]  }"|"  c}"}!|_F        |j                  D ]2  }| j                   j                  j                  d,|jF                         4 | j                   j                  d-|       |D ]  }$|r(|$j                  D ]  }|j                  dd |_R         |$}%n\| j$                  j'                  |j
                  |$j                         | j(                  | j*                  3      }|$j                  |      }%|D ]%  }||j                  v s||   j                  |%       '  t        j                  d4|z          |D ]6  }d| j                   j                  |jF                  <   |j                  |       8 t        j                  d5t        |      z         |D ]  }|j                  |||           t        j                  d6       t        j                  tf        j                         |r| j                   j                  s	3| j                  |      }t        t        |   |||      S # t8        $ r}t;        d|j<                  |      d}~ww xY w# tT        $ r d}Y mw xY w# t8        $ r t        j                  d|
       Y Ow xY wc c}"}!w # t        $ r  t:        $ r}t        j                  tW        |      d.       |j                  D ]  }#d|#j                  d/<   t        |      |#j                  d0<   | j                   j                  j                  d1|#j                  jF                         | j                   j                  d2|#       |j                  |#j                          Y d}~Fd}~ww xY w)7am  
        The "free" strategy is a bit more complex, in that it allows tasks to
        be sent to hosts as quickly as they can be processed. This means that
        some hosts may finish very quickly if run tasks result in little or no
        work being done versus other systems.

        The algorithm used here also tries to be more "fair" when iterating
        through hosts by remembering the last host in the list to be given a task
        and starting the search from there as opposed to the top of the hosts
        list again, which would end up favoring hosts near the beginning of the
        list.
        r   NzsUsing max_fail_percentage with the free strategy is not supported, as tasks are executed independently on each hostT!v2_playbook_on_no_hosts_remainingFznext free host: %s)peekzfree host state: %s)hostzfree host task: %szthis host has work to dozgetting variables)playr   task_hosts
_hosts_all)r   )loader	variableszdone getting variablesz3Failed to convert the throttle value to an integer.)objorig_exc   ztask: %s, same_tasks: %d)
class_onlycollection_list)fail_on_undefinedempty)	nonstringzdone templatingz!templating failed for some reasonBYPASS_HOST_LOOPzThe '%s' module bypasses the host loop, which is currently not supported in the free strategy and would instead execute for every host in the inventory list.)r#   zUsing run_once with the free strategy is not currently supported. This task will still be executed for every host in the inventory list.z)'%s' skipped because role has already run)target_hostzpUsing any_errors_fatal with the free strategy is not supported, as tasks are executed independently on each host!v2_playbook_on_handler_task_startv2_playbook_on_task_start)is_conditionalz%s is blocked, skipping for now)iteratorr!   variable_managerc              3  $   K   | ]  }|g f 
 y w)N ).0r   s     r   	<genexpr>z%StrategyModule.run.<locals>.<genexpr>   s     !D4*!Ds   zcollecting new blocks for %s)r   r1   r!   )r0   
is_handlerhandle_stats_and_callbacksokv2_playbook_on_include)	wrap_textfailedreasonfailuresv2_runner_on_failed)r   r   r   r    z!done collecting new blocks for %sz@adding all collected blocks from %d included file(s) to iteratorz(done adding collected blocks to iterator)`_tqmRUN_OKlen_workers_set_hosts_cache_playmax_fail_percentagedisplaywarning_terminatedget_hosts_leftsend_callbackdebugget_nameget_next_task_for_host_blocked_hostsget_unreachable_hosts_variable_managerget_vars_hosts_cache_hosts_cache_alladd_tqm_variablesr   _loaderinttemplatethrottle	Exceptionr   _dsis_alive_task_uuidset_state_for_hostname
isinstancer   remove_hostr	   actioncollectionsKeyErrorr   run_oncegetattr_role_get_cached_rolehas_run	_metadataallow_duplicatesC_ACTION_METAr   _execute_meta_step
_take_stepany_errors_fatal_queue_task_process_pending_resultsextendupdate_active_connectionsr   process_include_resultsdictset_is_role_copy_included_fileget_block_list_load_included_filehandlersblockr   _stats	incrementr   error_results_resultstr_hostaddnotified_hostsget_first_parent_includefilter_tagged_tasksappend_failed_hostsmark_host_failed	add_taskstimesleepDEFAULT_INTERNAL_POLL_INTERVAL_wait_on_pending_resultsr   r   run)'r   r0   play_context	last_hostresultworkers_free
work_to_do
hosts_leftstarting_hosthost_resultsmeta_task_dummy_results_countr   	host_namestater   	task_varstemplarrY   e
same_tasksworkerrc   rf   role_objresultsincluded_files
all_blocksfailed_includes_hostsincluded_filer6   new_ir
new_blockshandler_blocksbhr	new_blockfinal_blockr   s'                                         r   r   zStrategyModule.run8   sY
    	!! 4==)hnn->>--9OO  R  S
!6!6,,X6J:!#		''(KLJ%M L,-)!),2T9: MMO	 !) ? ?4 ? P3e;)L2T9	J
 D//33IuEtyyOkOkOoOopy  |A  PBMM"<9MM "&Jyy3377	5Id..229eD&9	J$($:$:$C$C^bimKOK\K\OSOdOd %D %f	 ..yx~~.N")"S&>YO@'*7+;+;DMM+J'KH $a<)*J*.-- 4#)foo.?FLLDVDVZ^ZdZdDd$.!OJ4 $MM*DYcGd*de)X5 % :>++I6 33DIIuE%dG4 ,,T2*%2%6%6t{{teieueu%vF_(/0@0@^c0@0dpw(xDI#MM*;)ML $+#3#3DMM#B#{fF{QXY_asuzQ{#%'&:Le*T&2 4uw{  xC  xC4C IM  IQ  IQ'R !R !( 1a !b
  *$8TZZ'+'<'<T8>>'RH'//5$**:N:N:_:_ch:h '.Y\`.`gp q$($7$7	$B (;;!..8#00 = B = , 1 ..t\8Y].^=BD//	: $(::y1Q#'#8#8$+OO 5g %h#-dG#<$(II$;$;<_ae$f$(II$;$;<WY]ns$;$t $ 0 0tY U , 1$-&G)&ST $$):z -I Q	s:22 !I -u x 33H=G( CL+HHHL**73)AA!||!%!7!7	N >"Q&!!D!DD
(+%%3 :WMMM"@="PQ!&J%Y(11%)%=%=m%LF9?9N9N%-^^151G1G'+|| :O :6J *4M4G4G)QJ)-)A)A -)1+5;@	 *B *J 9A8O8O,a1YZY`Y`,aTUQ,aQ,a) %2$8$8 HD II,,66tTYYGH		//0H-X%/ E	%(1 N6C6J6J16M 3N*3K(,(>(>(G(G%-^^%.%G%G%I'+'8'8+/+@+@	 )H )I +4*G*G	*RK$. ED#}';';; *4 0 7 7 DEE  MM"E"UVu:Wx 2 4D9=DII++DII6--d34 `cfgucvvw& ?D&&tZ-=>?HI JJq778G !6!6L //9 ^T.xvNNu  ) @"./djnjrjr}~"@*  ( * &*F*  ) _ $MM*MT]M^_N -b- ' !gajEB!.!7!7 ?A26AIIh/25a&AIIh/ II,,66z177<<P II334I1M155agg>? !!si   $g+ 	,h 6Ah& Bi(i	i+	h4hhh#"h#& i
	i
il9%C	l44l9)__name__
__module____qualname__ALLOW_BASE_THROTTLINGr   r   __classcell__)r   s   @r   r   r   /   s     ""FO FOr   r   )
__future__r   DOCUMENTATIONr   ansibler   rm   ansible.errorsr   r   ansible.playbook.handlerr   ansible.playbook.included_filer   ansible.plugins.loaderr	   ansible.plugins.strategyr
   ansible.templater   +ansible.module_utils.common.text.convertersr   ansible.utils.displayr   rF   r   r3   r   r   <module>r      sH   " #  " ; , 7 0 1 $ ? )
)OO\ OOr   