# Ansible builtin expect fails with python error

**URL:** <https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125>\
**Category:** Get Help\
**Tags:** playbook, ansible-core\
**Created:** [October 9, 2024, 11:57pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125 "2024-10-09T23:57:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danby](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Danby](https://forum.ansible.com/u/Danby)\
**Post date:** [October 9, 2024, 11:57pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/1 "2024-10-09T23:57:40Z")

</div>

I have a playbook I’ve been using for a couple of years that uses the expect module to join a Linux host to the AD domain.  
The relevant stanza is this:

```auto
  - name: Join system to AD
    expect:
      command: /bin/bash -c "/usr/sbin/realm join --user={{ ldap_user }} mydomain --computer-ou=OU=Servers"
      responses:
        (?i)Password: "{{ bind_password }}"
    notify:
    - restart sssd

```

Username and bind password are prompted for at invocation.  
As I said it’s been working fine, but I am setting up my first RHEL9 servers, RHEL9.2-EUS and it fails with this error:

```auto
Traceback (most recent call last):
  File "/tmp/ansible_expect_payload_f22xbqfd/ansible_expect_payload.zip/ansible/modules/expect.py", line 210, in main
AttributeError: module 'pexpect' has no attribute 'run'

```

pexpect and ptyprocess are installed by another playbook. Both are at current versions. Python version at the remote end is 3.9

Ansible version is

```auto
ansible-playbook [core 2.14.4]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/myuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/myuser/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.9.13 (main, Nov 9 2022, 13:16:24) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)] (/usr/bin/python3.9)
  jinja version = 3.1.3
  libyaml = True

```

This is obviously a python error, and probably related to the RedHat version but I can’t figure out why it would be unable to use a function out of the pexpect module. Is it missing a dependency?  
Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [October 10, 2024, 3:55pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/2 "2024-10-10T15:55:37Z")

</div>

> [@Danby](#):
>
> pexpect and ptyprocess are installed by another playbook. Both are at current versions.

On the remote server? I’m asking as I’d guess that is where they are required and your post appears to indicate that this is on the Ansible controller? Of course I could be totally wrong, this is just a quick guess!

---

<div class="post-metadata">

**Author:** ![Danby](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Danby](https://forum.ansible.com/u/Danby)\
**Post date:** [October 10, 2024, 9:29pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/3 "2024-10-10T21:29:58Z")

</div>

Yes, pexpect and ptyprocess are installed on the remote server by a previous ansible playbook that makes sure the environment is ready for ansible. It also installs python3,9 if it is not present, pip 3.9, etc.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [October 11, 2024, 5:32pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/4 "2024-10-11T17:32:56Z")

</div>

post full execution plus error using `-vvv`, also note that 2.14 is pretty old at this point.

Also you should show pexpect version (module requires pexpect \>= 3.3)

---

<div class="post-metadata">

**Author:** ![Danby](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Danby](https://forum.ansible.com/u/Danby)\
**Post date:** [October 14, 2024, 4:12pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/5 "2024-10-14T16:12:13Z")

</div>

```auto
TASK [Join system to AD] ***************************************************************************************************
task path: /home/danbak1/git/current-playbooks/AD.yml:44
<myhost> ESTABLISH SSH CONNECTION FOR USER: root
<myhost> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/home/danbak1/.ansible/cp/da4efc7913"' myhost '/bin/sh -c '"'"'echo ~root && sleep 0'"'"''
<myhost> (0, b'/root\n', b'')
<myhost> ESTABLISH SSH CONNECTION FOR USER: root
<myhost> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/home/danbak1/.ansible/cp/da4efc7913"' myhost '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489 `" && echo ansible-tmp-1728921680.1241539-3907961-267960157196489="` echo /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489 `" ) && sleep 0'"'"''
<myhost> (0, b'ansible-tmp-1728921680.1241539-3907961-267960157196489=/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489\n', b'')
Using module file /usr/lib/python3.9/site-packages/ansible/modules/expect.py
<myhost> PUT /home/danbak1/.ansible/tmp/ansible-local-3902922r0c1y55b/tmpim5ot2eu TO /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py
<myhost> SSH: EXEC sshpass -d10 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/home/danbak1/.ansible/cp/da4efc7913"' '[myhost]'
<myhost> (0, b'sftp> put /home/danbak1/.ansible/tmp/ansible-local-3902922r0c1y55b/tmpim5ot2eu /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py\n', b'')
<myhost> ESTABLISH SSH CONNECTION FOR USER: root
<myhost> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/home/danbak1/.ansible/cp/da4efc7913"' myhost '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/ /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py && sleep 0'"'"''
<myhost> (0, b'', b'')
<myhost> ESTABLISH SSH CONNECTION FOR USER: root
<myhost> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/home/danbak1/.ansible/cp/da4efc7913"' -tt myhost '/bin/sh -c '"'"'/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py && sleep 0'"'"''
<myhost> (1, b'Traceback (most recent call last):\r\n File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 210, in main\r\nAttributeError: module \'pexpect\' has no attribute \'run\'\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 107, in <module>\r\n _ansiballz_main()\r\n File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 99, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 47, in invoke_module\r\n runpy.run_module(mod_name=\'ansible.modules.expect\', init_globals=dict(_module_fqn=\'ansible.modules.expect\', _modlib_path=modlib_path),\r\n File "/usr/lib64/python3.9/runpy.py", line 225, in run_module\r\n return _run_module_code(code, init_globals, run_name, mod_spec)\r\n File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code\r\n _run_code(code, mod_globals, init_globals,\r\n File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code\r\n exec(code, run_globals)\r\n File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 258, in <module>\r\n File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 229, in main\r\nAttributeError: module \'pexpect\' has no attribute \' __version__ \'\r\n', b'Shared connection to myhost closed.\r\n')
<myhost> Failed to connect to the host via ssh: Shared connection to myhost closed.
<myhost> ESTABLISH SSH CONNECTION FOR USER: root
<myhost> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/home/danbak1/.ansible/cp/da4efc7913"' myhost '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/ > /dev/null 2>&1 && sleep 0'"'"''
<myhost> (0, b'', b'')
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 210, in main
AttributeError: module 'pexpect' has no attribute 'run'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 107, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible.modules.expect', init_globals=dict(_module_fqn='ansible.modules.expect', _modlib_path=modlib_path),
  File "/usr/lib64/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 258, in <module>
  File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 229, in main
AttributeError: module 'pexpect' has no attribute ' __version__'
fatal: [myhost]: FAILED! => {
    "changed": false,
    "rc": 1
}

MSG:

MODULE FAILURE
See stdout/stderr for the exact error

MODULE_STDOUT:

Traceback (most recent call last):
  File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 210, in main
AttributeError: module 'pexpect' has no attribute 'run'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 107, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1728921680.1241539-3907961-267960157196489/AnsiballZ_expect.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible.modules.expect', init_globals=dict(_module_fqn='ansible.modules.expect', _modlib_path=modlib_path),
  File "/usr/lib64/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 258, in <module>
  File "/tmp/ansible_expect_payload_3n1p_k08/ansible_expect_payload.zip/ansible/modules/expect.py", line 229, in main
AttributeError: module 'pexpect' has no attribute ' __version__'

MODULE_STDERR:

Shared connection to myhost closed.

```

And pexpect

```auto
[root@RHEL92-Template ~]# pip show pexpect
Name: pexpect
Version: 4.9.0
Summary:
Home-page:
Author:
Author-email:
License:
Location: /usr/local/lib/python3.9/site-packages
Requires:
Required-by: sos

```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [October 14, 2024, 8:18pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/7 "2024-10-14T20:18:30Z")

</div>

> [@Danby](#):
>
> ` module 'pexpect' has no attribute ' __version__'`

Weird, it is like it is getting the ‘wrong’ pexpect:

```auto
>>> print(pexpect. __version__ )
4.9.0

```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [October 14, 2024, 8:22pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/8 "2024-10-14T20:22:31Z")

</div>

The target uses `/usr/bin/python3` on `myhost` is it 3.9 and does it have the correct version of pexpect installed?

---

<div class="post-metadata">

**Author:** ![Danby](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Danby](https://forum.ansible.com/u/Danby)\
**Post date:** [October 14, 2024, 8:25pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/9 "2024-10-14T20:25:25Z")

</div>

It is weird, and perplexing.  
Yes, the only version of python on the system is 3.9. pexpect is installed with pip3.9. I specify that after getting burned by that a couple of years ago, so it should be the right version.

---

<div class="post-metadata">

**Author:** ![Danby](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Danby](https://forum.ansible.com/u/Danby)\
**Post date:** [October 14, 2024, 8:43pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/10 "2024-10-14T20:43:00Z")

</div>

You have found it.  
There was a module pre-installed at /usr/local/lib/python3.9/site-packages/ that has no run or version properties in **init**.py  
I renamed it and copied the installed version from /usr/lib/python3.9/site-packages/pexpect.  
Now it is giving a ptyprocess/python/pexpect version mismatch. SO I need to figure out how to specify the correct location when installing the python module or the correct python module within the playbook.  
Anyway, I understand what’s going on now, so thank you  
Dan Baker

---

<div class="post-metadata">

**Author:** ![Danby](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Danby](https://forum.ansible.com/u/Danby)\
**Post date:** [October 14, 2024, 8:57pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/11 "2024-10-14T20:57:55Z")

</div>

Uninstalling pexpect and ptyprocess and then reinstalling resolved the issue

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 13, 2024, 8:58pm UTC](https://forum.ansible.com/t/ansible-builtin-expect-fails-with-python-error/10125/12 "2024-11-13T20:58:35Z")

</div>

Closed on November 13, 2024, 8:58pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
