o nnhd@sDddlmZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z ddlmZddlmZmZddlmZmZmZddlmZdd lmZdd lmZdd lmZdd lm Z dd l!m"Z"m#Z#m$Z$e ddZ%ddiZ&ddZ'd"ddZ(d#ddZ)ddZ*ddZ+d#ddZ,ddZ-Gd d!d!e.Z/dS)$) annotationsN) namedtuple)MappingSequence)NativeEnvironment)AnsibleOptionsError AnsibleError)to_textto_bytes to_native) yaml_load) string_types)boolean)unquote)AnsibleVaultEncryptedUnicode)cleanup_tmp_file makedirs_safe unfrackpathSettingzname value origin typelookup)_termscCs4d}|r|d|7}|r|d|7}|d|7}|S)z& construct entry for requested config zplugin_type: %s z plugin: %s z setting: %s ) plugin_type plugin_nameconfigentryrrA/usr/local/lib/python3.10/dist-packages/ansible/config/manager.py _get_entry!s   rcsd}d|rtj|rtjt|r||r|}|durV|dvr-t|dd}n|dvr7t|}n|dkrAt|}n|dkr]t |t rUd d | d D}nt |t s\d}n|d krn|d krgd}|durmd }n|dkrt |t r~t |d}nd}n|dvrt |t rt |d}tj|st|ddt}tj||d}tjt|ddnd}n|dkrt |t r| tj}t |t r҇fdd |D}nwd}nt|dkrt |t rdd | d D}t |t rfdd |D}nRd}nO|dvrt |tsd}nA|dvr/t |t tttttfr,t|dd }|r+|d!kr+t|}nd"}nt |t tfrIt|dd }|rI|d!krIt|}|rVtd#|t|ft|dd$d%S)&a return a configuration variable with casting :arg value: The value to ensure correct typing of :kwarg value_type: The type of the value. This can be any of the following strings: :boolean: sets the value to a True or False value :bool: Same as 'boolean' :integer: Sets the value to an integer or raises a ValueType error :int: Same as 'integer' :float: Sets the value to a float or raises a ValueType error :list: Treats the value as a comma separated list. Split the value and return it as a python list. :none: Sets the value to None :path: Expands any environment variables and tilde's in the value. :tmppath: Create a unique temporary directory inside of the directory specified by value and return its path. :temppath: Same as 'tmppath' :tmp: Same as 'tmppath' :pathlist: Treat the value as a typical PATH string. (On POSIX, this means comma separated strings.) Split the value and then expand each part for environment variables and tildes. :pathspec: Treat the value as a PATH string. Expands any environment variables tildes's in the value. :str: Sets the value to string types. :string: Same as 'str' rN)rboolF)strict)integerintfloatlistcSsg|]}t|qSr)rstrip.0xrrr [zensure_type..,noneNonepathbasedir)tmptemppathtmppathizansible-local-%s)prefixdirT)warnr2pathspeccg|]}t|dqSr/ resolve_pathr&r/rrr)|r*pathlistcSsg|]}|qSr)r%r&rrrr)cr8r9r:r&r/rrr)r*)dict dictionaryr?)strstringsurrogate_or_stricterrorsinirAz"Invalid type provided for "%s": %spassthru)rD nonstring)osr.isabsexistsr lowerrr"r# isinstancer splitrr;rgetpidtempfilemkdtempatexitregisterrpathseprrrcomplexr r ValueErrorr )value value_typeorigin origin_ftypeerrmsgr4rr/r ensure_type-s                      r[cCs&d|vr |dt}t|d|dS)z& resolve relative or 'variable' paths z{{CWD}}F)followr0)replacerHgetcwdr)r.r0rrrr;sr;cCsTd}|dur(tj|d}|dvrd}|S|dvrd}|Std|t|f|S)N)z.iniz.cfgrE)z.yamlz.ymlyamlz3Unsupported configuration file extension for %s: %s)rHr.splitextrr )cfileftypeextrrrget_config_typesrecCsLd}|dur$z|j|dd|dddd}W|Sty#Y|Sw|S)z+ returns the value of last ini entry found NsectiondefaultskeyrT)raw)get Exception)prrVrrrget_ini_config_values" rmc Cs<|durt}t}g}td|}||ur/t|dd}tjt|r*tj|d}| |d}z*t }t |}tj|d}|j t j @rQtj|rPd}n | t|ddWn tydYnw| td dd| d |D]}t|} tj| rt| tjrnqud}||kr|r|d t||S) zO Load INI Config File order(first found is used): ENV, CWD, HOME, /etc/ansible NANSIBLE_CONFIGF)r\z ansible.cfgTrBrCz~/.ansible.cfgz/etc/ansible/ansible.cfgzAnsible is being run in a world writable directory (%s), ignoring it as an ansible.cfg source. For more information see https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir)setobjectrHgetenvrr.isdirr joinappendr^statst_modeS_IWOTHrJr OSErroraccessR_OKadd) warningsSENTINELpotential_paths path_from_envwarn_cmd_publiccwdpermscwd_cfgr.b_pathrrrfind_ini_config_filesJ         rcCsNdd}|D]\}}||dD]}||vr#||D]}||qqqdS)zDAdd deprecation source 'ansible.builtin' to deprecations in base.ymlcSsd|vr d|dd<dSdS)N deprecatedzansible.builtincollection_namer)rrrrprocess sz,_add_base_defs_deprecations..process)rEenvvarsN)items) base_defsrdummydatarfrrrr_add_base_defs_deprecationss  rc@seZdZgZeZd!ddZddZddZd"dd Z d d Z d#d d Z ddZ ddZ d!ddZddZd$ddZddZd%ddZd%ddZdd ZdS)& ConfigManagerNcCsvi|_i|_i|_||_||pdtjt|_t |j|jdur*t |j |_|jr1| ddd|jd<dS)Nz %s/base.ymlr.)defaulttype CONFIG_FILE) _base_defs_plugins_parsers _config_file_read_config_yaml_filerHr.dirname__file__rrWARNINGS_parse_config_file)self conf_file defs_filerrr__init__s   zConfigManager.__init__cCs\t|tr,|dr,|dr,|dur,zt|}||}W|Sty+Y|Sw|S)Nz{{z}})rLr startswithendswithr from_stringrenderrk)rrV variablestrrrtemplate_default1s&   zConfigManager.template_defaultcCsZt|}tj|r%t|d}t|piWdS1s wYtdt|)Nrbz4Missing base YAML definition file (bad install?): %s)r rHr.rJopenr rr )ryml_file config_defrrrr<s     z$ConfigManager._read_config_yaml_filec Cs|dur|j}t|}|dur|dkrwtjdd|j|<tt|d)}z t|dd}Wnt yD}z t d|t |fd}~wwWdn1sOwYz |j| |WdStj yv}z t d |t |fd}~wwt d t |dS) z1 return flat configuration settings from file(s) NrE);)inline_comment_prefixesrrBrCzNError reading config file(%s) because the config file was not utf8 encoded: %sz"Error reading config file (%s): %sz'Unsupported configuration file type: %s)rre configparser ConfigParserrrr r read UnicodeErrorrr read_stringError)rrbrcfcfg_texterrrrFs.z ConfigManager._parse_config_filecCsdS)zL Load YAML Config Files in order, check merge flags, keep origin of settingsNr)rrrr_find_yaml_config_filesasz%ConfigManager._find_yaml_config_filesc Cs8i}|||}|D]}|j||||||d||<q |S)N)rrkeysrdirect)get_configuration_definitionsget_config_value) rrnamerrroptionsdefsoptionrrrget_plugin_optionses  z ConfigManager.get_plugin_optionscCsJg}|||D]}d|vr"|dr"|dD] }||dqq |SNrr)rvaluesrt)rrrpvarspdef var_entryrrrget_plugin_varsns zConfigManager.get_plugin_varscCsVg}|||D]\}}d|vr(|dr(|dD] }||dkr'||qq |Sr)rrrt)rrrvariabler option_namerrrrrget_plugin_options_from_varws   z)ConfigManager.get_plugin_options_from_varcCsbi}|dur|j|d}|S|dur |j|i|d}|S|j|i|i|d}|SN)rrjr)rrrrretrrrget_configuration_definitionsz*ConfigManager.get_configuration_definitioncCs d}||jvr||j|v}|S)NFr)rrrhasrrrhas_configuration_definitions z*ConfigManager.has_configuration_definitionFcCsli}|dur |j}n|dur|j|i}n |j|i|i}|r4t|D] }|dr3||=q)|S)zP just list the possible settings, either base or for specific plugins or plugin N_)rrrjr$rr)rrrignore_privatercdefrrrrs z+ConfigManager.get_configuration_definitionsc Csd}d}|D]G}|d}z||d}Wnty)|jdt|Yqw|durMt|tr9t|dd}|}|}d|vrM|j |d|dfq||fS)z( repeat code for value entry assignment NrzCvalue for config entry {0} contains invalid characters, ignoring...rBrCr) rjUnicodeEncodeErrorrr{formatr rLr DEPRECATEDrt)r container entry_listrVrXrr temp_valuerrr _loop_entriess&    zConfigManager._loop_entriesc Csbz|j|||||||d\}} W|Styty0} z td|t| f| dd} ~ ww)z wrapper )rbrrrrrz*Unhandled exception when retrieving %s: %s)orig_excN)get_config_value_and_originrrkr ) rrrbrrrrrrV_droprrrrrs   zConfigManager.get_config_valuec s|dur|j}dkr|dfSd}d} d} |||vrÈdg} rFvr5}d} nfdd| D} | rF| d}d} |durc|rcd rc||d \}} d | } |durd r|r||d \}} d | } |dur|r|vr|}} n| r| D]}||vr||}|} nq|durd | } |durd vrddlm}||jd \}} d| } |durdr|tjd\}} d| } |j |ddur| ||dur|durt |}|r|rzX|D]O}|dkr't |j ||}n|dkr0t dt d||dure|}|} |} d|vre|dkr_|jd|d|df|dfqt d|qWnty}ztjd|t|fWYd}~nd}~ww|durddr|rt|ivrt dtt||n d} |d|}zt|d | | d!}WnFty}z9| d"r|dkrd} tdd | | d!}ntd#tt||| t|fWYd}~nd}~ww|durd$vrd$durd%}d d&krAtfd'd(|D }n|d$v}|rtd$trgd) d*dd$!D}n0td$t"rxd$}ntd$t#rd) d+dd$D}nd$}td,|tt|||fdvr| dkr|jdf|| fSt d-tt||).zY Given a config key figure out the actual value and report on the origin of the settings NrraliasesDirectcsg|] }|vr|qSrr)r'alias)rrrr)sz=ConfigManager.get_config_value_and_origin..rrzvar: %skeywordz keyword: %scli)contextzcli: %srzenv: %srEr`z4YAML configuration type has not been implemented yetz#Invalid configuration file type: %srz[%s]%srfrhzUnimplemented file type: %sz!Error while loading config %s: %srequiredFz5No setting was provided for required configuration %srr)rXrYzenv:z6Invalid type for configuration option %s (from %s): %schoicesTr$c3s |] }|dvVqdS)rNr)r'choice)rrrr Ksz.z, cSg|]}t|qSrr )r'krrrr)Sr=cSrrr)r'crrrr)Wr=zFInvalid value "%s" for configuration option "%s", valid values are: %sz6Requested entry (%s) was not defined in configuration.)$rrrjransiblerCLIARGSrHenvironrrrermrrrtrksysstderrwriter INTERNAL_DEFSrrr[rUrrr%allrLrrsrr r)rrrbrrrrrrVrXrYrdirect_aliasesrrrrcrrrinvalid_choicesvalidr)rrrrrs           ( $ & * " z)ConfigManager.get_config_value_and_origincCs&||jvr i|j|<||j||<dSrr)rrrrrrr+initialize_plugin_configuration_definitionsfs  z9ConfigManager.initialize_plugin_configuration_definitionsNNr)NNN)NNF)NNNNNN)__name__ __module__ __qualname__rrorrrrrrrrrrrrrrrrrrrrrs&         rrr)0 __future__rrQrrHos.pathrrurO collectionsrcollections.abcrrjinja2.nativetypesransible.errorsrr+ansible.module_utils.common.text.convertersr r r ansible.module_utils.common.yamlr ansible.module_utils.sixr )ansible.module_utils.parsing.convert_boolransible.parsing.quotingransible.parsing.yaml.objectsransible.utils.pathrrrrrrr[r;rermrrrprrrrrs:          u  A