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

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:

---
- 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:

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) uses subprocess calls? How can I debug this further?

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

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.