
    Vh9                    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 d dlmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ  eg d      Z eej6                        Z eej<                        ZejA                  e      Z! e ee!            jA                  e      Z" e ed            Z#d Z$ G d d      Z%y)    )annotationsN)AnsibleParserErrorAnsibleErrorAnsibleAssertionError)string_types)to_text)parse_kv
split_args)module_loaderaction_loader)Templar)add_internal_fqcns)Sentinel)	include_varsinclude_tasksinclude_roleimport_tasksimport_roleadd_hostgroup_byset_factmeta)r   r   r   r   r   c                    t        j                  | |      }|r$|j                  r|j                  r|j                  } t	        j                  | |      }|r|j                  s|}|S )N)collection_list)r   find_plugin_with_contextresolvedaction_pluginr   )action_or_moduler   module_contextcontexts       H/home/dcms/DCMS/lib/python3.12/site-packages/ansible/parsing/mod_args.py_get_action_contextr"   9   s^    ";;<L^mnN.11n6R6R)77445EWfgG'** N    c                  :    e Zd ZdZd	dZd Zd	dZd Zd Zd
dZ	y)ModuleArgsParsera?  
    There are several ways a module and argument set can be expressed:

    # legacy form (for a shell command)
    - action: shell echo hi

    # common shorthand for local actions vs delegate_to
    - local_action: shell echo hi

    # most commonly:
    - copy: src=a dest=b

    # legacy form
    - action: copy src=a dest=b

    # complex args form, for passing structured data
    - copy:
        src: a
        dest: b

    # gross, but technically legal
    - action:
        module: copy
        args:
          src: a
          dest: b

    # Standard YAML form for command-type modules. In this case, the args specified
    # will act as 'defaults' and will be overridden by any args specified
    # in one of the other formats (complex args under the action, or
    # parsed from the k=v string
    - command: 'pwd'
      args:
        chdir: '/tmp'


    This class has some of the logic to canonicalize these into the form

    - module: <module_name>
      delegate_to: <optional>
      args: <args>

    Args may also be munged for certain shell command parameters.
    Nc                   |i n|}t        |t              st        dt        |      z        || _        || _        ddlm} ddlm	} t        |j                        | _        | j                  j                  t        |j                               | j                  j                  ddg       t        | j                        | _        d | _        y )Nz3the type of 'task_ds' should be a dict, but is a %sr   )Task)Handlerlocal_actionstatic)
isinstancedictr   type_task_ds_collection_listansible.playbook.taskr'   ansible.playbook.handlerr(   setfattributes_task_attrsupdate	frozensetresolved_action)selftask_dsr   r'   r(   s        r!   __init__zModuleArgsParser.__init__s   s    "W'4('(]`del`m(mnn /.4t//0G$7$7 89 :;$T%5%56#r#   c                    t        |      }t        |      dkD  r'|d   j                         dj                  |dd       fS |d   j                         dfS )z
        when module names are expressed like:
        action: copy src=a dest=b
        the first part of the string is the name of the module
        and the rest are strings pertaining to the arguments.
           r    N )r
   lenstripjoin)r8   module_stringtokenss      r!   _split_module_stringz%ModuleArgsParser._split_module_string   sT     M*v;?1IOO%sxxqr
';<<1IOO%r**r#   c                   |i n|}t               }|rkt        |t              r.t        d      }|j	                  |      r||d<   n8t        d      t        |t               r|j                  |       nt        d      || j                  ||      }nW| j                  |      \  }}|rAd|v r=|j                  d      }t        |t              rt        |      }|j                  |       |r?|t        vr7|D ]2  }t        |      }|j                  d      s d|d	|d
}	t        |	       |r|j                  |       ||fS )zC
        arguments can be fuzzy.  Deal with all the forms.
        Nloader_variable_paramszComplex args containing variables cannot use bare variables (without Jinja2 delimiters), and must use the full variable style ('{{var_name}}')zAComplex args must be a dictionary or variable string ("{{var}}").args	_ansible_zKInvalid parameter specified beginning with keyword '_ansible_' for action 'z': 'z<'. The prefix '_ansible_' is reserved for internal use only.)r,   r+   r   r   is_templater   r5   _normalize_new_style_args_normalize_old_style_argspopr	   FREEFORM_ACTIONSr   
startswithr   )
r8   thingactionadditional_args
final_argstemplarrI   tmp_argsargerr_msgs
             r!   _normalize_parametersz&ModuleArgsParser._normalize_parameters   si   
 !0 7"_
 V
/<8!.&&75DJ12, .e f fOT2!!/2()lmm 11%@D!;;EBNVT $88F+h5'1HH% F"22 0cl>>+.eflepptuxt| }T T  'w//0 d#
##r#   c                    t        |t              r|}|S t        |t              r|t        v }t	        ||      }|S |d}|S t        dt        |      z  | j                        )ak  
        deals with fuzziness in new style module invocations
        accepting key=value pairs and dictionaries, and returns
        a dictionary of arguments

        possible example inputs:
            'echo hi', 'shell'
            {'region': 'xyz'}, 'ec2'
        standardized outputs like:
            { _raw_params: 'echo hi', _uses_shell: True }
        	check_rawN'unexpected parameter type in action: %sobj)r+   r,   r   rO   r	   r   r-   r.   )r8   rQ   rR   rI   r\   s        r!   rL   z*ModuleArgsParser._normalize_new_style_args   sz     eT"D  |,"22IEY7D  ]D  %%NQUV[Q\%\bfboboppr#   c                   d}d}t        |t              rf|j                         }d|v rN| j                  |d         \  }}|j                         }|t        v }|j                  t        ||             |d= ||fS t        |t              r-| j                  |      \  }}|t        v }t        ||      }||fS t        dt        |      z  | j                        )a\  
        deals with fuzziness in old-style (action/local_action) module invocations
        returns tuple of (module_name, dictionary_args)

        possible example inputs:
           { 'shell' : 'echo hi' }
           'shell echo hi'
           {'module': 'ec2', 'x': 1 }
        standardized outputs like:
           ('ec2', { 'x': 1} )
        Nmoduler[   r]   r^   )r+   r,   copyrD   rO   r5   r	   r   r   r-   r.   )r8   rQ   rR   rI   module_argsr\   s         r!   rM   z*ModuleArgsParser._normalize_old_style_args   s     eT"JJLE5 &*&?&?h&P#zz|"&66	H[IFGN ~ |,!66u=NVT"22IDI6D ~ %%NQUV[Q\%\bfboboppr#   c                <    d}d} j                   j                  dt              }t               } j                   j                  dt                     }d j                   v r& j                   d   } j	                  |||      \  }}d j                   v rN|t        d j                          j                   j                  dd	      }d
} j	                  |||      \  }}|7|s5t        | j                        }||j                  r|j                   _
        t         fd j                   j                         D              }|j                         D ]  \  }	}
d}d}|	t        v rd}n?|rd}n:	 t        |	 j                        }|j                  xr t        |j                         }|sW|t        d|d|	 j                         ||j                  r|j                   _
        |	}|
} j	                  |||      \  }} |[|rBt#        |j%                               d   }t        dj'                  |       j                         t        d j                         |j                  dd	      d	k7  rl|t(        vrdt+        d      }|j-                  d      }|j/                  |      r||d<   n0t        d|ddj1                  t2               j                         |||fS # t        $ r$}|j                   j                   |_        |d}~ww xY w)z
        Given a task in one of the supported forms, parses and returns
        returns the action, arguments, and delegate_to values for the
        task, dealing with all sorts of levels of fuzziness.
        Ndelegate_torI   rR   )rR   rS   r)   z.action and local_action are mutually exclusiver^   r>   	localhostc              3  n   K   | ],  \  }}|j                   vs|j                  d       r'||f . yw)with_N)r4   rP   ).0kvr8   s      r!   	<genexpr>z)ModuleArgsParser.parse.<locals>.<genexpr>:  sD       DdaQUQaQaHalmlxlx  zA  mBAq6  Ds   55	5FTzconflicting action statements: z, r   zwcouldn't resolve module/action '{0}'. This often indicates a misspelling, missing collection, or incorrect module path.z"no module/action detected in task._raw_paramsrF   rH   zthis task 'zD' has extra params, which is only allowed in the following modules: )r.   getr   r,   rY   r   r"   r/   r   resolved_fqcnr7   itemsBUILTIN_TASKSr   r_   boolredirect_listlistkeysformatRAW_PARAM_MODULESr   rN   rK   rA   RAW_PARAM_MODULES_SIMPLE)r8   skip_action_validationrQ   rR   re   rI   rS   r    non_task_dsitemvalueis_action_candidatee
bad_actionrU   
raw_paramss   `               r!   parsezModuleArgsParser.parse  s6    mm''x@v
 --++FDF; t}}$MM(+E55eF\k5lLFD T]]*!()Y_c_l_lmmMM%%nb9E%K55eF\k5lLFD&<)&$2G2GHG"w'7'7'.'<'<$
   Ddmm.A.A.C  D  D ',,. 	qKD%G"'}$&*#'&*#1$8M8MNG '.&6&6&V4@U@U;V#"%,X^`d-ekokxkxyy&7+;+;+2+@+@D(#99%`o9p7	q< >!+"2"2"45a8
( *fflflmwfx-1]]< < ))M-1]]< <XXmR(B.6AR3RT*G-0J"":.+5'((msuyu~u~  @X  vY  Z' ' k**O $ uu} $Gs   4K..	L7LL)NN)F)
__name__
__module____qualname____doc__r:   rD   rY   rL   rM   r    r#   r!   r%   r%   D   s)    +Z$&+9$v6$L^+r#   r%   )&
__future__r   ansible.constants	constantsCansible.errorsr   r   r   ansible.module_utils.sixr   +ansible.module_utils.common.text.convertersr   ansible.parsing.splitterr	   r
   ansible.plugins.loaderr   r   ansible.templater   ansible.utils.fqcnr   ansible.utils.sentinelr   r2   !_BUILTIN_RAW_PARAM_MODULES_SIMPLEMODULE_REQUIRE_ARGS_SIMPLEFREEFORM_ACTIONS_SIMPLEr6   MODULE_REQUIRE_ARGSrO   unionrx   rw   rq   r"   r%   r   r#   r!   <module>r      s   $ #  R R 1 ? 9 ? $ 1 + %( 
) 
% ! a::; Q223 <BBCZ[ 01IJKQQRbc , .  j+ j+r#   