o nnhz@sddlmZddlZddlmZmZddlmZddl m Z ddl m Z ddl mZddlmZdd lmZeZgd ZGd d d eZGd ddeZGdddZGdddZdS)) annotationsN)IntEnumIntFlag) constants)AnsibleAssertionError)boolean)Block)Task)Display) PlayIteratorIteratingStates FailedStatesc@$eZdZdZdZdZdZdZdZdS)r rN) __name__ __module__ __qualname__SETUPTASKSRESCUEALWAYSHANDLERSCOMPLETErrI/usr/local/lib/python3.10/dist-packages/ansible/executor/play_iterator.pyr &r c@r)r rrrrN) rrrNONErrrrrrrrrr /rr c@s<eZdZddZddZddZddZd d Zd d Zd S) HostStatecCs||dd|_g|_g|_d|_d|_d|_d|_d|_tj |_ t j |_ d|_d|_d|_d|_d|_d|_d|_d|_dS)NrTF)_blockshandlershandler_notifications cur_blockcur_regular_taskcur_rescue_taskcur_always_taskcur_handlers_taskr r run_stater r" fail_statepre_flushing_run_stateupdate_handlers pending_setuptasks_child_staterescue_child_statealways_child_state did_rescuedid_start_at_task)selfblocksrrr__init__9s$ zHostState.__init__cCs d|jS)Nz HostState(%r))r$r6rrr__repr__O zHostState.__repr__cCsDd|j|j|j|j|j|j|j|j|j|j |j |j |j |j |jfS)NaHOST STATE: block=%d, task=%d, rescue=%d, always=%d, handlers=%d, run_state=%s, fail_state=%s, pre_flushing_run_state=%s, update_handlers=%s, pending_setup=%s, tasks child state? (%s), rescue child state? (%s), always child state? (%s), did rescue? %s, did start at task? %s)r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r9rrr__str__Rs$zHostState.__str__cCs6t|tsdSdD]}t||t||krdSq dS)NF)r$r'r(r)r*r+r,r-r.r/r0r1r2r3T) isinstancer#getattr)r6otherattrrrr__eq__hs zHostState.__eq__cCs |j|jSN)r$r'r9rrrget_current_blockus zHostState.get_current_blockcCst|j}|jdd|_|jdd|_|j|_|j|_|j|_|j|_|j|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_|j|_|jdurP|j|_|jdur[|j|_|jdurf|j|_|SrB)r#r$r%r&r'r(r)r*r+r,r-r.r/r0r4r5r1copyr2r3)r6 new_staterrrrDxs,       zHostState.copyN) rrrr8r:r<rArCrDrrrrr#8s r#c@seZdZd:ddZddZd:ddZdd Zd d Zd d ZddZ ddZ ddZ ddZ d;ddZ ddZddZd d!Zd"d#Zed$d%Zdd.d/Zd?d2d3Zd@d5d6Zd@d7d8Zd9S)Ar FcCs<||_g|_||_t|jd}d|_t|d}d|_d|_d|_i|_ |jj s+dg|_ dD]} t |j| d} | dur?| |j | <q-| |jj |jjdurV|jjdd|_|g|_||}|j|||_|jD]} | |} | r|j| |j| qod d |jjD|_i|_d} |j|jj|jjd }t||_|D]e}||jt |jd |j!dur|s |j"|d d\}}|j#t$j%krn,|j|j!ks|jrt&&|j|j!s|'|j!kst&&|'|j!rd } n||j|q| rd |j|j_(t$j)|j|j_#q| rd|_!d|_*d|_+dS)N)playFblock gather_factszansible.builtin.gather_factszGathering Factsalways) gather_subsetgather_timeout fact_pathcSg|] }|jD]}|qqSrrG.0bhrrr z)PlayIterator.__init__..)orderr7T)peekr),_playr$_variable_managerrrun_oncer actionresolved_actionnameargstagsr> set_loader_loader_included_conditionalwhenrHfilter_tagged_tasksappend get_tasks all_taskscompile has_tasksextendr% _host_states get_hostshostsrUlen batch_sizeset_state_for_hostr# start_at_taskget_next_task_for_hostr,r rfnmatchget_namer5rend_playcur_task)r6 inventoryrF play_contextvariable_managerall_vars start_at_done setup_block setup_taskoptionvaluerH new_blockstart_at_matchedbatchhoststaskrrrr8sp           "   zPlayIterator.__init__cCs0|j|jvr||jtgd|j|jS)NrV)r]rkrpr#rDr6rrrrget_host_states zPlayIterator.get_host_statecCstd|j||}d}|jtjkr!td|j|dfS|j||d\}}|s3||j|td|jtd|td|||fS)Nz!getting the next task for host %sz$host %s is done iterating, returningrz"done getting next task for host %sz ^ task is: %sz ^ state is: %s) displaydebugr]rr,r r_get_next_task_from_staterp)r6rrWrrrrrrrs  z#PlayIterator.get_next_task_for_hostcCsbd} z|j|j}Wntytj|_|dfYSw|jtjkr|jsvd|_tj }|j j dup9t |j j dd}|dkr@|s_|dkrLt |j j dds_|dkrt|rt|j j|jiddst|jd}|rtt|jdkrt|jd}n d|_tj|_|js|jd 7_d|_d|_d|_d|_d|_d|_n|jtjkr|jrd|_|jr|j|j|d \|_}||jrd|_||n|dus|jjtjkrd|_qn||rtj|_n|jt|jkrtj |_n|j|j}t!|t"r t#|gd |_tj|j_d}|jd 7_nl|jtjkr|jrM|j|j|d \|_}||jr:d|_||nE|dusG|jjtjkrKd|_qn2|j$t%j@t%jkr]tj |_n"|jt|j&kr{t|j&dkrrt%j'|_$tj |_d|_(n|j&|j}t!|t"rt#|gd |_tj|j_d}|jd 7_n|jtj kr-|jr|j|j|d \|_}||jrd|_||n|dus|jjtjkrd|_qn|jt|j)kr |j$t%j'krtj|_n|jd 7_d|_d|_d|_tj|_d|_d|_d|_d|_(nt|j)|j}t!|t"r%t#|gd |_tj|j_d}|jd 7_nR|jtj*krt|j+rC|j,dd|_,d|_+ z|j,|j-}Wntybd}d|_-|j.|_d|_+Ynw|j-d 7_-|/|rqnqDn |jtjkr|dfS|r|j0r|j1tj2vr|j3d dd kr|4|jj5st6dd|j,Drq ||fSq)NTF)strictimplicitexplicitsmart_ansible_facts_gatheredrrrrV _raw_paramsflush_handlerscss|]}|j VqdSrB)notified_hosts)rPrRrrr sz9PlayIterator._get_next_task_from_state..)7r$r' IndexErrorr rr,rr0CDEFAULT_GATHERINGrXrIrrY _fact_cachegetr]rirnrHrr5r(r)r*r1r2r3r_check_failed_state_set_failed_staterrr=rr#r-r rescuer"r4rJrr/r%r+r.is_host_notifiedrr[ _ACTION_METAr^get_state_for_hostr&all)r6staterrrH gatheringimpliedr|rrrrs    &                      z&PlayIterator._get_next_task_from_statecCsB|jtjkr|jtjO_tj|_|S|jtjkrP|jdur(||j|_|S|jtjO_|j |j j r=tj |_|S|j |j j rJtj|_|Stj|_|S|jtj kr|jdurd||j|_|S|jtj O_|j |j j rytj|_|Stj|_|S|jtjkr|jdur||j|_|S|jtjO_tj|_|SrB)r,r rr-r rrr1rr$r'rrrJrr2r3r6rrrrrsD      zPlayIterator._set_failed_statecCsp||}td||f|jtjkr|j|_d|_||}td|| |j ||j j |j dS)Nz)marking host %s failed, current state: %sTz^ failed state is now: %s)rrrr,r rr.r/rrpr]rX_removed_hostsrer6rrrrrmark_host_faileds   zPlayIterator.mark_host_failedcstfddjDS)Nc3s&|]\}}|r|dfVqdS)TN)r)rPrrr9rrrs$z0PlayIterator.get_failed_hosts..)dictrkitemsr9rr9rget_failed_hostsszPlayIterator.get_failed_hostscCs|durdS|jtjkr||jrdS|jtjkr"||jr"dS|jtj krT|jtjkr8|jtj@dkr8dS|jtjkrH|jtj@dkrHdS|j oR|jtj@dk S|jtj kry||j ry|j |j}t|jdkrw|jtj@dkrwdSdSdS)NFTr)r,r rrr2rr3r-r r"r4rr1r$r'rnr)r6rr'rrrrs$  z PlayIterator._check_failed_statecCs||}||SrB)rrrrrr is_faileds  zPlayIterator.is_failedcCs|||jdSrB)_clear_state_errorsrr]rrrrclear_host_errors"zPlayIterator.clear_host_errorsrr#returnNonecCsZtj|_|jdur||jdS|jdur||jdS|jdur+||jdSdSrB)r r"r-r1rr2r3rrrrr%s   z PlayIterator._clear_state_errorscCsj|jtjkr|jdur||jS|jtjkr"|jdur"||jS|jtjkr3|jdur3||jS|S)z_ Finds the active state, recursively if necessary when there are child states. N) r,r rr1get_active_staterr2rr3rrrrr/s   zPlayIterator.get_active_statecCs`|jtjkr |jr dS|jdur||jS|jdur#||jS|jdur.||jSdS)z Given the current HostState state, determines if the current block, or any child blocks, are in rescue mode. TNF) r,r rrCrr1is_any_block_rescuingr2r3rrrrr;s      z"PlayIterator.is_any_block_rescuingcCs|jtjkr |jtjks|s|S|jtjkrJ|jr#||j||_|S|j|j  }|j d|j }|j |j d}||||_ ||j|j <|S|jtj kr|jr]||j||_|S|j|j  }|jd|j}|j|jd}||||_||j|j <|S|jtjkr|jr||j||_|S|j|j  }|jd|j}|j|jd}||||_||j|j <|S|jtjkrdd|D|j|j|j<|S)NcSrNrrGrOrrrrSkrTz9PlayIterator._insert_tasks_into_state..)r-r r"r,r rr1_insert_tasks_into_stater$r'rDrHr(rr2rr)rr3rJr*rr%r+)r6r task_list target_blockbeforeafterrrrrJsF        z%PlayIterator._insert_tasks_into_statecCs ||j||||dSrB)rpr]rr)r6rrrrr add_tasksos zPlayIterator.add_taskscCs|jSrBrkr9rrr host_statesrszPlayIterator.host_stateshostnamestrcCs |j|SrBr)r6rrrrrvr;zPlayIterator.get_state_for_hostcCs(t|ts tdt|||j|<dS)Nz-Expected state to be a HostState but was a %s)r=r#rtyperk)r6rrrrrrpys zPlayIterator.set_state_for_hostr,r cC*t|ts tdt|||j|_dS)Nz5Expected run_state to be a IteratingStates but was %s)r=r rrrkr,)r6rr,rrrset_run_state_for_host~ z#PlayIterator.set_run_state_for_hostr-r cCr)Nz3Expected fail_state to be a FailedStates but was %s)r=r rrrkr-)r6rr-rrrset_fail_state_for_hostrz$PlayIterator.set_fail_state_for_host notificationcCs(|j|}||jvr|j|dSdSrB)rkr&re)r6rr host_staterrradd_notifications  zPlayIterator.add_notificationcCs|j|j|dSrB)rkr&remove)r6rrrrrclear_notificationrzPlayIterator.clear_notificationN)F)rr#rr)rrrr#)rrrr#rr)rrr,r rr)rrr-r rr)rrrrrr)rrrr8rrrrrrrrrrrrrrrpropertyrrrprrrrrrrrr s2 V S     %      r ) __future__rrsenumrransiblerransible.errorsr)ansible.module_utils.parsing.convert_boolransible.playbook.blockransible.playbook.taskr ansible.utils.displayr r__all__r r r#r rrrrs         Y