# expect module

**URL:** <https://forum.ansible.com/t/expect-module/35808>\
**Category:** Ansible Project\
**Created:** [September 30, 2021, 7:43pm UTC](https://forum.ansible.com/t/expect-module/35808 "2021-09-30T19:43:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hanumantha\_Reddy\_Bas](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@Hanumantha\_Reddy\_Bas](https://forum.ansible.com/u/Hanumantha_Reddy_Bas)\
**Post date:** [September 30, 2021, 7:43pm UTC](https://forum.ansible.com/t/expect-module/35808/1 "2021-09-30T19:43:40Z")

</div>

Hello All,

trying to understand expect functionality using below piece of code… I know I can do using becom\_user, but I would like to check expect module… since there is requirement where I need to use expect module for automating client app CLI

tasks:

- name: testing expect  
expect:  
command: sudo su  
responses:  
‘(?_)password’: “password”  
'(?_)’: ls  
timeout: 30  
echo: yes

task is hanged and not continued further. Any suggestions are highly welcome.

---

<div class="post-metadata">

**Author:** ![Sandip\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Sandip\_Bhattacharya](https://forum.ansible.com/u/Sandip_Bhattacharya)\
**Post date:** [September 30, 2021, 11:01pm UTC](https://forum.ansible.com/t/expect-module/35808/2 "2021-09-30T23:01:55Z")

</div>

In your specific case where pexpect is working on the remote system (else you would have got an error),  
perhaps the problem is that you are not exiting from the shell?

Instead of just "ls", try "ls; exit"

HTH,  
&nbsp;&nbsp;&nbsp;Sandip

P.S. I once had a similar problem, with the additional constraint that the right version of python-expect  
was not available on the remote system. So I had to use the system expect executable.

I documented my solution here:

[https://blog.sandipb.net/2019/06/14/ansible-privilege-escalation-with-expect-when-you-dont-have-root-shell-privileges/](https://blog.sandipb.net/2019/06/14/ansible-privilege-escalation-with-expect-when-you-dont-have-root-shell-privileges/)

---

<div class="post-metadata">

**Author:** ![Hanumantha\_Reddy\_Bas](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@Hanumantha\_Reddy\_Bas](https://forum.ansible.com/u/Hanumantha_Reddy_Bas)\
**Post date:** [October 1, 2021, 2:13am UTC](https://forum.ansible.com/t/expect-module/35808/3 "2021-10-01T02:13:17Z")

</div>

but unfortunately, expect package is not installed on target nodes and even not allowed too. only I have to use ansible built in module.

I tried ls;exit… but still the same issue.
