expect module does not work with multiple prompts

2,762 views
Skip to first unread message

Edgars

unread,
Sep 4, 2015, 5:53:57 AM9/4/15
to Ansible Project
Hi

I have this task:

  tasks:
   
- expect:
        command
: /path/to/bash/script.sh
        responses
:
         
'#? ': '5'
         
"[y/N]": 'y'

expect module correctly responds to first prompt, but not to second. It responds to second prompt with '5' when it should respond with 'y'.

When I enable echo=yes, I see:

...
"#? 5"

"[y/N]5"

...

You can easily reproduce it with bash script and two echo/read

Edgars

Matt Martz

unread,
Sep 4, 2015, 8:01:06 AM9/4/15
to ansible...@googlegroups.com
I believe the problem is that pexpect treats all matches as regex.  You have specified '#?' which means the # is optional, effectively making the regex an empty string.

An empty string matches anything, so pexpect will send the response for '#?' for everything.

You probably want:

responses:
          '#\? ': '5'
          "\[y/N\]": 'y'


I think that should like help out.

On Friday, September 4, 2015, Edgars <edgars.ma
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/704f6fd4-b7d0-464f-a868-07ab18ba9848%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Edgars

unread,
Sep 4, 2015, 8:17:32 AM9/4/15
to Ansible Project
Thanks a lot Matt, it worked. 

I was confused a bit because I saw that p/expect actually found the match (both of them), so I did not think that it could be regex issue

Thanks again

Edgars
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

CD Truong

unread,
Feb 5, 2016, 10:42:05 PM2/5/16
to Ansible Project
Hi,

I ran across a problem with expect response the same value to all prompt.

I have a task that execute a command and expecting four prompt the expect module response to the first prompt correctly but response to the 2nd & 3rd prompt with the same value from the 1st prompt.

 - expect:
    command: /path/to/the/script.sh
    responses:
     '.*?:$': '12345'
     '.*?:$': '12345'
     '.*?:$': 'xena-warrior'
     '.*?:$': '12345'
    echo: yes

All prompts have end of line ':'

From the output I can see the reponse '12345' in the third prompt and not 'xena-warrior'.

Any help would greatly appreciated,

David.

Matt Martz

unread,
Feb 6, 2016, 8:48:51 AM2/6/16
to ansible...@googlegroups.com
All of your "questions" are the same.  Because you are defining a hash/dict for responses, by providing the same key you are over writing the values.

If you need to respond multiple times to a question with different answers, this is not currently possible with the released version of the expect module but will be included in the next release.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f8753982-6d44-4381-b0ca-dda5a0b9b2dc%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages