# \`ansible.builtin.shell\`: Python script completes but the Ansible Controller doesn't see it finish

**URL:** <https://forum.ansible.com/t/ansible-builtin-shell-python-script-completes-but-the-ansible-controller-doesnt-see-it-finish/40056>\
**Category:** Get Help\
**Tags:** ubuntu\
**Created:** [February 4, 2025, 5:32pm UTC](https://forum.ansible.com/t/ansible-builtin-shell-python-script-completes-but-the-ansible-controller-doesnt-see-it-finish/40056 "2025-02-04T17:32:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![beaver](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@beaver](https://forum.ansible.com/u/beaver)\
**Post date:** [February 4, 2025, 5:32pm UTC](https://forum.ansible.com/t/ansible-builtin-shell-python-script-completes-but-the-ansible-controller-doesnt-see-it-finish/40056/1 "2025-02-04T17:32:04Z")

</div>

**Ansible version** : ansible [core 2.17.7]  
**Python version** : python version = 3.10.12 (main, Jan 17 2025, 14:35:34) [GCC 11.4.0] (/usr/bin/python3)  
**Target machine: Ubuntu 24.04, Python 3.12**

I have this Ansible playbook to enable toriptables:

(You can prepare the Controller by doing `git clone https://github.com/ruped24/toriptables3.git` first)

Then I run this playbook:

```yml
---
- name: Ensure toriptables3 is installed and loaded
  hosts: tor_enabled
  become: yes

  tasks:
  - name: Ensure Tor is installed
    apt:
      name: tor
      state: present
  
  - name: Copy toriptables3.py if not already present
    ansible.builtin.copy:
      content: "{{ lookup('file', './toriptables3/toriptables3.py') }}"
      dest: /usr/local/bin/toriptables3.py
      mode: '0755'

  - name: Load toriptables if not already loaded
    ansible.builtin.shell:
      cmd: "timeout 60s toriptables3.py -l"
      executable: /bin/bash

```

On a fresh Ubuntu/Debian install the last task, running `toriptables.py -l`, will run but its completion will **not be detected** by the Ansible controller. Running the playbook again works successfully, but this is of little use since it’s already loaded.

Subsequently on the same machine I can reproduce the issue by:

```sh
sudo toriptables3.py -f
sudo rm /usr/local/bin/toriptables3.py
sudo systemctl restart tor@default.service

```

and then running the playbook. The last task times out after many minutes or I end it manually myself: `^C [ERROR]: User interrupted execution`.

Strangely I can confirm the **script always completes successfully** (by printing to files for instance), but the Ansible Controller never detects that it finishes.

There are no logs output by the Ansible controller (even with `-vvv`). But I think this is by Ansible’s design.

Could the reason for this be that the script itself ([https://github.com/ruped24/toriptables3/blob/master/toriptables3.py](https://github.com/ruped24/toriptables3/blob/master/toriptables3.py)) uses subprocess calls? How can I debug this further?

---

<div class="post-metadata">

**Author:** ![it-pappa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/it-pappa/32/7523_2.png) [@it-pappa](https://forum.ansible.com/u/it-pappa)\
**Post date:** [February 7, 2025, 11:03am UTC](https://forum.ansible.com/t/ansible-builtin-shell-python-script-completes-but-the-ansible-controller-doesnt-see-it-finish/40056/2 "2025-02-07T11:03:24Z")

</div>

Have you tried with the script module?  
ansible.builtin.script.  
I havent tried it myself but i have seen many other use it for running python

---

<div class="post-metadata">

**Author:** ![beaver](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@beaver](https://forum.ansible.com/u/beaver)\
**Post date:** [February 7, 2025, 4:10pm UTC](https://forum.ansible.com/t/ansible-builtin-shell-python-script-completes-but-the-ansible-controller-doesnt-see-it-finish/40056/3 "2025-02-07T16:10:28Z")

</div>

Hi! Thanks so much for the response.

Unfortunately, `ansible.builtin.script` gives me the same results: the Ansible controller does not continue until the task times out even though the script runs and finishes successfully. When if I modify the `toriptables3.py` to output to a file when it finishes successfully, it does this when called by the task.

So there must be something with the task that Ansible can’t detect when it finishes. But I don’t know what it is.
