o nnh8@sddlmZddlmZddlmZmZmZddl m Z ddl m Z ddl mZmZddlmZmZddlmZdd lmZdd lmZeejZeed Zeed Zd dZGdddZ dS)) annotationsN)AnsibleParserError AnsibleErrorAnsibleAssertionError) string_types)to_text)parse_kv split_args) module_loader action_loader)Templar)add_internal_fqcns)Sentinel) include include_vars include_tasks include_role import_tasks import_roleadd_hostgroup_byset_factmeta)rrrrrcCsDtj||d}|r|jr|jr|j}tj||d}|r|js |}|S)N)collection_list)r find_plugin_with_contextresolved action_pluginr )action_or_modulermodule_contextcontextr C/usr/local/lib/python3.10/dist-packages/ansible/parsing/mod_args.py_get_action_context8s r"c@sFeZdZdZdddZddZdddZd d Zd d ZdddZ dS)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: delegate_to: args: Args may also be munged for certain shell command parameters. NcCs|durin|}t|tstdt|||_||_ddlm}ddlm }t |j |_ |j t |j |j ddgt|j |_ d|_dS)Nz3the type of 'task_ds' should be a dict, but is a %sr)Task)Handler local_actionstatic) isinstancedictrtype_task_ds_collection_listansible.playbook.taskr$ansible.playbook.handlerr%set fattributes _task_attrsupdate frozensetresolved_action)selftask_dsrr$r%r r r!__init__rs      zModuleArgsParser.__init__cCsBt|}t|dkr|dd|ddfS|ddfS)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 lenstripjoin)r5 module_stringtokensr r r!_split_module_strings z%ModuleArgsParser._split_module_stringc Cs|durin|}t}|r4t|tr%tdd}||r!||d<ntdt|tr0||ntd|dur?|||}n ||\}}|r_d|vr_| d}t|trZt |}|||r|t vr|D]}t |}| drd|d |d } t| qg|r||||fS) zC arguments can be fuzzy. Deal with all the forms. Nloader_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(rr is_templaterr2_normalize_new_style_args_normalize_old_style_argspoprFREEFORM_ACTIONSr startswithr) r5thingactionadditional_args final_argstemplarrDtmp_argsargerr_msgr r r!_normalize_parameterss<             z&ModuleArgsParser._normalize_parameterscCsZt|tr |}|St|tr|tv}t||d}|S|dur"d}|Stdt||jd)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: %sobj)r(r)rrJrrr*r+)r5rLrMrDrVr r r!rGs   z*ModuleArgsParser._normalize_new_style_argscCsd}d}t|tr2|}d|vr.||d\}}|}|tv}|t||d|d=||fSt|trL||\}}|tv}t||d}||fStdt ||j d)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} ) NmodulerUrWrX) r(r)copyr@rJr2rrrr*r+)r5rLrMrD module_argsrVr r r!rHs"    z*ModuleArgsParser._normalize_old_style_argsFc sdd}d}jdt}t}jdt}djvr*jd}j|||d\}}djvrM|dur:tdjdjdd }d }j|||d\}}|durd|sdt|j}|durd|jrd|j _ tfd d j D}| D]f\} } d}d } | t vrd} n-|rd} n(zt| j}Wnt y} z | jdurj| _| d} ~ ww|jot|j} | r|durtd|| fjd|dur|jr|j _ | }| }j|||d\}}qv|dur|rt|d} td| jdtdjd|dd d kr-|tvr-tdd}|d}||r||d<ntd|dtfjd|||fS)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_torDrM)rMrNr&z.action and local_action are mutually exclusiverXr: localhostc3s0|]\}}|jvr|ds||fVqdS)with_N)r1rK).0kvr5r r! 9s.z)ModuleArgsParser.parse..FTz%conflicting action statements: %s, %srzwcouldn't resolve module/action '{0}'. This often indicates a misspelling, missing collection, or incorrect module path.z"no module/action detected in task. _raw_paramsrArCzSthis task '%s' has extra params, which is only allowed in the following modules: %sz, )r+getrr)rTrr"r,r resolved_fqcnr4items BUILTIN_TASKSrrYbool redirect_listlistkeysformatRAW_PARAM_MODULESr rIrFr=)r5skip_action_validationrLrMr]rDrNr non_task_dsitemvalueis_action_candidatee bad_actionrP raw_paramsr rcr!parses           zModuleArgsParser.parse)NN)F) __name__ __module__ __qualname____doc__r7r@rTrGrHrxr r r r!r#Cs - ;&r#)! __future__ransible.constants constantsCansible.errorsrrransible.module_utils.sixr+ansible.module_utils.common.text.convertersransible.parsing.splitterrr ansible.plugins.loaderr r ansible.templater ansible.utils.fqcnr ansible.utils.sentinelrr3MODULE_REQUIRE_ARGSrJunionrorir"r#r r r r!s