a )gz@sddlmZmZmZeZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlmZmZmZmZmZmZddlmZddlmZddlmZddlm Z dd l!m"Z"m#Z#dd l$m%Z%dd l&m'Z'dd l(m)Z)dd l*m+Z+ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2ddl3m4Z4ddl5m6Z6m7Z7ddl8m9Z9ddl:m;Z;mZ>m?Z?ddl@mAZAddlBmCZCmDZDeAZEddejFGDZHdgZIGdddeJZKddZLddZMGdddZNd d!ZOdS)")absolute_importdivisionprint_functionN) constants) AnsibleErrorAnsibleParserErrorAnsibleUndefinedVariableAnsibleConnectionFailureAnsibleActionFailAnsibleActionSkip) TaskResult)get_action_args_with_defaults)boolean) binary_type)to_text to_native)write_to_file_descriptor) Conditional)Task)get_plugin_class) become_loadercliconf_loaderconnection_loaderhttpapi_loadernetconf_loaderterminal_loader)Templar)AnsibleCollectionConfigAnsibleCollectionRef)listify_lookup_plugin_terms)to_unsafe_textwrap_var)namespace_facts clean_facts)Display) combine_vars isidentifiercCs g|]}d|vrd|vr|qS)becomeZ_pass).0xr(r(B/usr/lib/python3.9/site-packages/ansible/executor/task_executor.py (r, TaskExecutorc@s eZdZdS)TaskTimeoutErrorN)__name__ __module__ __qualname__r(r(r(r+r/-sr/cCstdSN)r/)Zsignumframer(r(r+ task_timeout1sr5cst|ts|Si}|D]x}|dkr.qqt|dtrTt|d||d<qt|dtrfdd|dD||d<q|d||d<q|S)z Remove args with a value equal to the ``omit_token`` recursively to align with now having suboptions in the argument_spec rcsg|]}t|qSr() remove_omit)r)v omit_tokenr(r+r,Er-zremove_omit..) isinstancedictitemsr7list)Z task_argsr:new_argsir(r9r+r75s    r7c@s|eZdZdZddZddZddZdd Zdd d Zdd dZ ddZ ddZ ddZ ddZ ddZddZddZd S)r.z This is the main worker class for the executor pipeline, which handles loading an action plugin to actually dispatch the task to a given host. This class roughly corresponds to the old Runner() class. c CsJ||_||_||_||_||_||_||_d|_||_d|_ |j dSr3) _host_task _job_vars _play_context _new_stdin_loader_shared_loader_obj _connection_final_q_loop_eval_errorZsquash) selfhosttaskZjob_vars play_contextZ new_stdinloadershared_loader_objZfinal_qr(r(r+__init__UszTaskExecutor.__init__cstd|jjz2zz |}Wn.tyT}zd}||_WYd}~n d}~00|durt|dkr||}t |d}d|d<|D]}d|vr|dr| dsd|d<|drd|vsd|vr|dsd|d<d |vr:|d r:| d }| d s"d|d <d |d <||j_ n|jj r:|s:||j_ d D]d}||vr>||r>||vrhg||<t ||ts||g||<||||||<||=q>q| d dsd|d <|drd|d <nt dddgd}ntd|}tdd|vrd|d<d fdd td|}td|WWz|jWStybYSty}ztdt|WYd}~Sd}~00Sty:}zt dtt|dd|jjdWYd}~Wz|jWStyYSty.}ztdt|WYd}~Sd}~00Sd}~0ty}zt dtdt|ddttd|jjdWYd}~Wz|jWStyYSty}ztdt|WYd}~Sd}~00Sd}~00Wz|jWnJtyYn8tyD}ztdt|WYd}~n d}~00n\z|jWnJtyjYn8ty}ztdt|WYd}~n d}~000dS)!a The main executor entrypoint, where we determine if the specified task requires looping and either runs the task with self._run_loop() or self._execute(). After that, the returned results are parsed and returned as a dict. zin run() - task %sNr)resultsTskippedchangedFfailed_ansible_ignore_errorszOne or more items failedmsg)warningsZ deprecationszAll items completedzAll items skippedzNo items in the list)rTrSZskipped_reasonrRzcalling self._execute()z_execute() donesurrogate_or_strictc st|trt||dSt|tr|D]Z}z|||d||<Wq$ty||dkrvtd||dd||<nYq$0q$n,t|trt|D]\}}||d||<q|S)Nerrorsdiffz{We were unable to decode all characters in the module return data. Replaced some in an effort to return as much as possiblesurrogate_then_replace) r;rr r< UnicodeErrordisplaywarningr> enumerate)resr[kidxitem _clean_resr(r+rgs       z$TaskExecutor.run.._clean_reszdumping result to jsonzdone dumping result, returningzerror closing connection: %sZ simplerepr)Z nonstring)rUrW_ansible_no_logz.Unexpected failure during module execution: %s)rUrW exceptionstdoutrh)rY)r_debugrB_uuid_get_loop_itemsrrJlen _run_loopr<getpop ignore_errorsr;r>_executerHcloseAttributeError Exceptionrrr!rDno_logr traceback format_exc)rKr=eZ item_resultsrbreZ item_ignoreZarrayr(rfr+runcs                  (*( (*zTaskExecutor.runcsh|j|jd<|j|jdvr:|jd|jt|j|jdd}|jd}|durh|}n|jjr0|jj|j j vrd}|jjdkrd}t |jj |dd}|s‡fd d |D}|j j j|jj|jd }d D]}||jj vrqqt|d |dt|j||jdd}ntd|jjn4|jj durd|jj }t|tsdtd||S)z Loads a lookup plugin to handle the with_* portion of a task (if specified), and returns the items result. Zansible_search_pathrO variablesNZ_ansible_loop_cacheTZ first_foundF)termstemplarZfail_on_undefinedZ convert_barecsg|]}|s|qSr()Z is_template)r)trr(r+r,r-z0TaskExecutor._get_loop_items..)rOr)templatevarfileZ_subdirs)rr~ZwantlistzSUnexpected failure in finding the lookup named '%s' in the available lookup pluginszInvalid data passed to 'loop', it requires a list, got this instead: %s. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup.)rBZget_search_pathrCrFZ get_basedirappendrrqZ loop_withrGZ lookup_loaderrZloopactionsetattrr!r|rrr;r>)rKr=Z loop_cacheZfailZ loop_termsZmylookupZsubdirr(rr+rns@     zTaskExecutor._get_loop_itemsc Cs$|j}t|j|d}|jjj|d|jjj}|jjj}|jjj}|jjj }|jjj }|jjj pjd|d} ||vrt d|j|fd} d} t|} g} t|D]p\}}||d<|||<|r||d<|||<|rn|d ||d k|d | k| | || |d d |d <|r||d d <z||d |d d<WntyJYn0|d d krn||d |d d<||_|r| rt|nd} z(|jjddd}|jj|_|j}WnHty}z.| tdt|dWYd}~qWYd}~n d}~00||j|_}||j|_}|j|d}|j}||j|_}||j|_}| pZ|j} |||<||d<|r|||<||d<|r|d |d <d|d<|d|d<z|| |d<Wn>t y}z$|!ddt|dWYd}~n d}~00t"|j#j$|jj%||d}|&s(|'r8|j()d|nV|*rR|j()d|n}|dur|j?}njj@}tA|jjjjBjjCjDjEd!j_|(d"}|durtFjj|j_jjGr2jjH}|durd#}n|d$kr(d%}n|d%7}nd%}jjI}|d$krLd%}td&d}tJd%|d%D]n}td'zz:jj4rtKKtKjLtM}tKNjj4j>jOd(}Wn>tPtQfy}zD|jRWYd}~Wjj4rtKNd$tKKtKjL|}j>SSd}~0tTy~}zNtdt|d)WYd}~Wjj4rftKNd$tKKtKjL|}j>SSd}~0tUy}z^d*jjjj4f}td|d WYd}~Wjj4rtKNd$tKKtKjL|}j>SSd}~00Wjj4rtKNd$tKKtKjL|}j>Sn.jj4rHtKNd$tKKtKjL|}j>S0td+tV|pp|(d,d|d,<jjtjWvrtX|}jjYrtZjjYst d-jjY|jjY<jj[d$krnjj\d$krf|(d.sf|(d/sfj]|d0}|(d/r>j^_d1t`j jajj9|jbd2n(j^_d3t`j jajj9|jbd2||d,<fd4d5}fd6d7}d8|v rDjjtjcv rDjjtjWv rjjrjjdrd9vrd9'|d8n |d8d9<n'|d8nt y.}z$d|d/<t||d>|<WYd}~n d}~00|d%krhthjd?}jjG|_i| rf qnp||krhd|d@<||dA<tdB||fj^_dCt`j jajj9|jbd2tjk|jlj*d _>qh|d%k r|d%|d<<d|d/<jjtjWv rtX|}jjY r&||jjY<d8|v rjjtjcv rjjtjWv r`|'|d8njjdur:jjr:tjd}jj|_||d<dS)NrOrT)rBZ changed_whenrrFwhenevaluate_conditional)rcondrKrZ vars_copyr(r+_evaluate_changed_when_results  z._evaluate_changed_when_resultcsDjjr._evaluate_failed_when_resultZ ansible_factsZ_ansible_delegated_varsrc)r0ZattemptsrTz%s_when_resultrZ_ansible_retryretrieszRetrying task, attempt %d of %dZv2_runner_retryZ_ansible_notifyZansible_delegated_host)Z ansible_hostZ ansible_portZ ansible_userrz'attempt loop complete, returning result)nrCrrFrrDZset_task_and_variable_overriderBr remote_addrrAaddressZ update_varsrrxrr_rlr<rJr8rZ delegate_tor;rrrrrrZ_ACTION_ALL_INCLUDE_TASKSargsrrrZ_ACTION_INCLUDE_ROLErwryrzZINJECT_FACTS_AS_VARSr`rrqrrHZ matches_name_get_connection_set_become_plugin_set_connection_optionsanyZsupports_persistenceZUSE_PERSISTENT_CONNECTIONSZforce_persistence get_optiontimeoutvvvvZ transportZ get_optionsstart_connectionrmrsys executable'_get_action_handler_with_module_contextZ_handlerZ resolved_fqcnZresolved_actionr Zmodule_defaultsrZ_play_action_groupsr7ZuntilrdelayrangesignalSIGALRMr5alarmr|r r rcleanupr r/boolZ_ACTION_WITH_CLEAN_FACTSr!registerr& async_valpoll_poll_async_resultrIrr rrZ _ACTION_DEBUGZdelegate_factsr%r"r#rrrr_get_action_handlernotify) rKr~Zcontext_validation_errorZtempvarsr{rxZraiseitrrZ include_fileZvariable_paramscvarscurrent_connectionZ plugin_varsoptions socket_pathZmodule_contextZmodule_defaults_fqcnr:rrrZattemptZold_sigrWrrafcondnamerrcZ requireshedr(rr+rts                                                         &            &             zTaskExecutor._executec CsB|dur|j}|d}|dur,tdddSttdd|i|jjd}|jjjd ||j |j |j ||jd }|jj }|d krt |jjzF|j|d }t|d d dksd|vr|ddsd|vrWqWnty\} zptdt| tdttz|j Wnty.Yn0||jj8}|d krHWYd} ~ qtd} ~ 00||jj8}|jdt|jj|j ||!dqtt|d d dkr|drtdd|jj |dStdd|dSnft|dd|jjd} |jjjd | |j |j |j ||jd } | j|d | j"dd|j"dd|SdS)z< Polls for the specified JID to be complete NZansible_job_idTz(No job id was returned by the async taskr async_statusjid)rr environmentzansible.legacy.async_status)rMrrNrOrrPrrfinishedr6rUZ_ansible_parsedFrSz-Exception during async poll, retrying... (%s)zAsync poll exception was: %sZv2_runner_on_async_pollrz;async task did not complete within the requested time - %ss)rUrW async_resultz'async task produced unparseable resultsr)rmode)rr)force)#rCrqr<rloadrBrrG action_loaderrHrDrFrrrrr|intrwr_rrrlryrzresetrvrIrr rArrmrr) rKrrrZ async_jidZ async_taskZ async_handlerZ time_leftrr{Z cleanup_taskZcleanup_handlerr(r(r+r8s                zTaskExecutor._poll_async_resultcCst|}|std||S)NzInvalid become method specified, could not find matching plugin: '%s'. Use `ansible-doc -t become -l` to list available plugins.)rrqr)rKrr'r(r(r+ _get_becomes  zTaskExecutor._get_becomecCsj||j_|jj}|jjj||j|j|jjtt d\}}|sLt d|| ||||j ||S)z Reads the connection property for the host, and returns the correct connection object from the list of connection plugins ) task_uuidZansible_playbook_pidz(the connection plugin '%s' was not found)rDrrGrZget_with_contextrErBrmrosgetppidrrZset_attributes_from_plugin)rKrrrZ conn_typerZplugin_load_contextr(r(r+rs   zTaskExecutor._get_connectioncCs|ddur"t||d}n|jj}|r^|drN|||d}qb||jj}nd}z||WntyYn0|rt |jddrt |ddst d|j |j f|j |j dS)NZansible_becomeZansible_become_methodZ require_ttyFZhas_ttyz`The '%s' connection does not provide a TTY which is required for the selected become plugin: %s.)rqrrrBr'rZ become_methodZset_become_pluginrvrrrrrD)rKrrrr'Z become_pluginr(r(r+rs(   zTaskExecutor._set_become_pluginc Cszt|jd|}Wnty2t|j|}Yn0|dkr@d}tj||j}i}|D]}||vrX|||||<qX|j||d|S)Nz_%s ssh_type_connr task_keysZ var_options) rrHrvrrrrr set_options) rKrr~rrZplugin option_varsrrcr(r(r+_set_plugin_optionss z TaskExecutor._set_plugin_optionsc Csg}tjd|jj}||dii}|D]}||vr,|||||<q,t|jddr|D]4}|d|jjr^||vr^||||d|<q^|j }|j j |d<|j j r|j j |d<|d=|jj||d ||d ||||jjdurp|j jr|j j|d <||d |||d D]8}zt|j ||jj|Wnty\Yn0q(|jjj|j _t|jdd} | dur| ddkrt| d} ||| |||t|jdd} | dur||d||||S)NrZ_extrasZ allow_extrasFz ansible_%s_rpasswordrrr become_passr')Z become_userZ become_flagsZ become_exerZ _sub_plugintypeZexternalobjr)rrrrHrextendrr startswithrBrrDrrrrr'rrrKeyErrorpromptrqr) rKr~rZvarnamesrrrcroptionsubrZsub_connr(r(r+rsH        z$TaskExecutor._set_connection_optionscCs|||dS)zX Returns the correct action plugin to handle the requestion task action r)r)rKrrr(r(r+rFsz TaskExecutor._get_action_handlerc Cs$|jjd\}}}|dd}|r4d||}n|}|jj}|jjj|jj|d} | j rb| j sfd} | durv| j } nn|jj j |jj|dr|jj} nNt |tjv|jj j ||dfr|} tjdj| |jjd|jjd nd } d}|jj j| |j||j|j||j|d } | std | | | fS) zo Returns the correct action plugin to handle the requestion task action and the module context ._rz{0}.{1})collection_listNz1Using network group action {handler} for {action})handlerrrzansible.legacy.normal)rMrrNrOrrPrzthe handler '%s' was not found)rBr rpartitionsplitformat collectionsrGZ module_loaderZfind_plugin_with_contextZresolvedZ action_pluginrZ has_pluginallrZNETWORK_GROUP_MODULESr_rrDrrqrFr) rKrrZmodule_collection separatorZ module_nameZ module_prefixZnetwork_actionrmoduleZ handler_namerr(r(r+rLsJ      z4TaskExecutor._get_action_handler_with_module_context)N)N)r0r1r2__doc__rQr|rnrprtrrrrrrrrr(r(r(r+r.Ls$h8 ) i$Fc Cstjptjtjdg}|tj dd tj |D]2}tj |d}tj |r:td|qvq:tdtj}|ttttj tjttttdg}tjr|dd tjtj }t!"\} } t#j$||g|t%t&t%|| t#j't#j'|d } t(| t)*| } t)*| } | d t)j+@| d <zHt),| t)j-| t.| |t.| |/| 0\}}Wt),| t)j-| nt),| t)j-| 0t(| | j1dkrt23t%|d d }nFzt23t%|d d }Wn.t4t2j5dt6y dt%|d d i}Yn0d|vr|dD]p\}}|dkrVtj|ddnL|dvrvt4t|||j7dn,t8t|rt4t||ntj||j7dq4d|vrtjdkr| drd|d}tj|tj9dt|d|dS)z* Starts the persistent connection rPATHrizansible-connectionz$Found ansible-connection at path {0}ziUnable to find location of 'ansible-connection'. Please set or check the value of ANSIBLE_CONNECTION_PATH)ZANSIBLE_BECOME_PLUGINSZANSIBLE_CLICONF_PLUGINSZANSIBLE_COLLECTIONS_PATHZANSIBLE_CONNECTION_PLUGINSZANSIBLE_HTTPAPI_PLUGINSZANSIBLE_NETCONF_PLUGINSZANSIBLE_TERMINAL_PLUGINSz-%sr8)stdinrkstderrenvrr]rZZJSONDecodeErrorerrormessageslogT)Zlog_only)rlr8ZvvZvvvrZvvvvvZvvvvvvrrjzThe full traceback is: )Zcolorr):rZANSIBLE_CONNECTION_PATHrpathdirnamerargvrenvironrqrpathsepjoinisfiler_rrrrrrZ print_pathsrrrZcollection_pathsrrrr verbosityrrptyopenpty subprocessPopenrrPIPErutermiosZ tcgetattrZICANONZ tcsetattrZTCSANOWrZ serializeZ communicate returncodejsonloadsrdecoder ValueErrorrhasattrZ COLOR_ERROR)rNrrZcandidate_pathsrrrrZpythonZmasterZslavepoldnewrkrrlevelmessagerWr(r(r+rsx         $           r)PZ __future__rrrrZ __metaclass__rrrr#rrrr!ryZansiblerrZansible.errorsrrrr r r Zansible.executor.task_resultr Zansible.executor.module_commonr Z)ansible.module_utils.parsing.convert_boolrZansible.module_utils.sixrZansible.module_utils._textrrZansible.module_utils.connectionrZansible.playbook.conditionalrZansible.playbook.taskrZansible.pluginsrZansible.plugins.loaderrrrrrrZansible.templaterZansible.utils.collection_loaderrrZansible.utils.listifyrZansible.utils.unsafe_proxyr r!Zansible.vars.cleanr"r#Zansible.utils.displayr$Zansible.utils.varsr%r&r_ZMAGIC_VARIABLE_MAPPINGr=Z RETURN_VARS__all__ BaseExceptionr/r5r7r.rr(r(r(r+sZ              >