o nnhח@spddlmZddlZddlZddlmZddlmZmZm Z ddl m Z ddl m Z ddlmZddlmZmZmZmZmZmZdd lmZdd lmZmZdd lmZdd lm Z m!Z!dd l"m#Z#ddl$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-m.Z.m/Z/ddl0m1Z1ddl2m3Z3m4Z4ddl5m6Z6m7Z7ddl8m9Z9e)Z:ddZ;GdddZr?rFrGr@rJrNr(r*r*r+ __getstate__is 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?rrCrDrErFr@rGrJrArBr[r*r*r+ __setstate__xs  zVariableManager.__setstate__cCs|jSN)r=rNr*r*r+rVszVariableManager.extra_varscCs ||_dSr^)r@)rNrOr*r*r+ set_inventory zVariableManager.set_inventoryTFtaskc ! stdt} j||||||d} ifddgjr&jg|r<|jD]} | jr;| | d| j } q+|rt j dkrH| nt j dvrU| dgn t j d kratd t j |jd ur|sn|jt jvr| |jj |d d|jj } |r^jjdtd d|Dfdd} fddfddfdd}fdd}fdd}fdd}fdd}fdd }fd!d"}t jD]%}|jvrtd#||j f| t|d$|} qtd%|qވ| |d&|} | |gd'|} | |gd(|} z/tj|j i}| t|O} t j rD| tt!|d)} n| td*|d*iid)} Wn t"y]Ynw|r]| |d+} |#}z|D]}t$| j%}t$|| }t&j|d,}|}t'|t(s|g}zd|D]S}|)|}t'|t*std-|z-| }j+|d.|}t,jj-|d/d0d1}|d ur|D] } | | d2|} qWnt.yYqt/yw|r|rt.d3|Wn3t0t1fy&|d urj|j td4r|d urt1d5||d6t2d7|Yqpwt2d8|qpWnt3yCt/d9|t4|fw|jD]} | jr[| | jd:d/d;d<| j } qG|r|jrw| |jj|d:d:d;d=|jj } | |d>} |r| j5|6td?} | j7|j td@} |r|jr| |j8|dA} | |9dB} | j%dC} t:| ;| | dD} |r|j<| dE<|s|r| =| d.<|r|r|j>d ur|r?||| \| dF<| dG<tdHt j@r tAB| S| S)Ian 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())playhostrbinclude_hostvars_hosts _hosts_allcs.|ikr|Stjr|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(new_datasourcekey) _vars_sourcesr*r+_combine_and_tracks   z4VariableManager.get_vars.._combine_and_trackzrole '%s' defaultsall)bottom playbook_dirrtopzUnknown playbook vars logic: %sN) dep_chaincSsg|] }|jdvr|qS))roname).0gr*r*r+ z,VariableManager.get_vars..c si}z |j||}W|StyTz|D]}t|tr'|||jO}q|||jO}qWY|StySt|drIt d|j |j ft d|j |j fww)Nrunz)Cannot use v1 type vars plugin %s from %szInvalid vars plugin %s from %s) get_varsrAAttributeErrorr$r get_host_varsrurhasattrr _load_name_original_path)pluginpathentitiesr(entityr_r*r+_get_plugin_varss$   z2VariableManager.get_vars.._get_plugin_varscstjjj|S)z) merges all entities by inventory source )r!rAr@_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)rr(r)rnbasedirsrNrr*r+ _plugins_playsz/VariableManager.get_vars.._plugins_playcsSr^)r{r*) all_groupr*r+r4sz/VariableManager.get_vars..all_inventoryc gSr^r*r*)rrr*r+r3raz7VariableManager.get_vars..all_plugins_inventorycrr^r*r*)rrr*r+r2 raz2VariableManager.get_vars..all_plugins_playcstS)z gets group vars from inventory )rr*) host_groupsr*r+r1 z2VariableManager.get_vars..groups_inventorycS)z/ gets plugin sources from inventory for groups r*r*)rrr*r+r0rz:VariableManager.get_vars..groups_plugins_inventorycr)z* gets plugin sources from play for groups r*r*)rrr*r+r/rz5VariableManager.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)rnrrrr*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'facts 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 expansionvarsTvaulted)unsafecachezplay vars_files from '%s'zvars file %s was not found 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 %sF)include_params only_exportszrole '%s' exported varszrole '%s' all varsz task vars include_varsset_factz role paramszinclude paramsz extra varsz magic vars environmentansible_delegated_vars_ansible_loop_cachezdone with get_vars())CrLdebugr:_get_magic_variablesrJrA get_basedirrolespublicget_default_varsrurhPLAYBOOK_VARS_ROOTget_search_pathr _roleaction_ACTION_INCLUDE_ROLE get_dep_chainr@groupsrHr get_groupsVARIABLE_PRECEDENCE_ALLOWEDlocalsrMr{rrKrINJECT_FACTS_AS_VARSr KeyErrorget_vars_filesrr=rr$r%templaterpath_dwim_relative_stackr,load_from_filer r rr vvv TypeErrorr&r<get_namer;get_role_paramsget_include_paramsr#keysrcopy delegate_to_get_delegated_varsriVarsWithSourcesnew_vars_with_sources)!rNrcrdrbreinclude_delegate_to use_cacherfrgrall_varsmagic_variablesrolerr4r3r2r1r0r/rentryr vars_filesvars_file_item temp_varstemplarvars_file_list vars_fileplay_search_stack found_filer(r)r*) rnrrrmrrrrNrr+r{s        "             . :          zVariableManager.get_varscsi}tj|j|d<tj|d<tj|d<rIt ddj D}ddj D} t t || |d<| |d <||d <|d |d < |d <|rr|j rr|j j d d|d<|j j|d<t|j j|d<|j j|d<|j |d<|jdur|j|d<rt|jd} js| jrd} njpd} |sdd|jj| ddD}|sdd|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. rqansible_playbook_pythonansible_config_filecSs"h|] }|D]}|qqSr*)get_all_dependenciesr)rvrdr*r*r+ s"z7VariableManager._get_magic_variables..cSsg|]}|qSr*)r)rvrr*r*r+rxsz8VariableManager._get_magic_variables..ansible_role_namesansible_play_role_namesansible_dependent_role_names role_namesansible_play_nameF)include_role_fqcn role_name role_path role_uuidansible_collection_nameansible_role_nameNrr8rocSg|]}|jqSr*rtrvhr*r*r+rxT)patternignore_restrictionscSrr*rtrr*r*r+rxransible_play_hosts_allcg|] }|jvr|qSr*_removed_hostsrvxrcr*r+rxryansible_play_hostscrr*rrrr*r+rxryansible_play_batch play_hostsomithostvars)rCrabspathrArsys executablerh CONFIG_FILEr%rsetrr _role_pathr_uuid_role_collectionr@get_groups_dictr finalized is_templatehosts get_hostsrFrGitemsrB)rNrcrdrbrerfrgrdependency_role_namesplay_role_namesrroption option_valuer*rr+rsP             z$VariableManager._get_magic_variablescCsi}d}|jr[|j|jdd}||jkr[|std|j|j|}|dur@|jjdddD] }|j|kr:|}nq/t|d}||j | ||dddi|d <| d |d |d <||fS) zGet the delegated_vars for an individual task invocation, which may be in the context of an individual loop iteration. Not used directly be VariableManager, but used primarily within TaskExecutor NFfail_on_undefinedz.Empty hostname produced from delegate_to: "%s"T ignore_limitsrrtrcrdrbrrerinventory_hostname) rrrFr r@get_hostraddressrr{get_playrH)rNrrbrdelegated_varsdelegated_host_namedelegated_hostrr*r*r+get_delegated_vars_and_hostnames2     z/VariableManager.get_delegated_vars_and_hostnamecst|ds idfStjddd|}||d<|j|dvr-|d|jt|j|dg}d}|j dur|j t vrd}|j dkrKd }z=t |j |d d }|s`fd d |D}t j |j |jd } dD] } | |jvrunqlt| d| dt| j||d}Wn2tydg}Yn'wtd|j |j durz|j }Wntydg}Ynwd }dg}t} t|jdd} d } |D]}|dur||| <|_j|jd d}||jkrd} |durtd|jdt|tstdt||jd|| vrqd}|jdur6|j|}|dur5|jj dddD] }|j!|kr.|}nq"t"|d}nt"|d}|j#|||d dd| |<| d| |d<qd}|r[| r[|}| |fS)NloopzcGetting delegated variables via get_vars is no longer used, and is handled within the TaskExecutor.z2.18)versionansible_search_pathrT first_foundF)termsrr  convert_barecsg|] }|s|qSr*)r)rvtrr*r+rx[ryz7VariableManager._get_delegated_vars..)r9r)rvarfile_subdirs)rrzDFailed to find the lookup named '%s' in the available lookup pluginsloop_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.r rtr r )$r~rL deprecatedrrrArappendr loop_withrrrrHrsetattrrrzrr rr:getattr loop_controlavailable_variablesr_dsr$rr&r@rrrrr{)rNrcrbexisting_variables vars_copyrhas_loopfail loop_termsmylookupsubdirdelegated_host_varsitem_var cache_itemsr)rrrrr*rr+r1s                      z#VariableManager._get_delegated_varscCs|j|ddS)z- Clears the facts for a host N)rKpop)rNhostnamer*r*r+ clear_factsszVariableManager.clear_factscCst|ts tdt|t|z|j|}Wn ty%|}Ynwt|ts5t d |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&r#rrKrrrformat)rNrdr host_cacher*r*r+set_host_factss     zVariableManager.set_host_factscCs^t|ts tdt|t|z |j||O<WdSty.||j|<YdSw)r8zRthe type of 'facts' to set for nonpersistent_facts should be a Mapping but is a %sN)r$rrr&r#rr;r)rNrdrr*r*r+set_nonpersistent_factss   z'VariableManager.set_nonpersistent_factscCs|t|g||jvrt|j|<||j|vr5t|j||tr5t|tr5t|j|||i|j|<dS||j||<dS)z< Sets a value in the vars_cache for a host. N)r#r<r:r$rr)rNrdvarnamevaluer*r*r+set_host_variables   ,z!VariableManager.set_host_variable)NNN) NNNTFTNNrb)NN)__name__ __module__ __qualname__ frozensetrrRr\r]propertyrVr`r{rrrr7r;r<r?r*r*r*r+r-Fs*     6G) r-c@s|eZdZdZddZeddZddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZddZdS)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+rRs zVarsWithSources.__init__cCs||}||_|S)z@ Alternate constructor method to instantiate class with sources )rE)clsr(rEvr*r*r+rsz%VarsWithSources.new_vars_with_sourcescCs|j|dSr^)rErHrNrlr*r*r+ get_sourcezVarsWithSources.get_sourcecCs*|j|}td||j|df|S)Nzvariable '%s' from source: %sunknown)r(rLrrErH)rNrlvalr*r*r+ __getitem__ s zVarsWithSources.__getitem__cCs||j|<dSr^r()rNrlr>r*r*r+ __setitem__rLzVarsWithSources.__setitem__cCs |j|=dSr^rPrJr*r*r+ __delitem__ zVarsWithSources.__delitem__cC t|jSr^)iterr(r_r*r*r+__iter__razVarsWithSources.__iter__cCrTr^)lenr(r_r*r*r+__len__razVarsWithSources.__len__cCs |j|Sr^)r( __contains__rJr*r*r+rYrSzVarsWithSources.__contains__cCst|j|jSr^)rrr(rrEr_r*r*r+rszVarsWithSources.copycCs&t|tr|j}|||StSr^)r$rr(rupdateNotImplementedrNothercr*r*r+__or__"s   zVarsWithSources.__or__cCs0t|tr|}||||j|StSr^)r$r __class__rZr(r[r\r*r*r+__ror__)s   zVarsWithSources.__ror__cCs|j||jSr^)r(rZ)rNr]r*r*r+__ior__1s zVarsWithSources.__ior__N)r@rArB__doc__rR classmethodrrKrOrQrRrVrXrYrr_rarbr*r*r*r+rs   r)> __future__rrCr collectionsrcollections.abcrrrhashlibrjinja2.exceptionsransibler rhansible.errorsr r r r rransible.inventory.hostransible.inventory.helpersrr+ansible.module_utils.common.text.convertersransible.module_utils.sixrransible.plugins.loaderransible.vars.fact_cacheransible.templateransible.utils.displayransible.utils.listifyransible.utils.varsrrransible.utils.unsafe_proxyransible.vars.cleanrr ansible.vars.pluginsr!r"ansible.vars.reservedr#rLr,r-rr*r*r*r+s@               1