
    Vh                        d dl mZ dZdZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d d	lmZ  ed      Z G d
 de      Zy)    )annotationsa  
    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)AnsibleErrorAnsibleParserError)string_types)	to_nativeto_text)BaseFileInventoryPluginc                  D     e Zd ZdZ fdZ fdZd fd	Zd Zd Z xZ	S )InventoryModuleyamlc                *    t         t        |           y )N)superr   __init__)self	__class__s    N/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/inventory/yaml.pyr   zInventoryModule.__init__T   s    ot-/    c                    d}t         t        |   |      r9t        j                  j                  |      \  }}|r|| j                  d      v rd}|S )NFyaml_extensionsT)r   r   verify_fileospathsplitext
get_option)r   r   valid	file_nameextr   s        r   r   zInventoryModule.verify_fileX   sM    $3D9WW--d3NIs#1B!CCr   c                   t         t        |   |||       | j                          	 | j                  j                  |d      }|st        d      t        |t              st        dt        |      z        |j                  d      rt        d      t        |t              r|D ]  }| j                  |||           yt        dt        |      z        # t        $ r}t        |      d}~ww xY w)	z parses the inventory file none)cacheNzParsed empty YAML filezHYAML inventory has invalid structure, it should be a dictionary, got: %spluginz2Plugin configuration YAML file, not YAML inventoryz8Invalid data from file, expected dictionary and got:

%s)r   r   parseset_optionsloaderload_from_file	Exceptionr   
isinstancer   typeget_parse_groupr   )	r   	inventoryr%   r   r!   datae
group_namer   s	           r   r#   zInventoryModule.parsea   s     	ot*9fdC	(;;--d&-AD $%=>>D.1$%orvw{r|%|}}XXh$%YZZ dN+" @
!!*d:.>?@ %%admnrds%stt!  	($Q''	(s   C 	C2"C--C2c                   t        |t        t        f      r	 | j                  j	                  |      }|dD ]^  }||v st        ||   t              r
||   d i||<   t        ||   t        t        f      r?t        d|d|dt        ||         d       |D ]_  }t        ||   t        t        f      s1| j                  j                  d|d	|d
t        ||                Nt        ||   t              r#| j                  j                  d|d	|d       |dk(  r.||   D ]%  }| j                  j                  ||||   |          ' |dk(  r?||   D ]6  }| j                  |||   |         }| j                  j                  ||       8 |dk(  r>||   D ]4  }| j!                  |      \  }	}
| j#                  |	||   |   xs i ||
       6 >| j                  j                  d|d	|d       b |S | j                  j                  d|z         |S # t
        $ r}t        d|dt        |            d }~ww xY w)NzUnable to add group z: )varschildrenhostsz	Invalid "z" entry for "z'" group, requires a dictionary, found "z
" instead.zSkipping key (z) in group (z") as it is not a mapping, it is a zSkipping empty key ()r1   r2   r3   zSkipping unexpected key (z0), only "vars", "children" and "hosts" are validz5Skipping '%s' as this is not a valid group definition)r(   r   NoneTyper,   	add_groupr   r   r	   r   r)   displaywarningvvvset_variabler+   	add_child_parse_host_populate_host_vars)r   group
group_datar.   sectionkeyvarsubgrouphost_patternr3   ports              r   r+   zInventoryModule._parse_group{   s   j>8"<=]007 %< bG*,%j&9<H3=g3F2MJw/)*W*=PX?YZ"46=ud:V]K^F_6a #b bb & YC%jo7QR,,uxz  BF  GQ  RU  GV  BW  .X  Y !*S/8<((TWY^)_`#-c? ZC NN77sJsOTWDXYZ
*(23 FH'+'8'8:c?S[C\']H NN44UHEF ,6sO nL*.*:*:<*HKE4 44UJsOL<Y<_]_afhlmn ,,  MP  RW  .X  Y+Y4  LL  !X[`!`aS   ](PWXYPZ)[\\]s   H! !	I	*II	c                l    	 | j                  |      \  }}||fS # t        $ r t        d| d      w xY w)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.)_expand_hostpattern	TypeErrorr   )r   rD   	hostnamesrE   s       r   r<   zInventoryModule._parse_host   sS    	 $ 8 8 FY
 $	  	$~-kl 	s    3)T)
__name__
__module____qualname__NAMEr   r   r#   r+   r<   __classcell__)r   s   @r   r   r   P   s#    D0u4/b
r   r   )
__future__r   DOCUMENTATIONEXAMPLESr   collections.abcr   ansible.errorsr   r   ansible.module_utils.sixr   +ansible.module_utils.common.text.convertersr   r	   ansible.plugins.inventoryr
   r)   r5   r    r   r   <module>rX      sE    #:B 
 * ; 1 J =:f- fr   