a )g% @sddlmZmZmZeZdZdZddlZddl Z ddl m Z m Z ddlmZddlmZmZmZddlmZmZmZdd lmZmZdd lmZmZdd lmZdd l m!Z!dd l"m#Z#m$Z$dZ%zddl&Z&dZ%Wne'yYn0dZ(zddl)Z)dZ(Wne'y Yn0dZ*zddl+Z+dZ*Wn:e'y\zddl,Z+dZ*Wne'yVYn0Yn0e!Z-e%re.e&drGddde&j/Z0ee&j1e0dZ2nddZ2e%re&j3Z4e&j5Z6ne*re+j3Z4e+j6Z6ddZ7GdddeZ8dS))absolute_importdivisionprint_functionaP name: toml version_added: "2.8" short_description: Uses a specific TOML file as an inventory source. description: - TOML based inventory format - File MUST have a valid '.toml' file extension notes: - > Requires one of the following python libraries: 'toml', 'tomli', or 'tomllib' a# fmt: toml # Example 1 [all.vars] has_java = false [web] children = [ "apache", "nginx" ] vars = { http_port = 8080, myvar = 23 } [web.hosts] host1 = {} host2 = { ansible_port = 222 } [apache.hosts] tomcat1 = {} tomcat2 = { myvar = 34 } tomcat3 = { mysecret = "03#pa33w0rd" } [nginx.hosts] jenkins1 = {} [nginx.vars] has_java = true # Example 2 [all.vars] has_java = false [web] children = [ "apache", "nginx" ] [web.vars] http_port = 8080 myvar = 23 [web.hosts.host1] [web.hosts.host2] ansible_port = 222 [apache.hosts.tomcat1] [apache.hosts.tomcat2] myvar = 34 [apache.hosts.tomcat3] mysecret = "03#pa33w0rd" [nginx.hosts.jenkins1] [nginx.vars] has_java = true # Example 3 [ungrouped.hosts] host1 = {} host2 = { ansible_host = "127.0.0.1", ansible_port = 44 } host3 = { ansible_host = "127.0.0.1", ansible_port = 45 } [g1.hosts] host4 = {} [g2.hosts] host4 = {} N)MutableMappingMutableSequence)partial)AnsibleFileNotFoundAnsibleParserErrorAnsibleRuntimeError)to_bytes to_nativeto_text) string_types text_type)AnsibleSequenceAnsibleUnicode)BaseFileInventoryPlugin)Display)AnsibleUnsafeBytesAnsibleUnsafeTextFT TomlEncodercseZdZfddZZS)AnsibleTomlEncoderc sVtt|j|i||jt|jtt|jt t |jt t |jt idSN) superr__init__Z dump_funcsupdatergetlistrstrrr)selfargskwargs __class__B/usr/lib/python3.9/site-packages/ansible/plugins/inventory/toml.pyrs    zAnsibleTomlEncoder.__init__)__name__ __module__ __qualname__r __classcell__r$r$r"r%rsr)encodercCs0trtt|Str$tt|StddS)NzTThe python "toml" or "tomli-w" library is required when using the TOML output format)HAS_TOMLtomldumpsconvert_yaml_objects_to_native HAS_TOMLIWtomli_wr )datar$r$r% toml_dumpssr2cCsRt|tr tdd|DSt|tr8dd|DSt|trJt|S|SdS)aOlder versions of the ``toml`` python library, and tomllib, don't have a pluggable way to tell the encoder about custom types, so we need to ensure objects that we pass are native types. Used with: - ``toml<0.10.0`` where ``toml.TomlEncoder`` is missing - ``tomli`` or ``tomllib`` This function recurses an object and ensures we cast any of the types from ``ansible.parsing.yaml.objects`` into their native types, effectively cleansing the data before we hand it over to the toml library. This function doesn't directly check for the types from ``ansible.parsing.yaml.objects`` but instead checks for the types those objects inherit from, to offer more flexibility. css|]\}}|t|fVqdSrr.).0kvr$r$r% z1convert_yaml_objects_to_native..cSsg|] }t|qSr$r3)r4r6r$r$r% r8z2convert_yaml_objects_to_native..N) isinstancedictitemsrr)objr$r$r%r.s   r.cs>eZdZdZddZddZd fdd Zfd d ZZS) InventoryModuler,c Cs^|dur&t|ts&|jd|dS|j|}|dur>dS|D]\}}|dkrt|tsvtd|t|f|D]\}}|j |||q~qF|dkrt|t std|t|f|D]}| |i|j ||qqF|dkrDt|tstd|t|f|D](\}}| |\} } || ||| qqF|jd||fqFdS) Nz5Skipping '%s' as this is not a valid group definitionvarszIInvalid "vars" entry for "%s" group, requires a dict, found "%s" instead.ZchildrenzMInvalid "children" entry for "%s" group, requires a list, found "%s" instead.hostszJInvalid "hosts" entry for "%s" group, requires a dict, found "%s" instead.zYSkipping unexpected key "%s" in group "%s", only "vars", "children" and "hosts" are valid)r:rdisplayZwarning inventory add_groupr<r typeZ set_variabler _parse_groupZ add_childZ_expand_hostpatternZ_populate_host_vars) rgroupZ group_datakeyr1varvalueZsubgroupZ host_patternr@portr$r$r%rEsR         zInventoryModule._parse_groupc Cs$|rt|tstdt|t|j|}|j|sFtd|dz"|j |\}}t t |ddWSt y}z$td|t|f|dWYd}~nd}~0t tfy}z$td|t|f|dWYd}~nFd}~0ty}z$td |t|f|dWYd}~n d}~00dS) NzInvalid filename: '%s'z Unable to retrieve file contents) file_nameZsurrogate_or_strict)errorszTOML file (%s) is invalid: %s)Zorig_excz8An error occurred while trying to read the file '%s': %szparseZ set_optionsrTrRrrE)rrBrMpathcacher1rSZ group_namer"r$r%rV s   zInventoryModule.parsecs0tt||r,tj|\}}|dkr,dSdS)Nz.tomlTF)rr> verify_fileosrWsplitext)rrWrKextr"r$r%rY%s zInventoryModule.verify_file)T) r&r'r(NAMErErTrVrYr)r$r$r"r%r>s ,r>)9Z __future__rrrrDZ __metaclass__Z DOCUMENTATIONZEXAMPLESrZtypingtcollections.abcrr functoolsrZansible.errorsrr r Zansible.module_utils._textr r r Zansible.module_utils.sixrrZansible.parsing.yaml.objectsrrZansible.plugins.inventoryrZansible.utils.displayrZansible.utils.unsafe_proxyrrr+r, ImportErrorr/r0rUZtomllibZtomlirAhasattrrrr-r2loadsrNZTomlDecodeErrorrOr.r>r$r$r$r%sb G