o nnh$@sddlmZdZdZddlZddlZddlZdZzddlZdZ Wne y/e ZdZ Ynwddl m Z mZddlmZmZdd lmZd d Zd d ZedkrWedSdS)) annotationsa --- module: expect version_added: '2.0' short_description: Executes a command and responds to prompts description: - The M(ansible.builtin.expect) module executes a command and responds to prompts. - The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like C($HOME) and operations like C("<"), C(">"), C("|"), and C("&") will not work. options: command: description: - The command module takes command to run. required: true type: str creates: type: path description: - A filename, when it already exists, this step will B(not) be run. removes: type: path description: - A filename, when it does not exist, this step will B(not) be run. chdir: type: path description: - Change into this directory before running the command. responses: type: dict description: - Mapping of prompt regular expressions and corresponding answer(s). - Each key in O(responses) is a Python regex U(https://docs.python.org/3/library/re.html#regular-expression-syntax). - The value of each key is a string or list of strings. If the value is a string and the prompt is encountered multiple times, the answer will be repeated. Provide the value as a list to give different answers for successive matches. required: true timeout: type: raw description: - Amount of time in seconds to wait for the expected strings. Use V(null) to disable timeout. default: 30 echo: description: - Whether or not to echo out your response strings. default: false type: bool requirements: - python >= 2.6 - pexpect >= 3.3 extends_documentation_fragment: action_common_attributes attributes: check_mode: support: none diff_mode: support: none platform: support: full platforms: posix notes: - If you want to run a command through the shell (say you are using C(<), C(>), C(|), and so on), you must specify a shell in the command such as C(/bin/bash -c "/path/to/something | grep else"). - Case insensitive searches are indicated with a prefix of C(?i). - The C(pexpect) library used by this module operates with a search window of 2000 bytes, and does not use a multiline regex match. To perform a start of line bound match, use a pattern like ``(?m)^pattern`` - The M(ansible.builtin.expect) module is designed for simple scenarios. For more complex needs, consider the use of expect code with the M(ansible.builtin.shell) or M(ansible.builtin.script) modules. (An example is part of the M(ansible.builtin.shell) module documentation). - If the command returns non UTF-8 data, it must be encoded to avoid issues. One option is to pipe the output through C(base64). seealso: - module: ansible.builtin.script - module: ansible.builtin.shell author: "Matt Martz (@sivel)" a - name: Case insensitive password string match ansible.builtin.expect: command: passwd username responses: (?i)password: "MySekretPa$$word" # you don't want to show passwords in your logs no_log: true - name: Match multiple regular expressions and demonstrate individual and repeated responses ansible.builtin.expect: command: /path/to/custom/command responses: Question: # give a unique response for each of the 3 hypothetical prompts matched - response1 - response2 - response3 # give the same response for every matching prompt "^Match another prompt$": "response" - name: Multiple questions with responses ansible.builtin.expect: command: /path/to/custom/command responses: "Please provide your name": - "Anna" "Database user": - "{{ db_username }}" "Database password": - "{{ db_password }}" NTF) AnsibleModulemissing_required_lib)to_bytes to_native)check_type_intcs"dd|Dfdd}|S)Ncss"|] }dt|dVqdS)%s  N)rrstrip).0rr A/usr/local/lib/python3.10/dist-packages/ansible/modules/expect.py s z#response_closure..cs<ztWStyjd|ddfdYdSw)Nz0No remaining responses for '%s', output was '%s'child_result_listmsg)next StopIteration fail_json)infomodulequestionresp_genr rwrappeds   z!response_closure..wrappedr )rr responsesrr rrresponse_closures rcCsXtttddtddtddtddtdddtddd td d d d d }ts1|jtdtd|jd}|jd}|jd}|jd}|jd}|jd}|durzzt|}Wn tyy}z|jdt |d|dWYd}~nd}~ww|jd}t} | D]\} } t | t rt || | } n dt| d} | | t| <q|dkr|jddd|rtj|}t||rtj|r|j|d |d d!d"|rtj|s|j|d#|d d!d"tj} z/ztj||d| ||dd$\}}Wntytj||d| dd|dtj|d% \}}YnwWnBttfy<}z|jd&tjt|fdWYd}~n%d}~wtj y\}z|jd't|t!"dWYd}~nd}~wwtj}|| }|durmd(}t|t|d)|t#| t#|t#|dd*}|dur|jd.d+d,i|n|d!kr|jd.d+d-i||jd.i|dS)/NT)requiredpath)typedict)r!rraw)r!defaultboolF)commandchdircreatesremovesrtimeoutecho) argument_specpexpect)r exceptionr(r'r)r*rr+zargument 'timeout' is of type z' and we were unable to convert to int: rr,rr zno command given)rcrzskipped, since %s existsr)cmdstdoutchangedr2z skipped, since %s does not exist)r+withexitstatuseventscwdr,encoding) r+r6r7 extra_argslogfiler8env_spawnr,z_Insufficient version of pexpect installed (%s), this module requires pexpect>=3.3. Error was %sz%sz )r3r4r2startenddeltar5rzcommand exceeded timeoutznon-zero return coder )$rr" HAS_PEXPECTrrPEXPECT_IMP_ERRparamsr TypeErrorr!items isinstancelistrrr striposr abspathr(exists exit_jsondatetimenowr.run_runspawnAttributeError __version__rExceptionPexpect traceback format_excstr)rr(argsr)r*rr+ter,r7keyvalueresponsestartdb_outr2eenddrAresultr r rmains           (            &     rc__main__) __future__r DOCUMENTATIONEXAMPLESrNrJrVrCr.rB ImportErrorrWansible.module_utils.basicrr+ansible.module_utils.common.text.convertersrr&ansible.module_utils.common.validationrrrc__name__r r r rs, O!  z