o nnh~@sJddlmZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZddlmZmZmZmZdd lmZdd lmZdd lmZdd lmZdd lmZmZmZm Z ddl!m"Z"m#Z#ddl$m%Z%m&Z&ddl'm(Z(ddl)m*Z*ddl+m,Z,m-Z-m.Z.e(Z/ddZ0GdddZ1GdddZ2Gddde2Z3dS)) annotationsN)copy)cache)UndefinedError) constants)context) AnsibleErrorAnsibleParserErrorAnsibleUndefinedVariableAnsibleAssertionError) string_types)boolean)to_text) DataLoader) AttributeFieldAttributeConnectionFieldAttributeNonInheritableFieldAttribute) module_loader action_loader)_get_collection_metadataAnsibleCollectionRef)DisplaySentinel) combine_vars isidentifier get_unique_idc Cstdttfddi}g}tj}t|tod|vot|dk}|r4|s4dtt|}| dj |dno|r|r=| d t|dtsP| d j |dd nSt |d t | }|rk| d d|g} | D]$\} } | |dvr|qq|d| } t| | d s| d| | | dfqq| r| d d| |r|dd |td|dSdS)N extend_grouplist)types errortypemetadata, z7The only expected key is metadata, but got keys: {keys})keysz-The group contains multiple metadata entries.z0The metadata is not a dictionary. Got {metadata})r"z*The metadata contains unexpected keys: {0}r z%s is %s (expected type %s)r!z/The metadata contains unexpected key types: {0}rzNInvalid metadata was found for action_group {0} while loading module_defaults. )rr CVALIDATE_ACTION_GROUP_METADATA isinstancedictlenjoinsortedappendformatsetr%itemsinsertdisplaywarning) actionfound_group_metadata fq_group_namevalid_metadatametadata_warningsvalidate metadata_only found_keysunexpected_keysunexpected_typesfield requirementvaluerB@/usr/local/lib/python3.10/dist-packages/ansible/playbook/base.py_validate_action_group_metadata!sB   rDc@seZdZddZdddZdS)_ClassPropertycCs ||_dSN)name)selfownerrGrBrBrC __set_name__Is z_ClassProperty.__set_name__NcCst|d|jS)N_)getattrrG)rHobjobjtyperBrBrC__get__Lsz_ClassProperty.__get__rF)__name__ __module__ __qualname__rJrOrBrBrBrCrEHsrEc@seZdZeZeeddZddZe ddZ d;dd Z d d Z dd d!Zd>d"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd?d1d2Zd3d4Zd5d6Z d7d8Z!d9d:Z"d S)@FieldAttributeBasecCsbi}t|jD]'}t|jD]\}}t|tsq|||<|jr-t||j||||j<qq|SrF) reversed__mro__r__dict__r1r)raliassetattr)cls fattributes class_objrGattrrBrBrC _fattributesTs  zFieldAttributeBase._fattributescCs2d|_d|_d|_d|_d|_t|_t|_dS)NF) _loader_variable_manager _validated _squashed _finalizedr_uuidr*varsrHrBrBrC__init__bs zFieldAttributeBase.__init__cC|jSrF)rbrerBrBrC finalizedtszFieldAttributeBase.finalizedrcCs|dkr tdtdd||jj|t|ft|dr=|jr=|j|d|j}|r=|D] }||dq3t|drO|j rQ|j |ddSdSdS) zf this is never called from production code, it is here to be used when debugging as a 'complex print' rzEDUMPING OBJECT ------------------------------------------------------z%s- %s (%s, id=%s)r&_parent_playN) r3debug __class__rPidhasattrridump_me get_dep_chainrk)rHdepth dep_chaindeprBrBrCrpxs " zFieldAttributeBase.dump_mecCs|S)zD infrequently used method to do some pre-processing of legacy terms rB)rHdsrBrBrCpreprocess_datasz"FieldAttributeBase.preprocess_dataNcCs|dur td|t|d|||_|dur||_nt|_||}||t|j t ddD]&\}}||vr[t |d|d}|rSt||||||q5t||||q5| |S)z4 walk the input datastructure and assign any values Nz%ds (%s) should not be None but it is._dsr#)keyz_load_%s)r rXr_r^rrv_validate_attributesr-rZr1operator itemgetterrLr:)rHruvariable_managerloaderrGr\methodrBrBrC load_datas$     zFieldAttributeBase.load_datacCs"zt|dWStyYdSw)Nrw)rLAttributeErrorrerBrBrCget_dss   zFieldAttributeBase.get_dscCrgrF)r^rerBrBrC get_loaderzFieldAttributeBase.get_loadercCrgrF)r_rerBrBrCget_variable_managerrz'FieldAttributeBase.get_variable_managercCsJ||}td}|r#t|tr#||vr#td|d|f|d|S)N)always on_failedon_unreachable on_skippedneverz9'%s' is not a valid value for debugger. Must be one of %sr$rM)template frozensetr)r r r,r)rHr\rAtemplar valid_valuesrBrBrC_post_validate_debuggers z*FieldAttributeBase._post_validate_debuggercCs8t|j}|D]}||vrtd||jjf|dqdS)z} Ensures that there are no keys in the datastructure which do not map to attributes for this object. z&'%s' is not a valid attribute for a %srN)rrZr rmrP)rHru valid_attrsrxrBrBrCrys z'FieldAttributeBase._validate_attributescCs|durin|}|jsP|jD]?\}}t|d|d}|r(|||t||qt|d|t}|durO|jdkrOt|ttfrOt d|t |f| dqd|_dS)z6 validation that is done at parse time, not load time Nz _validate_%srKstringz[The field '%s' is supposed to be a string type, however the incoming data structure is a %srT) r`rZr1rLrisar)rr*r typer)rHall_varsrG attributer~rArBrBrCr:s"  zFieldAttributeBase.validatec Cs|durdSt|ts|g}g}|D]j}t|tstdi}|D]S\}}|drH|dd}|jdur?||\}} d|}|||<q#t |ddkrUd|}| |} | r`||| <|drv|j | dddd } | rv||| <q#| |q|S) NzThe field 'module_defaults' is supposed to be a dictionary or list of dictionaries, the keys of which must be static action, module, or group names. Only the values may contain templates. For example: {'ping': "{{ ping_defaults }}"}zgroup/.zansible.legacy.zansible.builtin.F mandatory) r)rr*r r1 startswithsplitplay_resolve_groupr+_resolve_actionreplacer.) rHrGrAvalidated_module_defaults defaults_dictvalidated_defaults_dictdefaults_entrydefaults group_namedummyresolved_actionrBrBrC_load_module_defaultssB         z(FieldAttributeBase._load_module_defaultscCsJt|dr |j}nt|drt|jdr|jj}n|}|jjdkr#dS|S)NrkriPlay)rorkrirmrP)rHrrBrBrCr s   zFieldAttributeBase.playTcCs^t|sd}|d|}n d|ddd}||jjvr(||jj|fSz t|di}WntyN|sHt d||gfYSt d|w||dd}||||}|durx|srt d||gfSt d|g}g}d} |D]G} t | t st| | |t | d trd } | d d g}t |t r|g}t |tsg}qt| s|d| } |j| dd } | r|| q|D]} | |jjvrg|jj| <|jj| |q||jj|<|D]@} t| s|d| } |j| dd \} }|D]} | |jjvrg|jj| <|jj| |q|jj||||q||fS) Nzansible.builtinrrrj action_groupszMError loading module_defaults: could not resolve the module_defaults group %srFr"Trr)r is_valid_fqcrr,rr_group_actionsrget ValueErrorr3vvvvvr r)r rDr*rrr._action_groupsrextend)rHr7rcollection_namer resource_name action_groupresolved_actionsinclude_groupsr6r5r include_groupr group_actionsrBrBrCr/sx                     z!FieldAttributeBase._resolve_groupcCsjt|}|jr|jst|}|jr|}n|jst|}|jr$|jS|r,td|td|dS)Nz.Could not resolve action %s in module_defaults) rfind_plugin_with_contextresolved action_pluginr resolved_fqcnr r3r)rH action_namerrpreferrBrBrCrs     z"FieldAttributeBase._resolve_actioncCs2|js|jD] }t||t||qd|_dSdS)z Evaluates all attributes and sets them to the evaluated version, so that all future accesses of attributes do not need to evaluate parent attributes. TN)rarZrXrL)rHrGrBrBrCsquashs   zFieldAttributeBase.squashc Csz|}Wnty}ztd|dd}~ww|jD]}t||tt|d|tq|j|_|j |_ |j |_ |j |_ |j |_ t |drK|j|_|S)z= Create a copy of this object and return it. zTExceeded maximum object depth. This may have been caused by excessive role recursion)orig_excNrKrw)rm RuntimeErrorrrZrX shallowcopyrLrr^r_r`rbrcrorw)rHnew_meerGrBrBrCrs      zFieldAttributeBase.copycCs|jdkr t|}|S|jdkrt|}|S|jdkr!t|}|S|jdkr.t|dd}|S|jdkrHt|trBd|vrB|dd }t|}|S|jd kr|durTg}nt|ts\|g}|j dur|D]6}t||j s|t d ||j |t |f| d |j r|j tkr|dus|d krt d |f| d qc|S|jdkr|durt}nt|ttfst|tr|d}n|g}t|tst|}|S|jdkr|durt}|St|tstd||S|jdkrt||jstd||jt |f|j|d|Std|j)NrintfloatboolT)strictpercent%rz@the field '%s' should be a list of %s, but the item '%s' is a %srz8the field '%s' is required, and cannot have empty valuesr0,r*z%s is not a dictionaryclassz'%s is not a valid %s (got a %s instead))rz!Unknown value for attribute.isa: )rrrrr r)r rrlistofr rrrequiredstripr0rr* TypeError class_type post_validater )rHrGrrAritemrBrBrCget_validated_valuest 1 / - +  %            z&FieldAttributeBase.get_validated_valuecCsb|j|}t|trt||tdSzt|||j|ddWdSty0t||tYdSw)z9 set to parent inherited value or Sentinel as appropriateT)omitN)rZr)rrXr_get_parent_attributer)rHrGrrBrBrCset_to_contexts   z!FieldAttributeBase.set_to_contextc Cs|jd}|jD]\}}|jr*t||}|dvr)||r)td||fq t||dur;|j s5q t d||j sE|j j dvrEq zJt|d|d}|rZ||t|||}n|jdkret||}n|t||}|dur|||kr|||Wq |dur|||||}t|||Wq ttfy}zt||}t d |d |d |jd ||d d}~wttfy}z#|jr|dkr|dkrd}nd|d}t |||d WYd}~q d}~wwd|_dS)z we can't tell that everything is of the right type until we have all the variables. Run basic types (from isa) as well as any _post_validate_ functions. r)rdz]"%s" is not templatable, but we found: %s, it will not be templated and will be used "as is".Nz*the field '%s' is required but was not set)TaskHandler PlayContextz_post_validate_%srz the field 'z' has an invalid value (z!), and could not be converted to rrMrrGargsz7The task includes an option with an undefined variable.z The field 'z=' has an invalid value, which includes an undefined variable.T)available_variablesrrZr1staticrL is_templater3r4rr always_post_validatermrPrrrrrXrrrr r_fail_on_undefined_errorsrb) rHr omit_valuerGrrAr~rmsgrBrBrCrsV         z FieldAttributeBase.post_validatec Csdd}zNt|tr||t|j|WSt|trJt|dd}tjd|d|dfdd |j}|D]}t|ts=t||t||}q4|WS|d urQiWSttyi}z t d |j j ||d d }~wt y}z t d |j j |f||d d }~ww)z Vars in a play can be specified either as a dictionary directly, or as a list of dictionaries. If the later, this method will turn the list into a single dictionary. cSs"|D] }t|std|qdS)Nz!'%s' is not a valid variable name)rr)rurxrBrBrC_validate_variable_keysFs  z>FieldAttributeBase._load_vars.._validate_variable_keys ansible_pos)unknownrzSpecifying a list of dictionaries for vars is deprecated in favor of specifying a dictionary. Error occurred in the file: %s, line: %drr#z2.18)versionNz.Vars in a %s must be specified as a dictionaryrz2Invalid variable name in vars specified for %s: %s) r)r*rrdrrLr3 deprecatedrr rmrPr)rHr\rur line_filerrrrBrBrC _load_vars?s@      zFieldAttributeBase._load_varsFcCsft|ts|g}t|ts|g}dd|D}dd|D}|r%||}n||}ddt|DS)z Will extend the value given with new_value (and will turn both into lists if they are not so already). The values are run through a set to remove duplicate values. cSg|]}|tur|qSrBr.0vrBrBrC xz4FieldAttributeBase._extend_value..cSrrBrrrBrBrCryrcSsg|] \}}|dur|qSrFrB)rirrBrBrCr)r)r itertoolsgroupby)rHrA new_valueprependcombinedrBrBrC _extend_valueis   z FieldAttributeBase._extend_valuecCsPi}|jD]\}}t||}|jdkr!t|dr!|||<q|||<q|S)z6 Dumps all attributes to a dictionary r serialize)rZr1rLrror)rHattrsrGrr\rBrBrC dump_attrss  zFieldAttributeBase.dump_attrscCsv|D].\}}||jvr2|j|}|jdkr,t|tr,|}||t|||qt|||qd|_d|_ dS)z4 Loads attributes from a dictionary rTN) r1rZrr)r*r deserializerXrbra)rHrr\rArrMrBrBrC from_attrss     zFieldAttributeBase.from_attrscCs*|}|j|d<|j|d<|j|d<|S)a0 Serializes the object derived from the base object into a dictionary of values. This only serializes the field attributes for the object, so this may need to be overridden for any classes which wish to add additional items not stored as field attributes. uuidrhsquashed)rrcrbra)rHreprrBrBrCrs    zFieldAttributeBase.serializecCst|tstd|t|f|jD]\}}||vr%t||||q||qt|d|d|dd|_ |dd|_ dS)z Given a dictionary of values, load up the field attributes for this object. As with serialize(), if there are any non-field attribute data members, this method will need to be overridden and extended. z&data (%s) should be a dict but is a %srcrrhFrN) r)r*r rrZr1rXrrrbra)rHdatarGrrBrBrCrs  zFieldAttributeBase.deserialize)r)NNrF)T)F)#rPrQrRrErZ classmethodrr]rfpropertyrhrprvrrrrrryr:rrrrrrrrrrrrrrrrBrBrBrCrSPsB     * 7   U 5E *  rSc@sjeZdZeddddZededdZe ddZ e ded dZ ed d dd Z e d dddZ e d dddZe dejdZe ddZe ddZe ddZe deddZe deddZe dejdZe dddZe dejdZe ddZe deddZe deddZe deddZe deddZ e deddZ!gZ"ddZ#ddZ$ddZ%dS)BaserrT)rdefaultr connection)rrr)r remote_userr*d)rpriorityrr)rrrrcheckdiffrbecome become_method become_user become_flags become_exec Csjd}z d|jj|jjf}W|Sty4zd|jjjj|jjjjf}WY|Sty3YY|Sww)zE return the absolute path of the playbook object and its line number rz%s:%s)rw _data_source _line_numberrrirk)rHpathrBrBrCget_paths   z Base.get_pathcCst|dr |jr |jSdS)Nri)rorirqrerBrBrCrqs zBase.get_dep_chaincCsNg}|}|r|tdd|Dtj|}||vr%|||S)z Return the list of paths you should search for files, in order. This follows role/playbook dependency chain. cSsg|] }t|dr|jqS) _role_path)ror)rxrBrBrCrrz(Base.get_search_path..)rqrrTosrdirnamerr.)rH path_stackrstask_dirrBrBrCget_search_path s zBase.get_search_pathN)&rPrQrRrrGrrcliargs_deferred_getrrportr rdmodule_defaults environmentr'DEFAULT_NO_LOGno_logrun_once ignore_errorsignore_unreachable check_moder ANY_ERRORS_FATALany_errors_fatalthrottle TASK_TIMEOUTtimeoutdebuggerrrrrrDEPRECATED_ATTRIBUTESrrqrrBrBrBrCrs6       r)4 __future__rrrzrrr functoolsrjinja2.exceptionsransiblerr'ransible.errorsrr r r ansible.module_utils.sixr )ansible.module_utils.parsing.convert_boolr +ansible.module_utils.common.text.convertersransible.parsing.dataloaderransible.playbook.attributerrrransible.plugins.loaderrr2ansible.utils.collection_loader._collection_finderrransible.utils.displayransible.utils.sentinelransible.utils.varsrrrr3rDrErSrrBrBrBrCs<            '