
    Vh$                        d dl mZ dZdZd dlZd dlZd dlZdZ	 d dlZdZ	d dlmZmZ d dlmZmZ d d	lmZ d
 Zd Zedk(  r e        yy# e
$ r  ej                         ZdZ	Y Dw xY w)    )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 C((?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)AnsibleModulemissing_required_lib)to_bytes	to_native)check_type_intc                ,     d |D         fd}|S )Nc              3  V   K   | ]!  }d t        |      j                  d      z   # yw)   %s
   
N)r   rstrip).0rs     F/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/expect.py	<genexpr>z#response_closure.<locals>.<genexpr>   s$     G(1+,,U33Gs   ')c                x    	 t              S # t        $ r" j                  dd| d   d   d       Y y w xY w)NzNo remaining responses for 'z', output was 'child_result_list'msg)nextStopIteration	fail_json)infomodulequestionresp_gens    r   wrappedz!response_closure.<locals>.wrapped   sM    	B>! 	B"*"&':";B"?"A B	Bs   
 (99 )r   r   	responsesr   r   s   ``  @r   response_closurer"      s    GYGHB N    c                 v   t        t        t        d      t        d      t        d      t        d      t        dd      t        dd	      t        d
d	                  } t        s | j                  t	        d      t
               | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|	 t        |      }| j                  d   }t               }	|j                         D ]N  \  }
}t        |t              rt        | |
|      }ndt        |      j                  d      z  }||	t        |
      <   P |j!                         dk(  r| j                  dd       |r4t"        j$                  j'                  |      }t#        j(                  |       |r7t"        j$                  j+                  |      r| j-                  |d |z  dd!"       |r7t"        j$                  j+                  |      s| j-                  |d#|z  dd!"       t.        j.                  j1                         }	 	 t3        j4                  ||d|	||d $      \  }}t.        j.                  j1                         }||z
  }d)}t        |t?        |      j                  d*      tG        |      tG        |      tG        |      d+      }| | j                  d/d,d-i| n|d!k7  r | j                  d/d,d.i|  | j,                  d/i | y # t        $ r,}| j                  dt        |       d|        Y d }~fd }~ww xY w# t        $ r4 t3        j6                  ||d|	d d |d t2        j8                  |%
      \  }}Y #w xY w# t        t:        f$ r:}| j                  d&t2        j<                  d't?        |             Y d }~kd }~wt2        j@                  $ r<}| j                  d(t?        |      z  tC        jD                                Y d }~d }~ww xY w)0NT)requiredpath)typedict)r'   r%   raw   )r'   defaultboolF)commandchdircreatesremovesr!   timeoutecho)argument_specpexpect)r   	exceptionr.   r-   r/   r0   r!   r1   zargument 'timeout' is of type z' and we were unable to convert to int: r   r2   r   r       zno command given)rcr   zskipped, since %s existsr   )cmdstdoutchangedr8   z skipped, since %s does not exist)r1   withexitstatuseventscwdr2   encoding)	r1   r<   r=   
extra_argslogfiler>   env_spawnr2   z+Insufficient version of pexpect installed (z0), this module requires pexpect>=3.3. Error was z%sr#   z
)r9   r:   r8   startenddeltar;   r   zcommand exceeded timeoutznon-zero return coder    )$r   r(   HAS_PEXPECTr   r   PEXPECT_IMP_ERRparamsr   	TypeErrorr'   items
isinstancelistr"   r   r   striposr&   abspathr.   exists	exit_jsondatetimenowr4   run_runspawnAttributeError__version__r   ExceptionPexpect	traceback
format_excstr)r   r.   argsr/   r0   r!   r1   ter2   r=   keyvalueresponsestartdb_outr8   eenddrF   results                       r   mainrh      s   $'F#f%f%6eR0651

F 1)<#2 	 	4 MM'"E==#DmmI&GmmI&Gk*ImmI&G	~$W-G == DVFoo' )
UeT"'U;H%!7!7!>>H (x}) zz|rC%78&
 77>>'"1G;	    ww~~g&9GC	   ""$FT	0D'$+1u4-13IE2.   "D6ME}&&v.&kI%jF 
zB7B6B	q>3>v>Fv{  	~#A$w-Pwxzw{!|}}	~h  	0  T74,2tT).D*.0IE2		0 ~& S
 	/6/B/BIaLR 	S 	S ## TTIaL0I<P<P<RSSTsT   )L* M" *	M3!MM"9NN" NN" "P81/O&&P8<1P33P8__main__)
__future__r   DOCUMENTATIONEXAMPLESrS   rO   r[   rH   r4   rG   ImportErrorr\   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r   &ansible.module_utils.common.validationr   r"   rh   __name__r    r#   r   <module>rr      s    #M^B  	 K
 K K Awt zF e  *i**,OKs   A
 
A%$A%