# expect module does not work with multiple prompts

**URL:** <https://forum.ansible.com/t/expect-module-does-not-work-with-multiple-prompts/19185>\
**Category:** Ansible Project\
**Created:** [September 4, 2015, 9:53am UTC](https://forum.ansible.com/t/expect-module-does-not-work-with-multiple-prompts/19185 "2015-09-04T09:53:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [September 4, 2015, 9:53am UTC](https://forum.ansible.com/t/expect-module-does-not-work-with-multiple-prompts/19185/1 "2015-09-04T09:53:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [September 4, 2015, 12:00pm UTC](https://forum.ansible.com/t/expect-module-does-not-work-with-multiple-prompts/19185/2 "2015-09-04T12:00:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [September 4, 2015, 12:17pm UTC](https://forum.ansible.com/t/expect-module-does-not-work-with-multiple-prompts/19185/3 "2015-09-04T12:17:32Z")

</div>

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

piektdiena, 2015. gada 4. septembris 14:01:06 UTC+2, Matt Martz rakstīja:

---

<div class="post-metadata">

**Author:** ![CD\_Truong](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@CD\_Truong](https://forum.ansible.com/u/CD_Truong)\
**Post date:** [February 5, 2016, 11:18pm UTC](https://forum.ansible.com/t/expect-module-does-not-work-with-multiple-prompts/19185/4 "2016-02-05T23:18:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 6, 2016, 1:48pm UTC](https://forum.ansible.com/t/expect-module-does-not-work-with-multiple-prompts/19185/5 "2016-02-06T13:48:40Z")

</div>

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.
