
    VhP                    F   d dl mZ d dlZd dl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 d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlm Z m!Z!  e       Z"d ZddZ#ddZ$d Z% G d de      Z& G d de&      Z' G d de(      Z) G d de(      Z*y)    )annotationsN)Mapping)AnsibleErrorAnsibleParserError)to_safe_group_name)parse_address)AnsiblePlugin)CachePluginAdjudicator)to_bytes	to_native)boolean)string_types)Templar)Display)combine_varsload_extra_varsc                    t        | dd      S )NT)forcesilent)original_safe)names    R/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/inventory/__init__.pyr   r   *   s    T$77    c                
    d| v S )z
    A helper function that checks a given host line to see if it contains
    a range pattern described in the docstring above.

    Returns True if the given line contains a pattern, else False.
    [ )lines    r   detect_ranger   /   s     $;r   c                   g }| r| j                  ddd      j                  ddd      j                  d      \  }}}|j                  d      }t        |      dk7  rt        |      dk7  rt        d      |d	   }|d   }t        |      dk(  rd}n|d   }|sd
}|st        d      |d	   d
k(  r8t        |      dkD  r*t        |      t        |      k7  rt        d      fd}	nt        }		 t
        j                  j                  |      }
t
        j                  j                  |      }|
|kD  rt        d      t        t
        j                  |
|dz   t        |               }|D ]S  }dj                  | |	|      |f      }t        |      r|j                  t        |             C|j!                  |       U |S y# t        $ r. t        t        |      t        |      dz   t        |            }Y w xY w)a  
    A helper function that expands a given line that contains a pattern
    specified in top docstring, and returns a list that consists of the
    expanded version.

    The '[' and ']' characters are used to maintain the pseudo-code
    appearance. They are replaced in this function with '|' to ease
    string splitting.

    References: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#hosts-and-groups
    r   |   ]:      z.host range must be begin:end or begin:end:stepr   0z!host range must specify end valuez:host range must specify equal-length begin and end formatsc                8    t        |       j                        S N)strzfill)xrlens    r   fillz#expand_hostname_range.<locals>.fillg   s    1v||D))r   z!host range must have begin <= end N)replacesplitlenr   r)   stringascii_lettersindexlistint
ValueErrorrangejoinr   extendexpand_hostname_rangeappend)r   	all_hostsheadnrangetailboundsbegendstepr-   i_begi_endseqrseqhnamer,   s                  @r   r;   r;   9   s    I  $||Ca8@@c1MSSTWXvtc"v;!Fq 0OPPQiQiv;!D!9DCBCCq6S=SX\s8Ds3x"#_``* D	;((..s3E((..s3Eu}"#FGGv++E%!)CI,EFGC  	(DGGT4:t45EE"  !6u!=>  '	( q Z  	;C#c(Q,D	:C	;s   .A9G 4G98G9c                    	 t        | fi |}| dk7  r3|r1t        |j                  dd       st        dj                  |             |S # t        $ r"}dt        |      v rt        d| z        |d }~ww xY w)Nfact_caching_connectiona  error, '%s' inventory cache plugin requires the one of the following to be set to a writeable directory path:
ansible.cfg:
[default]: fact_caching_connection,
[inventory]: cache_connection;
Environment:
ANSIBLE_INVENTORY_CACHE_CONNECTION,
ANSIBLE_CACHE_PLUGIN_CONNECTION.memory_optionszUnable to use cache plugin {0} for inventory. Cache options were provided but may not reconcile correctly unless set via set_options. Refer to the porting guide if the plugin derives user settings from ansible.constants.)CacheObjectr   r   getattr_pluginformat)plugin_namekwargscachees       r   get_cache_pluginrV      s    	K262 h6'%--UY2Z 55;VK5HJ 	J L  $	!4  B EP P Q Q
 Gs   A 	A3A..A3c                  ^     e Zd ZdZdZ ee      Z fdZd
dZ	d Z
ddZd Zd Zd	 Z xZS )BaseInventoryPluginz Parses an Inventory Source	generatorc                j    t         t        |           i | _        d | _        t
        | _        i | _        y r(   )superrX   __init__rM   	inventorydisplay_varsself	__class__s    r   r\   zBaseInventoryPlugin.__init__   s-    !413
r   c                b    || _         || _        t        |      | _        t	        |      | _        y)az   Populates inventory from the given data. Raises an error on any parse failure
            :arg inventory: a copy of the previously accumulated inventory data,
                 to be updated with any new data this plugin provides.
                 The inventory can be empty if no other source/plugin ran successfully.
            :arg loader: a reference to the DataLoader, which can read in YAML and JSON files,
                 it also has Vault support to automatically decrypt files.
            :arg path: the string that represents the 'inventory source',
                 normally a path to a configuration file for this inventory,
                 but it can also be a raw string for this plugin to consume
            :arg cache: a boolean that indicates if the plugin should use the cache or not
                 you can ignore if this plugin does not implement caching.
        )loaderN)rd   r]   r   templarr   r_   )ra   r]   rd   pathrT   s        r   parsezBaseInventoryPlugin.parse   s*     "f-$V,
r   c                    d}t        |d      }t        j                  j                  |      r(t        j                  |t        j
                        rd}|S | j                  j                  d       |S )a   Verify if file is usable by this plugin, base does minimal accessibility check
            :arg path: a string that was passed as an inventory source,
                 it normally is a path to a config file, but this is not a requirement,
                 it can also be parsed itself as the inventory data to process.
                 So only call this base class if you expect it to be a file.
        Fsurrogate_or_stricterrorsTzWSkipping due to inventory source not existing or not being readable by the current user)r   osrf   existsaccessR_OKr^   vvv)ra   rf   validb_paths       r   verify_filezBaseInventoryPlugin.verify_file   s]     $'<=GGNN6"ryy'AE  LLvwr   c                    t        |t              st        dt        |      z        |D ]G  }| j                  j                  |||       |D ]"  }| j                  j                  ||||          $ I y )Nz8Invalid data from file, expected dictionary and got:

%s)groupport)
isinstancer   r   r   r]   add_hostset_variable)ra   hosts	variablesru   rv   hostks          r   _populate_host_varsz'BaseInventoryPlugin._populate_host_vars   sy    )W-$%admnwdx%xyy 	CDNN##DD#A C++D!Yq\BC	Cr   c                    i }	  j                   j                  |d      }t         d      xs  j                  g}|st        dt	        |      z        |j                  d      |vrt        d|j                  dd      z        t        |t              st        d	t        |      z         j                  | j                  
       d j                  v rI j                  d      r8g d}t         fd|D              }t!         j                  d      fi | _        |S # t        $ r}t        t	        |            d}~ww xY w)z validate config and set options as appropriate
            :arg path: path to common yaml format config file for this plugin
        none)rT   N_redirected_namesz%s is emptypluginz!Incorrect plugin name in file: %sz
none foundzJinventory source has invalid structure, it should be a dictionary, got: %s)directvar_optionsrT   )_uricache_connection)_timeoutcache_timeout)_prefixcache_prefixc              3  z   K   | ]2  }j                  |d          |d   j                  |d          f 4 ywr!   Nr   
get_option.0optra   s     r   	<genexpr>z8BaseInventoryPlugin._read_config_data.<locals>.<genexpr>   sI       !Hscgcrcrsvwxsycz  dG#a&$//#a&*A!B  !H   ;;cache_plugin)rd   load_from_file	Exceptionr   r   rO   NAMEgetrw   r   typeset_optionsr_   rM   r   dictrV   _cache)ra   rf   configrU   valid_namescache_option_keyscache_optionss   `      r   _read_config_dataz%BaseInventoryPlugin._read_config_data   sG   
 	3 [[//F/CF
 d$78GTYYK$]io%FGGZZ!4$%H6::V^`lKm%mnnFG,$%qtxy  uA  &A  B  BDJJ?dmm#(@ z   !HN_  !H  HM*4??>+J\m\DK-  	3$Yq\22	3s   D$ $	E-EEc                l    | j                   D ]%  }||v s|j                  |      | j                   |<   ' y)a<   update existing options from alternate configuration sources not normally used by Ansible.
            Many API libraries already have existing configuration sources, this allows plugin author to leverage them.
            :arg data: key/value pairs that correspond to configuration options for this plugin
        N)rM   pop)ra   datar}   s      r   _consume_optionsz$BaseInventoryPlugin._consume_options   s4      	/ADy#'88A;a 	/r   c                    	 t        |d      \  }}t        |      rt        |      }||fS |g}||fS # t        $ r |}d}Y 0w xY w)z
        Takes a single host pattern and returns a list of hostnames and an
        optional port number that applies to all of them.
        T)allow_rangesN)r   r   r   r;   )ra   hostpatternpatternrv   	hostnamess        r   _expand_hostpatternz'BaseInventoryPlugin._expand_hostpattern   sg    	+KdKOWd  -g6I 4   !	I4    	!GD	s   3 AAT)NN)__name__
__module____qualname____doc__TYPEstaticmethodr   _sanitize_group_namer\   rg   rs   r~   r   r   r   __classcell__rb   s   @r   rX   rX      s>    %D ((:;-& C D/!r   rX   c                  &     e Zd ZdZdZ fdZ xZS )BaseFileInventoryPluginz% Parses a File based Inventory Sourcestoragec                *    t         t        |           y r(   )r[   r   r\   r`   s    r   r\   z BaseFileInventoryPlugin.__init__  s    %t57r   )r   r   r   r   r   r\   r   r   s   @r   r   r     s    /D8 8r   r   c                  N    e Zd Z e       Zed        Zd Zd Zd Z	d Z
d Zd Zy)		Cacheablec                    | j                   S r(   )r   ra   s    r   rT   zCacheable.cache(  s    {{r   c                z      j                  d      }g d}t         fd|D              }t        |fi | _        y )Nr   r   c              3  z   K   | ]2  }j                  |d          |d   j                  |d          f 4 ywr   r   r   s     r   r   z.Cacheable.load_cache_plugin.<locals>.<genexpr>/  sI       D3_c_n_norstou_v  `Cc!fdooc!f&=>  Dr   )r   r   rV   r   )ra   rR   r   r   s   `   r   load_cache_pluginzCacheable.load_cache_plugin,  sB    oon5v  DJ[  D  D&{DmDr   c                X    dj                  | j                  | j                  |            S )Nz{0}_{1})rQ   r   _get_cache_prefix)ra   rf   s     r   get_cache_keyzCacheable.get_cache_key2  s$    		4+A+A$+GHHr   c                H   t        j                         }|j                  t        | j                  d             |j                         }t        j                         }|j                  t        |d             |j                         }dj                  |dd |dd g      S )z7 create predictable unique prefix for plugin/inventory ri   rj   s_N   )hashlibsha1updater   r   	hexdigestr9   )ra   rf   md1nd2s         r   r   zCacheable._get_cache_prefix5  s|     LLN	$)),ABC[[]LLN	$'<=>[[]yy"Ra&"Ra&)**r   c                8    | j                   j                          y r(   )r   flushr   s    r   clear_cachezCacheable.clear_cacheB  s    r   c                8    | j                   j                          y r(   )r   update_cache_if_changedr   s    r   r   z!Cacheable.update_cache_if_changedE  s    ++-r   c                8    | j                   j                          y r(   )r   	set_cacher   s    r   set_cache_pluginzCacheable.set_cache_pluginH  s    r   N)r   r   r   rN   r   propertyrT   r   r   r   r   r   r   r   r   r   r   r   $  s;    ]F EI+. r   r   c                  ,    e Zd ZddZddZddZddZy)	Constructablec                ,   | j                   }	 | j                  d      }|rt        || j                        |_        n||_        |j                  |j                  j                  ||j                  j                  |      S # t        $ r d}Y sw xY w)zi helper method for plugins to compose variables for Ansible based on jinja2 expression and inventory varsuse_extra_varsF)disable_lookups)
re   r   r   r   r_   available_variablestemplateenvironmentvariable_start_stringvariable_end_string)ra   r   r{   r   t	use_extras         r   _composezConstructable._composeN  s    LL	(89I $0DJJ$GA!$-A!zzamm&I&I8UVUbUbUvUvw*9  ; 	;  	I	s   B BBc                
   |rLt        |t              r;|D ]5  }	 | j                  ||   |      }| j                  j                  |||       7 yyy# t        $ r)}|rt	        d|d|dt        |            Y d}~hd}~ww xY w)z5 loops over compose entries to create vars for hosts zCould not set z
 for host : N)rw   r   r   r   r   r   r]   ry   )ra   composer{   r|   strictvarname	compositerU   s           r   _set_composite_varsz!Constructable._set_composite_vars_  s    z'40" F $gg.>	 JI
 ++D'9EF 17 ! *QXZ^`ijk`l+mnns   A	BA==Bc                    |rt        |t              r|r3t        || j                  j	                  |      j                               }|| j                  _        |D ]z  }d||   z  }| j                  |      }	 t        | j                  j                  |            }|sD| j                  j                  |      }| j                  j                  ||       | yyy# t        $ r)}	|rt        d|d|dt        |	            Y d}	~	d}	~	ww xY w)z} helper to create complex groups for plugins based on jinja2 conditionals, hosts that meet the conditional are added to groupz3{%% if %s %%} True {%% else %%} False {%% endif %%}zCould not add host z
 to group r   N)rw   r   r   r]   get_hostget_varsre   r   r   r   r   r   r   r   	add_group	add_child)
ra   groupsr{   r|   r   fetch_hostvars
group_nameconditionalresultrU   s
             r   _add_host_to_composed_groupsz*Constructable._add_host_to_composed_groupsk  s     j.(DNN4K4KD4Q4Z4Z4\]	/8DLL,$ ?
SV\]gVhh!66zB
$T\\%:%:;%GHF !%!9!9*!EJNN,,Z>?	 /6 ! 0\`blnwxynz1{||s   8$C	D$DDc                   |rt        |t              r|D ]  }|rt        |t              r|r3t        || j                  j                  |      j                               }	 | j                  |j                  d      |      }|j                  dd      }	|j                  d      }
|
|	t        d      |s	|d	k(  r|	|j                  d
d	      }|j                  dd      }|j                  dd      }|r	 | j                  j                  |      }g }t        |t              r+|d	k(  r|	|j!                  |	       n|j!                  |       nt        |t              r2|D ],  }|d	k(  r|	|j!                  |	       |j!                  |       . not        |t"              rH|j%                         D ]4  \  }}|||}|d	k(  r|		|||	}n|
du r|}|j!                  |       6 nt        dt'        |      z        |D ]  }|d	k(  r| j)                  d      du rd	}| j+                  |||      }| j                  j-                  |      }| j                  j/                  ||       |sn| j+                  |      }| j                  j-                  |       | j                  j1                  ||        |s|g i fvst        d|j                  d      d|d      t        d|z         yyy# t        $ r9}|r,t        d|d|j                  d      dt        |            Y d}~&d}~ww xY w# t        $ r*}|rt        d|d|dt        |            Y d}~\d}~ww xY w)zc helper to create groups for plugins based on variable values and add the corresponding hosts to itkeyz"Could not generate group for host z from z entry: Ndefault_valuetrailing_separatorzTparameters are mutually exclusive for keyed groups: default_value|trailing_separatorr.   prefix	separator_parent_groupz Could not generate parent group z for group r   FzUInvalid group name format, expected a string or a list of them or dictionary, got: %sleading_separatorz!No key or key resulted empty for z	 in host z, invalid entryz7Invalid keyed group entry, it must be a dictionary: %s )rw   r5   r   r   r]   r   r   r   r   r   r   r   re   r   r   r   r<   r   itemsr   r   r   r   rx   r   )ra   keysr{   r|   r   r   keyedr   rU   default_value_namer   r   sepraw_parent_namenew_raw_group_namesr   gnamegval	bare_nameresult_gnameparent_names                        r   _add_host_to_keyed_groupsz'Constructable._add_host_to_keyed_groups  s   JtT* HpZt4%$0DNN<S<STX<Y<b<b<d$e	!"mmEIIe,<iH
 */?D)I&).3G)H&)5:L:X0  2H  I  Isby-?-K!&8R!8#iiS9*/))ND*I*)26,,2G2G2X /1+%c<8"by-?-K 3 : :;M N 3 : :3 ?'T2(+ E#'2:2D2P$7$>$>?Q$R$7$>$>t$DE (W514 F8=sD,I	#'2:'9'E@EsL^4_	);u)D49	 3 : :9 EF #5  6M  PT  UX  PY  6Y  #Z  Z)< 
TI%|@S0TX]0]&($($=$=&RUW`>a$bE+/>>+C+CE+JL NN33D,G..2.G.G.X $ 8 8 E $ 8 8l S
T "c"b&9"4x}  yB  yB  CH  yI  KO  6P  #Q  Q,-fin-nooQHp +4 % !!"4quw|  xA  xA  BG  xH  JS  TU  JV  6W  #X  X ! $0 )#)*<  wF  HK  MV  WX  MY  >Z  +[  %[ ()s0   %!L <M 	M	.L==M	M8M33M8Nr   )F)FT)r   r   r   r   r   r   r  r   r   r   r   r   L  s    ;"
F?.Kpr   r   r(   )+
__future__r   r   rl   r2   collections.abcr   ansible.errorsr   r   ansible.inventory.groupr   r   ansible.parsing.utils.addressesr   ansible.pluginsr	   ansible.plugins.cacher
   rN   +ansible.module_utils.common.text.convertersr   r   )ansible.module_utils.parsing.convert_boolr   ansible.module_utils.sixr   ansible.templater   ansible.utils.displayr   ansible.utils.varsr   r   r^   r   r;   rV   rX   r   objectr   r   r   r   r   <module>r     s   $ #  	  # ; G 9 ) G K = 1 $ ) <
)8
EP&C!- C!L81 8%  % PApF Apr   