o nnhs% @sddlmZdZdZddlZddlZddlmZm Z ddl m Z ddl m Z mZmZddlmZmZmZdd lmZmZdd lmZmZdd lmZdd lmZdd lmZm Z dZ!zddl"Z"dZ!Wn e#ykYnwdZ$zddl%Z%dZ$Wn e#yYnwdZ&zddl'Z'dZ&Wne#yzddl(Z'dZ&Wn e#yYnwYnweZ)e!re*e"drGddde"j+Z,e e"j-e,dZ.nddZ.e!re"j/Z0e"j1Z2ne&re'j/Z0e'j2Z2ddZ3GdddeZ4dS)) annotationsaP 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__ dump_funcsupdatergetlistrstrrr)selfargskwargs __class__I/usr/local/lib/python3.10/dist-packages/ansible/plugins/inventory/toml.pyrs     zAnsibleTomlEncoder.__init__)__name__ __module__ __qualname__r __classcell__r#r#r!r$rsr)encodercCs,tr tt|Strtt|Std)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_dumpssr1cCsNt|trtdd|DSt|trdd|DSt|tr%t|S|S)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$ sz1convert_yaml_objects_to_native..cSsg|]}t|qSr#r2)r3r5r#r#r$ sz2convert_yaml_objects_to_native..) isinstancedictitemsrr )objr#r#r$r-s   r-cs>eZdZdZddZddZd fdd Zfd d ZZS) InventoryModuler+c CsV|durt|ts|jd|dS|j|}|durdS|D]\}}|dkrLt|ts:td|t|f|D] \}}|j |||q>q#|dkrrt|t s_td|t|f|D]}| |i|j ||qaq#|dkrt|tstd|t|f|D]\}}| |\} } || ||| qq#|jd||fq#dS) Nz5Skipping '%s' as this is not a valid group definitionvarszIInvalid "vars" entry for "%s" group, requires a dict, found "%s" instead.childrenzMInvalid "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)r8rdisplaywarning inventory add_groupr:rtype set_variabler _parse_group add_child_expand_hostpattern_populate_host_vars) rgroup group_datakeyr0varvaluesubgroup host_patternr?portr#r#r$rFsZ        zInventoryModule._parse_groupc Cs|rt|tstdt|t|j|}|j|s#td|dz|j |\}}t t |ddWSt yL}z td|t|f|dd}~wt tfye}z td|t|f|dd}~wty|}z td |t|f|dd}~ww) NzInvalid filename: '%s'z Unable to retrieve file contents) file_namesurrogate_or_strict)errorszTOML file (%s) is invalid: %s)orig_excz8An error occurred while trying to read the file '%s': %szsl  G