
    Vht              	      @   d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZ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 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) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ ddgZ0d Z1 G d deee%ee      Z2y)    )annotationsN)	ContainerMappingSetSequence)MappingProxyType)	constants)AnsibleErrorAnsibleParserErrorAnsibleAssertionError)to_text)binary_type	text_type)FieldAttribute)Base)CollectionSearch)Conditional)Delegatable)load_list_of_blocks)RoleMetadata)Taggable)add_all_plugin_dirs)AnsibleCollectionConfig)
is_subpath)Sentinel)combine_varsRolehash_paramsc                ^   t        | t              rst        | t        t        f      s]t        | t              r	 t        | j                               }|S t        | t        t        f      r	 t        |       }|S t        |       }|S t        | f      S # t        $ rM t               }| j                         D ]!  \  }}|j                  |t        |      f       # t        |      }Y |S w xY w# t        $ r: t               }| D ]  }|j                  t        |              t        |      }Y |S w xY w)a  
    Construct a data structure of parameters that is hashable.

    This requires changing any mutable data structures into immutable ones.
    We chose a frozenset because role parameters have to be unique.

    .. warning::  this does not handle unhashable scalars.  Two things
        mitigate that limitation:

        1) There shouldn't be any unhashable scalars specified in the yaml
        2) Our only choice would be to return an error anyway.
    )
isinstancer   r   r   r   	frozensetitems	TypeErrorsetaddr   r   r   )params
new_paramskvs       N/home/dcms/DCMS/lib/python3.12/site-packages/ansible/playbook/role/__init__.pyr   r   3   s/     &)$ZK@X-Yfg&3&v||~6
*  h03&v.
  #6*J fY1  3 U
"LLN 8DAqNNA{1~#678 'z2
 )3  3 U
 3ANN;q>23 'z2
 3s$   B *C) AC&%C&)?D,+D,c                       e Zd Zd fd	Zd ZddZd Zd Zd Ze	dd       Z
d dZd	 Zd
 Zd Zd!dZd Zd Zd Zd Zd dZd!dZd dZd"dZd Zd Zd Zd dZd Zd dZd fd	Zd fd	Zd Z  xZ!S )#r   c                H   d | _         d | _        d | _        t               | _        d | _        || _        |8t        j                  j                  d      \  }}|dk(  r|| _
        n| | _
        n|| _
        t               | _        || _        g | _        g | _        d | _        g | _        g | _        d | _        t               | _        t               | _        t               | _        t               | _        || _        |i }|| _        || _        d | _        t8        t:        | {          y )NDEFAULT_PRIVATE_ROLE_VARSdefault)
_role_name
_role_path_role_collectiondict_role_params_loaderstaticCconfigget_config_value_and_originpublicr   	_metadata_play_parents_dependencies_all_dependencies_task_blocks_handler_blocks_compiled_handler_blocks_default_vars
_role_vars_had_task_run
_completed_should_validate_from_filesfrom_include_hashsuperr   __init__)
selfplay
from_filesrH   validater9   r5   global_privateorigin	__class__s
            r*   rK   zRole.__init__e   s    $ F >%&XX%I%IJe%f"NF"$"00 DK%
!%!(,%!V&!V& (J% )
dD"$    c                "    | j                         S N)get_namerL   s    r*   __repr__zRole.__repr__   s    }}rS   c                z    |r.dj                  d | j                  | j                  fD              S | j                  S )N.c              3  &   K   | ]	  }|s|  y wrU    ).0xs     r*   	<genexpr>z Role.get_name.<locals>.<genexpr>   s     U!STAUs   )joinr1   r/   )rL   include_role_fqcns     r*   rV   zRole.get_name   s3    88U(=(=t'OUUUrS   c                T    t         j                  j                  | j                        S rU   )ospathrealpathr0   rW   s    r*   get_role_pathzRole.get_role_path   s    ww00rS   c                j   | j                   r| j                   S t        | j                         | j                         t        | j	                               | j
                  | j                  t        | j                        t        | j                        | j                  d      | _         | j                   S )N)namerd   r&   whentagsrN   varsrH   )
rI   r   rV   rf   get_role_paramsri   rj   rG   rk   rH   rW   s    r*   _get_hash_dictzRole._get_hash_dict   s    ::::%**,*4+?+?+AB				.t/?/?@(3 $ 1 1	

 zzrS   c                f    t        |t              sy| j                         |j                         k(  S NF)r    r   rm   )rL   others     r*   __eq__zRole.__eq__   s,    %&""$(<(<(>>>rS   c                X   |i }	 t        ||||||      }|j                  | |       |j                         }	|	|j                  vrg |j                  |	<   ||j                  |	   vr|j                  |	   j	                  |       |S # t
        $ r t        d| j                        w xY w)N)rM   rN   rH   rO   r9   r5   )parent_rolezvA recursion loop was detected with the roles specified. Make sure child roles do not have dependencies on parent roles)obj)r   _load_role_datarf   
role_cacheappendRuntimeErrorr
   _ds)
role_includerM   rs   rN   rH   rO   r9   r5   r	role_paths
             r*   loadz	Role.load   s    J	5
 $:L[clr  |B  CAlD)I/-/	* 	22	*11!4H 	5   X#/#3#35 5	5s   BB !B)c           
         |j                    _        |j                          _        |j                   _        |j                          _        |j                          _        |j                          _
        |r j                  |        j                  D ]$  }t         d| t        |d| t                     &  j!                  d j"                  j%                  d      d       _         j&                  i  _        n2t)         j&                  t*              st-        d j                  z         j!                  d j"                  j%                  d      d       _         j.                  i  _        n2t)         j.                  t*              st-        d j                  z         j!                  d      }|rGt1        j2                  |  j                   j                  	       _         j7                          _        g  _         j                  r' j:                  j=                  d
 j                         nCt>        j@                  }|r j:                  j=                  d
|       tC         j                          j4                  j:                  r8 j:                  jE                   fd j4                  j:                  D                j:                  rG j                  rdnd}d j:                  vr)d j:                  vr j:                  jG                  |        j!                  d j"                  j%                  d            } jH                  r" jK                         } jM                  ||      }|r4	 tO        | jP                    j                   j                         _)         j!                  d j"                  j%                  d            }
|
r6	 tO        |
 jP                   d j                   j                         _+        y y # tT        $ r }	t-        d j                  z  ||	      d }	~	ww xY w# tT        $ r }	t-        d j                  z  |
|	      d }	~	ww xY w)N_rk   T)main	allow_dirzKThe vars/main.yml file for role '%s' must contain a dictionary of variablesdefaultszOThe defaults/main.yml file for role '%s' must contain a dictionary of variablesmeta)ownervariable_managerloaderr   c              3  @   K   | ]  }|j                   vs|  y wrU   )collections)r]   crL   s     r*   r_   z'Role._load_role_data.<locals>.<genexpr>  s     $h1aW[WgWgNgQ$hs   zansible.builtinzansible.legacytasksr   )rM   roler   r   zBThe tasks/main.yml file for role '%s' must contain a list of tasks)rt   orig_exchandlers)rM   r   use_handlersr   r   zEThe handlers/main.yml file for role '%s' must contain a list of tasks),r   r/   rf   r0   r1   rl   r3   get_variable_manager_variable_manager
get_loaderr4   
add_parentfattributessetattrgetattrr   _load_role_yamlrG   getrC   r    r   r   rB   r   r}   r:   _load_dependenciesr=   r   insertr   default_collectionr   extendrw   rF   _get_role_argspecs_prepend_validation_taskr   r;   r?   AssertionErrorr@   )rL   rz   rs   	attr_namemetadatar   default_append_collection	task_datarole_argspecsehandler_datas   `          r*   ru   zRole._load_role_data   sN   &++&446 , = =(88:!-!B!B!D#..0OOK( )) 	]IDAi[/7<1YKRZ+[\	] ..vD<L<L<P<PQW<Xdh.i??" DODOOW5$%ruy  vE  vE  &E  F  F!11*4CSCSCWCWXbCcos1t%!#DD..8$%vy}  zI  zI  &I  J  J ''/)..xtVZVlVluy  vB  vB  CDN!%!8!8!:D    ##At'<'<=!8!K!K!  ''+=>0 >>%%##$h0J0J$hi =A=R=R(9Xh% (8(88=MUYUeUe=e  ''(AB((t7G7G7K7KG7T(U	   335M55iOID$7	

Y]fjfrfr  FJ  F\  F\  %]!
 ++JT=M=M=Q=QR\=]+^G':<djj_crv  @D  @L  @LLPLbLb(d$  " D()mptpp)-6D DD " G()psw  tC  tC  *C-9AG GGs0   3P %4Q 	Q$P??Q	Q0Q++Q0c                l   t         j                  j                  | j                  dd      }t        j
                  D ]M  }||z   }| j                  j                  |      s$| j                  dd      }	 |j                  d      xs i c S  t        | j                  di       S # t        $ r i cY c S w xY w)a  Get the role argument spec data.

        Role arg specs can be in one of two files in the role meta subdir: argument_specs.yml
        or main.yml. The former has precedence over the latter. Data is not combined
        between the files.

        :returns: A dict of all data under the top-level ``argument_specs`` YAML key
            in the argument spec file. An empty dict is returned if there is no
            argspec data.
        r   argument_specsr   )rc   rd   r`   r0   r6   YAML_FILENAME_EXTENSIONSr4   path_existsr   r   AttributeErrorr   r:   )rL   base_argspec_pathext	full_pathr   s        r*   r   zRole._get_role_argspecs"  s     GGLL&BRS-- 	C)C/I||''	2!%!5!5fCS!5!T)--.>?E2E	 t~~'7<< & Is   4B##B32B3c                    |rW| j                   j                  dd      }|j                  |      }|r(| j                  ||      }|sg }|j                  d|       |S )a  Insert a role validation task if we have a role argument spec.

        This method will prepend a validation task to the front of the role task
        list to perform argument spec validation before any other tasks, if an arg spec
        exists for the entry point. Entry point defaults to `main`.

        :param task_data: List of tasks loaded from the role.
        :param argspecs: The role argument spec data dict.

        :returns: The (possibly modified) task list.
        r   r   r   )rG   r   _create_validation_taskr   )rL   r   argspecs
entrypointentrypoint_arg_specvalidation_tasks         r*   r   zRole._prepend_validation_task>  sg      ))--gv>J"*,,z":""&">">?RT^"_
 ! "I  O4rS   c                    d|z  }d|v r|dz   |d   z   }d|j                  di       | j                  d| j                  || j                  dd|d	gd
S )a  Create a new task data structure that uses the validate_argument_spec action plugin.

        :param argument_spec: The arg spec definition for a particular role entry point.
            This will be the entire arg spec for the entry point as read from the input file.
        :param entrypoint_name: The name of the role entry point associated with the
            supplied `argument_spec`.
        z*Validating arguments against arg spec '%s'short_descriptionz - z&ansible.builtin.validate_argument_specoptionsr   )typerh   argument_spec_namerd   )moduleargument_specprovided_argumentsvalidate_args_contextalways)actionrh   rj   )r   r3   r/   r0   )rL   r   entrypoint_name	task_names       r*   r   zRole._create_validation_task[  s}     A?R	-/!E)M:M,NNI C!.!2!29b!A&*&7&7" OO*9 OO	* J
 	
rS   c                   d}t         j                  j                  | j                  |      }| j                  j                  |      r| j                  j                  |      rg d}|d}|j                  d       n|}|j                  dd       | j                  j                  ||||      }|r|D ]  }	t        |	|      s1t        dt        |	      d| j                  dt        |      d	      | j                  j                  |	      }
|
s^|t        |
t               rt#        ||
      }n|
}|r |S  |S |t        d
|d|      |S )aj  
        Find and load role YAML files and return data found.
        :param subdir: subdir of role to search (vars, files, tasks, handlers, defaults)
        :type subdir: string
        :param main: filename to match, will default to 'main.<ext>' if not provided.
        :type main: string
        :param allow_dir: If true we combine results of multiple matching files found.
                          If false, highlander rules. Only for vars(dicts) and not tasks(lists).
        :type allow_dir: bool

        :returns: data from the matched file(s), type can be dict or list depending on vars or tasks.
        N)z.ymlz.yamlz.jsonr    r   zFailed loading 'z' for role (z/) as it is not inside the expected role path: ''z'Could not find specified file in role: /)rc   rd   r`   r0   r4   r   is_directoryrw   r   find_vars_filesr   r   r   r/   load_from_filer    r   r   )rL   subdirr   r   data	file_path
extensions_mainfound_filesfoundnew_datas              r*   r   zRole._load_role_yamlz  sJ    GGLL&9	<<##I.4<<3L3LY3W3J |!!"%!!!R( ,,66y%U^_K( "E%eY7029%.$//SZ[dSe2g h h  $||::5AH+
8W0M#/h#?D#+D  )! +"* 	 !([acg)hiirS   c                    g }| j                   j                  D ]@  }t        j                  || j                  | | j
                        }|j                  |       B |S )zr
        Recursively loads role dependencies from the metadata list of
        dependencies, if it exists
        )rM   rs   r5   )r:   dependenciesr   r}   r;   r5   rw   )rL   depsrz   r{   s       r*   r   zRole._load_dependencies  sU      NN77 	L		,TZZTRVR]R]	^AKKN	 rS   c                    t        |t              s
t               || j                  vr| j                  j	                  |       yy)z/ adds a role to the list of this roles parents N)r    r   r   r<   rw   )rL   rs   s     r*   r   zRole.add_parent  s8    +t,'))dmm+MM  - ,rS   c                    | j                   S rU   )r<   rW   s    r*   get_parentszRole.get_parents  s    }}rS   c                    g }| j                   D ]2  }|j                  |j                                |j                  |       4 |S rU   )r<   r   get_dep_chainrw   )rL   	dep_chainparents      r*   r   zRole.get_dep_chain  sG    	mm 	%FV1134V$	% rS   c                    |g n|}t               }| j                         D ]  }t        ||j                               } |r|D ]  }t        ||j                        } t        || j                        }|S rU   )r2   get_all_dependenciesr   get_default_varsrB   )rL   r   default_varsdepr   s        r*   r   zRole.get_default_vars  s    #+B	v,,. 	NC'c6J6J6LML	N# P+L&:N:NOP#L$2D2DErS   c                    |g n|}t               }|r5|D ]0  }|st        ||j                        }t        ||j                        }2 |S rU   )r2   r   rk   rC   )rL   r   only_exportsinherited_varsr   s        r*   get_inherited_varszRole.get_inherited_vars  sV    #+B	# Q#%1.&++%NN!-nf>O>O!PQ rS   c                    |g n|}i }|r|D ]  }t        ||j                        } t        || j                        }|S rU   )r   r3   )rL   r   r&   r   s       r*   rl   zRole.get_role_params  sP    #+B	# C%ff.A.ABCfd&7&78rS   c                h   |g n|}i }| j                  ||      }g }| j                         D ]5  }||vst        ||j                  dd            }|j	                  |       7 t        || j
                        }|s4|rt        || j                  |            }t        || j                        }|S )N)r   FT)include_paramsr   )r   )r   r   r   get_varsrw   rC   rl   rk   )rL   r   r   r   all_varsseenr   s          r*   r   zRole.get_vars  s    #+B	 **9<*P ,,. 	!C $'#,,ebf,2ghC 	!  $//:'$2F2FQZ2F2[\ $Hdii8HrS   c                     | j                   d d  S rU   )r=   rW   s    r*   get_direct_dependencieszRole.get_direct_dependencies  s    !!!$$rS   c                    | j                   gg | _         | j                         D ]M  }|j                         D ]  }| j                   j                  |        | j                   j                  |       O | j                   S )z
        Returns a list of all deps, built recursively from all child dependencies,
        in the proper order in which they should be executed or evaluated.
        )r>   r   r   rw   )rL   r   	child_deps      r*   r   zRole.get_all_dependencies  s~    
 !!)%'D"335 3!$!9!9!; =I**11)<=&&--c23
 %%%rS   c                     | j                   d d  S rU   )r?   rW   s    r*   get_task_blockszRole.get_task_blocks&  s      ##rS   c                N   | j                   r| j                   S g x| _         }|g }|| gz   }| j                         D ]&  }|j                  ||      }|j                  |       ( | j                  D ]1  }|j                         }||_        ||_        |j                  |       3 |S )NrM   r   )	rA   r   get_handler_blocksr   r@   copy
_dep_chainr;   rw   )	rL   rM   r   
block_listnew_dep_chainr   
dep_blocks
task_blocknew_task_blocks	            r*   r   zRole.get_handler_blocks)  s     ((000577%
 I!TF*//1 	*C//T]/SJj)	* .. 	.J'__.N(5N%#'N n-		. rS   c                2    |j                   | j                  v S )zs
        Returns true if this role has been iterated over completely and
        at least one task was run
        )rh   rE   )rL   hosts     r*   has_runzRole.has_runE  s     yyDOO++rS   c                <   ddl m} ddlm} g }|g }|| gz   }| j	                         }|D ]&  }|j                  ||      }	|j                  |	       ( | j                  D ]1  }
|
j                         }||_	        ||_
        |j                  |       3  ||      }| j                  |_        | |_        | j                  |_        d|_         ||      }| |_        d|_        d	d
i|_        d|_        dg|_        d|_        |g|_        |j                  |       |S )a  
        Returns the task list for this role, which is created by first
        recursively compiling the tasks for all direct dependencies, and
        then adding on the tasks for this role.

        The role compile() also remembers and saves the dependency chain
        with each task, so tasks know by which route they were found, and
        can correctly take their parent's tags/conditionals into account.
        r   )Block)Taskr   )rM   F)blockr   _raw_paramsrole_completeTr   )ansible.playbook.blockr   ansible.playbook.taskr   r   compiler   r?   r   r   r;   rw   r4   _roler   run_oncer   argsimplicitrj   ri   r   )rL   rM   r   r   r   r   r   r   r   r   r   r   	eor_blockeor_tasks                 r*   r  zRole.compileM  s4    	1.
 I!TF*++- 	*C$-HJj)	* ++ 	.J'__.N(5N%#'N n-		. t$	 LL		&*&<&<	#"	i( &8 !
#*	)$rS   c                \   t         t        |          }| j                  |d<   | j                  |d<   | j
                  |d<   | j                  |d<   | j                  |d<   | j                  j                         |d<   | j                  j                         |d<   | j                  j                         |d<   |r;g }| j                         D ]!  }|j                  |j                                # ||d	<   g }| j                  D ]#  }|j                  |j                  d
             % ||d<   |S )Nr/   r0   rC   r3   rB   rD   rE   r:   r=   Finclude_depsr<   )rJ   r   	serializer/   r0   rC   r3   rB   rD   r   rE   r:   r   rw   r<   )rL   r  resr   r   parentsr   rR   s          r*   r  zRole.serialize  s"   D$)+ OOL OOL OOL"//N#11O#11668O OO002L>>335KD446 .DNN,-.#'C mm 	AFNN6+++?@	A!J
rS   c                |   |j                  dd      | _        |j                  dd      | _        |j                  dt                     | _        |j                  dt                     | _        |j                  dt                     | _        |j                  dt                     | _        |j                  dt                     | _        |rRg }|j                  d	g       D ].  }t               }|j                  |       |j                  |       0 t        | d	|       |j                  d
g       }g }|D ]0  }t               }|j                  |d       |j                  |       2 t        | d
|       |j                  d      }	|	r"t               }
|
j                  |	       |
| _        t        t        | +  |       y )Nr/   r   r0   rC   r3   rB   rD   rE   r=   r<   Fr  r:   )r   r/   r0   r2   rC   r3   rB   rD   rE   r   deserializerw   r   r   r:   rJ   )rL   r   r  r   r   r{   parent_datar  r   metadata_datamrR   s              r*   r  zRole.deserialize  sk   ((<4((<4((<8 HH^TV<!XXotv>!XXotv>((<8Dxx4 Fc"A D/40hhz2.! 	FAMM&uM5NN1	 	j'*-AMM-(DNdD%d+rS   c                    || _         | j                  D ]  }|j                  |        | j                         D ]  }|j                  |        y rU   )r4   r<   
set_loaderr   )rL   r   r   r   s       r*   r  zRole.set_loader  sN    mm 	&Ff%	&//1 	#CNN6"	#rS   )NNFTNT)TrU   ro   )NTF)"__name__
__module____qualname__rK   rX   rV   rf   rm   rq   staticmethodr}   ru   r   r   r   r   r   r   r   r   r   r   rl   r   r   r   r   r   r   r  r  r  r  __classcell__)rR   s   @r*   r   r   c   s    *%X
1"? 5 54QGf=8:
>4l.

B%&$8,0d4,B#rS   )3
__future__r   rc   collections.abcr   r   r   r   typesr   ansibler	   r6   ansible.errorsr
   r   r   +ansible.module_utils.common.text.convertersr   ansible.module_utils.sixr   r   ansible.playbook.attributer   ansible.playbook.baser   !ansible.playbook.collectionsearchr   ansible.playbook.conditionalr   ansible.playbook.delegatabler   ansible.playbook.helpersr   ansible.playbook.role.metadatar   ansible.playbook.taggabler   ansible.plugins.loaderr   ansible.utils.collection_loaderr   ansible.utils.pathr   ansible.utils.sentinelr   ansible.utils.varsr   __all__r   r   r\   rS   r*   <module>r3     ss   $ # 	 = = " " R R ? ; 5 & > 4 4 8 7 . 6 C ) + +=
!- `\	#4h(8+ \	#rS   