a )gR@spddlmZmZmZeZddlZddlZddlm Z ddl m Z m Z m Z ddlmZddlmZddlmZddlmZmZmZmZmZmZdd lmZdd lmZm Z dd l!m"Z"dd l#m$Z$m%Z%dd l&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/ddl0m1Z1m2Z2m3Z3ddl4m5Z5ddl6m7Z7m8Z8ddl9m:Z:m;Z;e-ZGddde Z?dS))absolute_importdivisionprint_functionN) defaultdict)MappingMutableMappingSequence)sha1)UndefinedError) constants) AnsibleErrorAnsibleParserErrorAnsibleUndefinedVariableAnsibleFileNotFoundAnsibleAssertionErrorAnsibleTemplateError)Host) sort_groupsget_group_vars)to_text) text_type string_types) lookup_loader) FactCache)Templar)Display)listify_lookup_plugin_terms) combine_varsload_extra_varsload_options_vars)wrap_var)namespace_facts clean_facts)get_vars_from_inventory_sourcesget_vars_from_pathcCsN|dur dSt|ts|g}n|}|D]"}t|ts&td|t|fq&|S)z Ensures that vars contained in the parameter passed in are returned as a list of dictionaries, to ensure for instance that vars loaded from a file conform to an expected state. Nzevariable files must contain either a dictionary of variables, or a list of dictionaries. Got: %s (%s)) isinstancelistrr type)adataitemr+8/usr/lib/python3.9/site-packages/ansible/vars/manager.pypreprocess_vars2s  r-c @seZdZegdZdddZddZddZed d Z d d Z dddZ dddZ ddZ ddZddZddZddZdS) VariableManager)Zplugins_by_groupgroups_plugins_playgroups_plugins_inventorygroups_inventoryall_plugins_playall_plugins_inventory all_inventoryNc Cstt|_tt|_tt|_tt|_tt|_||_||_d|_ dt t d |_t||_|jdd}t|dup||_t|jd|_z t|_Wn8ty}z tt|i|_WYd}~n d}~00dS)N__omit_place_holder__%s@basedirFloader)rdict_nonpersistent_fact_cache _vars_cache _extra_vars_host_vars_files_group_vars_files _inventory_loader _hostvarsr osurandom hexdigest _omit_tokenr _options_varsgetbool safe_basedirrr _fact_cacher displaywarningr)selfr9 inventory version_infor7er+r+r,__init__Ls$       zVariableManager.__init__c Cs4t|j|j|j|j|j|j|j|j|j |j d }|S)N) fact_cache np_fact_cache vars_cache extra_varshost_vars_filesgroup_vars_files omit_token options_varsrOrJ) r:rKr;r<r=r>r?rFrGr@rJrNr)r+r+r, __getstate__js zVariableManager.__getstate__cCs|dtt|_|dtt|_|dtt|_|dt|_|dtt|_|dtt|_|ddt t d  |_ |d d|_|d t|_|d d |_d|_d|_dS)NrSrTrUrVrWrXrYr5r6rOrZrJF)rHrr:rKr;r<r=r>r?r rCrDrErFr@rGrJrArBr[r+r+r, __setstate__ys zVariableManager.__setstate__cCs|jSN)r=rNr+r+r,rVszVariableManager.extra_varscCs ||_dSr^)r@)rNrOr+r+r, set_inventoryszVariableManager.set_inventoryTtaskc " s<tdt} j|||||||d} ifddgjrNjg|rv|D]} | | d| j } qZ|rt j dkr| n2t j dvr| dgnt j d krt d t j |jd ur|s|jt jvr| |jj|d d|jj } |rjjdtd d|Dfdd} fddfddfdd}fdd}fdd}fdd}fdd}fdd }fd!d"}t jD]N}|jvrtd#||j f| t|d$|} ntd%|q| |d&|} | |gd'|} | |gd(|} z^tj|j i}| t|O} t jr| tt |d)} n| td*|d*iid)} Wnt!yYn0|r| |d+} |"}z|D]}t#| j$}t#|| }t%j|d,}|}t&|t's$|g}z|D]}|(|}t&|t)sPt d-|zd| }j*|d.|}}t+jj,|d/d0d1} | d ur| D]}!| |!d2|} qWqWn.t-yYq*Ynt.yYn0q*|r|rt-d3|Wnlt/t0fyn|d urVj|j td4rV|d urVt0d5||d6nt1d7|YqYn0t1d8|qWn(t2yt.d9|t3|fYn0t j4s|D] } | | jd0d:d;| j } q|r"|jr| |jj|d0d:d;|jj } | |d<} |r^| j5|6td=} | j7|j td>} |r|jr| |j8|d?|jj } | |9d@} | j$dA} | | dB} |r|j:| dC<|s|r| ;| d.<|r|r|jr4t?@| S| Sd S)Gan Returns the variables, with optional "context" given via the parameters for the play, host, and task (which could possibly result in different sets of variables being returned due to the additional context). The order of precedence is: - play->roles->get_default_vars (if there is a play context) - group_vars_files[host] (if there is a host context) - host_vars_files[host] (if there is a host context) - host->get_vars (if there is a host context) - fact_cache[host] (if there is a host context) - play vars (if there is a play context) - play vars_files (if there's no host context, ignore file names that cannot be templated) - task->get_vars (if there is a task context) - vars_cache[host] (if there is a host context) - extra vars ``_hosts`` and ``_hosts_all`` should be considered private args, with only internal trusted callers relying on the functionality they provide. These arguments may be removed at a later date without a deprecation period and without warning. zin VariableManager get_vars())playhostrainclude_hostvarsinclude_delegate_to_hosts _hosts_allcs"tjr|D] }||<q t||S)z Wrapper function to update var sources dict and call combine_vars() See notes in the VarsWithSources docstring for caveats and limitations of the source tracking )C DEFAULT_DEBUGr)r)Znew_datasourcekey) _vars_sourcesr+r,_combine_and_tracks z4VariableManager.get_vars.._combine_and_trackzrole '%s' defaultsall)Zbottom playbook_dirrtopzUnknown playbook vars logic: %sN)Z dep_chaincSsg|]}|jdvr|qS))rnname).0gr+r+r, z,VariableManager.get_vars..c si}z|j||}Wntyz:|D]0}t|trL|||jO}q,|||jO}q,WnFtyt|drt d|j |j fnt d|j |j fYn0Yn0|S)Nrunz)Cannot use v1 type vars plugin %s from %szInvalid vars plugin %s from %s) get_varsrAAttributeErrorr%rZ get_host_varsrrrhasattrr Z _load_nameZ_original_path)Zpluginpathentitiesr)Zentityr_r+r,_get_plugin_varss     z2VariableManager.get_vars.._get_plugin_varscstjjj|S)z) merges all entities by inventory source )r#rAr@Z_sources)r|)rNstager+r,_plugins_inventorysz4VariableManager.get_vars.._plugins_inventorycs.i}D] }|tj||d|}q|S)z& merges all entities adjacent to play z path '%s')r$rA)r|r)r{)rmbasedirsrNr~r+r, _plugins_playsz/VariableManager.get_vars.._plugins_playcsSr^)rxr+) all_groupr+r,r4sz/VariableManager.get_vars..all_inventorycs gSr^r+r+)rrr+r,r3sz7VariableManager.get_vars..all_plugins_inventorycs gSr^r+r+)rrr+r,r2sz2VariableManager.get_vars..all_plugins_playcstS)z gets group vars from inventory )rr+) host_groupsr+r,r1 sz2VariableManager.get_vars..groups_inventorycsS)z/ gets plugin sources from inventory for groups r+r+)rrr+r,r0sz:VariableManager.get_vars..groups_plugins_inventorycsS)z* gets plugin sources from play for groups r+r+)rrr+r,r/sz5VariableManager.get_vars..groups_plugins_playcsJi}D]<}|||d|||<|||d|||<q|S)z merges all plugin sources by group, This should be used instead, NOT in combination with the other groups_plugins* functions zinventory group_vars for '%s'zplaybook group_vars for '%s'r+)r)group)rmrrrr+r,plugins_by_groupss z3VariableManager.get_vars..plugins_by_groupszCalling %s to load vars for %sz!group vars, precedence entry '%s'z.Ignoring unknown variable precedence entry: %szhost vars for '%s'zinventory host_vars for '%s'zplaybook host_vars for '%s'factsZ ansible_localz play varsr9 variableszInvalid vars_files entry found: %r vars_files entries should be either a string type or a list of string types after template expansionvarsTF)Zunsafecachezplay vars_files from '%s'zvars file %s was not foundZ module_setupzTan undefined variable was found when attempting to template the vars_files item '%s')objz4skipping vars_file '%s' due to an undefined variablezRead vars_file '%s'zXError while reading vars files - please supply a list of file names. Got '%s' of type %s)Zinclude_paramszrole '%s' varsz task varsZ include_varsZset_factzrole '%s' paramszinclude paramsz extra varsz magic vars environmentZansible_delegated_vars_ansible_loop_cachezdone with get_vars())ArLdebugr:_get_magic_variablesrJrA get_basedirZ get_rolesZget_default_varsrrrhZPLAYBOOK_VARS_ROOTget_search_pathr _roleactionZ_ACTION_INCLUDE_ROLEZ get_dep_chainr@groupsrHrZ get_groupsZVARIABLE_PRECEDENCE_ALLOWEDlocalsrMrxr rKr!ZINJECT_FACTS_AS_VARSr"KeyErrorZget_vars_filesrr=rr%r&templaterZpath_dwim_relative_stackr-Zload_from_filerr r rZvvv TypeErrorr'ZDEFAULT_PRIVATE_ROLE_VARSr<get_namer;Zget_role_paramsZget_include_paramsrcopy delegate_to_get_delegated_varsriVarsWithSourcesnew_vars_with_sources)"rNrbrcrardreZ use_cacherfrgr~Zall_varsZmagic_variablesZroler}r4r3r2r1r0r/rentryrZ vars_filesZvars_file_itemZ temp_varstemplarZvars_file_listZ vars_fileZplay_search_stackZ found_fileZ real_filer)r*r+) rmrrrlrrrrNr~r,rxs                            .           zVariableManager.get_varscsi}tj|j|d<tj|d<tj|d<rt ddj D} ddj D} t t | | |d<| |d <| |d <|d |d < |d <|r|j r|j j d d|d<|j j|d<t|j j|d<|j j|d<|j |d<|jdur|j|d<rt|jd} js,| jr,d} n jp6d} |sXdd|jj| ddD}|srdd|jD}|dd|d<fdd|dD|d<fdd|D|d <|d |d!<|j|d"<|jD]\} }||| <q|jdur|r|j|d#<|S)$z Returns a dictionary of so-called "magic" variables in Ansible, which are special variables we set internally for use. roZansible_playbook_pythonZansible_config_filecSs"h|]}|D] }|qqSr+)Zget_all_dependenciesr)rsrdr+r+r, rvz7VariableManager._get_magic_variables..cSsg|] }|qSr+)r)rsrr+r+r,rurvz8VariableManager._get_magic_variables..Zansible_role_namesZansible_play_role_namesZansible_dependent_role_namesZ role_namesZansible_play_nameF)Zinclude_role_fqcnZ role_nameZ role_pathZ role_uuidZansible_collection_nameZansible_role_nameNrr8rncSsg|] }|jqSr+rqrshr+r+r,rurvT)patternignore_restrictionscSsg|] }|jqSr+rqrr+r+r,rurvZansible_play_hosts_allcsg|]}|jvr|qSr+Z_removed_hostsrsxrbr+r,rurvZansible_play_hostscsg|]}|jvr|qSr+rrrr+r,rurvZansible_play_batchZ play_hostsZomitZhostvars)rCr{abspathrArsys executablerhZ CONFIG_FILEr&ZrolessetrrZ _role_pathrZ_uuidZ_role_collectionr@Zget_groups_dictrZ finalized is_templatehosts get_hostsrFrGitemsrB)rNrbrcrardrerfrgrZdependency_role_namesZplay_role_namesrroptionZ option_valuer+rr,rsP             z$VariableManager._get_magic_variablescst|dsidfS|}||d<|j|dvrL|d|jt|j|dg}d}|jdur4|jtvr$d}|jdkrd}zzt |j |dd}|sfdd |D}tj |j|jd } d D]} | |j vrqqt | d | d t| j||d}Wnty dg}Yn0ntd|jnD|j durnz|j }Wntyjdg}Yn0n d}dg}t} t|jdd} d} |D]}|dur||| <|_j|jdd}||jkrd} |durtd|jdt|ts tdt||jd|| vrqd}|jdurz|j|}|dur|jjdddD]}|j|krP|}qqPt |d}n t |d}|j!|||ddd| |<| d| |d<qd}|r| r|}| |fS)NloopZansible_search_pathrTZ first_foundF)termsrfail_on_undefinedZ convert_barecsg|]}|s|qSr+)r)rstrr+r,ru,rvz7VariableManager._get_delegated_vars..)r9r)rvarfileZ_subdirs)rrzDFailed to find the lookup named '%s' in the available lookup pluginsZloop_varr*)rz$Undefined delegate_to host for task:)messagerz^the field 'delegate_to' has an invalid type (%s), and could not be converted to a string type.)Z ignore_limitsrrq)rbrcrarerdZinventory_hostname)"rzrrrArappendrZ loop_withrrrrHrsetattrr rwrr rr:getattrZ loop_controlZavailable_variablesrZ_dsr%rr'r@Zget_hostraddressrrx)rNrbraZexisting_variablesZ vars_copyrZhas_loopZfailZ loop_termsZmylookupZsubdirZdelegated_host_varsZitem_varZ cache_itemsr*Zdelegated_host_nameZdelegated_hostrrr+rr,rs                      z#VariableManager._get_delegated_varscCs|j|ddS)z- Clears the facts for a host N)rKpop)rNhostnamer+r+r, clear_factsszVariableManager.clear_factscCstt|tstdt|z|j|}Wnty>|}Yn(0t|ts^td|t|||O}||j|<dS)O Sets or updates the given facts for a host in the fact cache. zIthe type of 'facts' to set for host_facts should be a Mapping but is a %szCThe object retrieved for {0} must be a MutableMapping but was a {1}N) r%rrr'rKrrrformat)rNrcrZ host_cacher+r+r,set_host_factss    zVariableManager.set_host_factscCsRt|tstdt|z|j||O<WntyL||j|<Yn0dS)rzRthe type of 'facts' to set for nonpersistent_facts should be a Mapping but is a %sN)r%rrr'r;r)rNrcrr+r+r,set_nonpersistent_factss   z'VariableManager.set_nonpersistent_factscCsp||jvrt|j|<||j|vr^t|j||tr^t|tr^t|j|||i|j|<n||j||<dS)z< Sets a value in the vars_cache for a host. N)r<r:r%rr)rNrcZvarnamevaluer+r+r,set_host_variables   ,z!VariableManager.set_host_variable)NNN) NNNTTTNNra)NN)__name__ __module__ __qualname__ frozensetrrRr\r]propertyrVr`rxrrrrrrr+r+r+r,r.Gs&    3 H r.c@sdeZdZdZddZeddZddZdd Zd d Z d d Z ddZ ddZ ddZ ddZdS)ra" Dict-like class for vars that also provides source information for each var This class can only store the source for top-level vars. It does no tracking on its own, just shows a debug message with the information that it is provided when a particular var is accessed. cOst|i||_i|_dS)z Dict-compatible constructor N)r:r)sources)rNargskwargsr+r+r,rRszVarsWithSources.__init__cCs||}||_|S)z@ Alternate constructor method to instantiate class with sources )r)clsr)rvr+r+r,rsz%VarsWithSources.new_vars_with_sourcescCs|j|dSr^)rrHrNrkr+r+r, get_sourceszVarsWithSources.get_sourcecCs*|j|}td||j|df|S)Nzvariable '%s' from source: %sunknown)r)rLrrrH)rNrkvalr+r+r, __getitem__s zVarsWithSources.__getitem__cCs||j|<dSr^r))rNrkrr+r+r, __setitem__szVarsWithSources.__setitem__cCs |j|=dSr^rrr+r+r, __delitem__szVarsWithSources.__delitem__cCs t|jSr^)iterr)r_r+r+r,__iter__szVarsWithSources.__iter__cCs t|jSr^)lenr)r_r+r+r,__len__szVarsWithSources.__len__cCs |j|Sr^)r) __contains__rr+r+r,rszVarsWithSources.__contains__cCst|j|jSr^)rrr)rrr_r+r+r,rszVarsWithSources.copyN)rrr__doc__rR classmethodrrrrrrrrrr+r+r+r,rs r)@Z __future__rrrr'Z __metaclass__rCr collectionsrcollections.abcrrrZhashlibr Zjinja2.exceptionsr Zansibler rhZansible.errorsr r rrrrZansible.inventory.hostrZansible.inventory.helpersrrZansible.module_utils._textrZansible.module_utils.sixrrZansible.plugins.loaderrZansible.vars.fact_cacherZansible.templaterZansible.utils.displayrZansible.utils.listifyrZansible.utils.varsrrrZansible.utils.unsafe_proxyr Zansible.vars.cleanr!r"Zansible.vars.pluginsr#r$rLr-r.rr+r+r+r,s<             |