a )g@sddlmZmZmZeZdZdZddlZddl m Z ddl m Z m Z ddlmZddlmZmZdd lmZedZGd d d eZdS) )absolute_importdivisionprint_functiona name: yaml version_added: "2.4" short_description: Uses a specific YAML file as an inventory source. description: - "YAML-based inventory, should start with the C(all) group and contain hosts/vars/children entries." - Host entries can have sub-entries defined, which will be treated as variables. - Vars entries are normal group vars. - "Children are 'child groups', which can also have their own vars/hosts/children and so on." - File MUST have a valid extension, defined in configuration. notes: - If you want to set vars for the C(all) group inside the inventory file, the C(all) group must be the first entry in the file. - Enabled in configuration by default. options: yaml_extensions: description: list of 'valid' extensions for files containing YAML type: list elements: string default: ['.yaml', '.yml', '.json'] env: - name: ANSIBLE_YAML_FILENAME_EXT - name: ANSIBLE_INVENTORY_PLUGIN_EXTS ini: - key: yaml_valid_extensions section: defaults - section: inventory_plugin_yaml key: yaml_valid_extensions a all: # keys must be unique, i.e. only one 'hosts' per group hosts: test1: test2: host_var: value vars: group_all_var: value children: # key order does not matter, indentation does other_group: children: group_x: hosts: test5 # Note that one machine will work without a colon #group_x: # hosts: # test5 # But this won't # test7 # group_y: hosts: test6: # So always use a colon vars: g2_var2: value3 hosts: test4: ansible_host: 127.0.0.1 last_group: hosts: test1 # same host as above, additional group membership vars: group_last_var: value N)MutableMapping) AnsibleErrorAnsibleParserError) string_types) to_nativeto_text)BaseFileInventoryPlugincsJeZdZdZfddZfddZd fdd Zd d Zd d ZZ S)InventoryModuleZyamlcstt|dS)N)superr __init__)self __class__B/usr/lib/python3.9/site-packages/ansible/plugins/inventory/yaml.pyrUszInventoryModule.__init__cs>d}tt||r:tj|\}}|r6||dvr:d}|S)NFZyaml_extensionsT)r r verify_fileospathsplitextZ get_option)rrZvalid file_nameextrrrrYs zInventoryModule.verify_fileTc stt|||||z|jj|dd}Wn,ty\}zt|WYd}~n d}~00|sltdn.t|t stdt |n| drtdt|t r|D]}| |||qntdt |dS) z parses the inventory file F)cacheNzParsed empty YAML filezHYAML inventory has invalid structure, it should be a dictionary, got: %sZpluginz2Plugin configuration YAML file, not YAML inventoryz8Invalid data from file, expected dictionary and got: %s)r r parseZ set_optionsloaderZload_from_file Exceptionr isinstancertypeget _parse_groupr )r inventoryrrrdataeZ group_namerrrrbs     zInventoryModule.parsec Cst|ttfrz|j|}Wn8tyX}z td|t|fWYd}~n d}~00|durdD]V}||vrht||tr||di||<t||ttfshtd||t ||fqh|D]*}t||ttfs|j d||t ||fqt||tr"|j d||fq|dkrV||D]}|j |||||q4q|dkr||D](}|||||}|j||qhq|dkr||D]2}||\} } || |||pi|| qq|j d ||fqn|j d ||S) NzUnable to add group %s: %s)varschildrenhostszMInvalid "%s" entry for "%s" group, requires a dictionary, found "%s" instead.zBSkipping key (%s) in group (%s) as it is not a mapping, it is a %sz%Skipping empty key (%s) in group (%s)r%r&r'zYSkipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are validz5Skipping '%s' as this is not a valid group definition)rrNoneTyper" add_grouprrr rrZdisplayZwarningZvvvZ set_variabler!Z add_child _parse_hostZ_populate_host_vars) rgroupZ group_datar$sectionkeyvarZsubgroup host_patternr'portrrrr!|sD*        $zInventoryModule._parse_groupcCs>z||\}}Wn"ty4td|dYn0||fS)z_ Each host key can be a pattern, try to process it and add variables as needed z Host pattern z> must be a string. Enclose integers/floats in quotation marks.)Z_expand_hostpattern TypeErrorr)rr/Z hostnamesr0rrrr*s  zInventoryModule._parse_host)T) __name__ __module__ __qualname__NAMErrrr!r* __classcell__rrrrr Qs   1r )Z __future__rrrrZ __metaclass__Z DOCUMENTATIONZEXAMPLESrcollections.abcrZansible.errorsrrZansible.module_utils.sixrZansible.module_utils._textr r Zansible.plugins.inventoryr r(r rrrrs!