Bootstrap FreeKB - Ansible - Resolve "The following modules failed to execute: ansible.legacy.setup"
Ansible - Resolve "The following modules failed to execute: ansible.legacy.setup"

Updated:   |  Ansible articles

Let's say something like this is being returned.

TASK [Gathering Facts]
[ERROR]: Task failed: Action failed: The following modules failed to execute: ansible.legacy.setup.
Task failed: Action failed.
<<< caused by >>>
The following modules failed to execute: ansible.legacy.setup.
+--[ Sub-Event 1 of 1 ]---
|
| The module interpreter '/usr/local/ansible-core/2.19.0/bin/python3.12' was not found. Consider overriding the configured interpreter path for this host. See stdout/stderr for the returned output.
|
+--[ End Sub-Event ]---
fatal: [server1.example.com]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.setup": {"exception": "(traceback unavailable)", "failed": true, "module_stderr": "Shared connection to server1.example.com closed.\r\n", "module_stdout": "/bin/sh: line 1: /usr/local/ansible-core/2.19.0/bin/python3.12: No such file or directory\r\n", "msg": "The module interpreter '/usr/local/ansible-core/2.19.0/bin/python3.12' was not found.", "rc": 127}}, "msg": "The following modules failed to execute: ansible.legacy.setup."}

 

I first happened upon this when attempting to run an Ansible playbook using ansible-core version 2.19.0.

]$ ansible --version
ansible [core 2.19.0]
  config file = None
  configured module search path = ['/home/john.doe/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/ansible-core/2.19.0/lib/python3.12/site-packages/ansible
  ansible collection location = /home/john.doe/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/ansible-core/2.19.0/bin/ansible
  python version = 3.12.0 (main, May  1 2025, 22:01:12) [GCC 8.5.0 20210514 (Red Hat 8.5.0-24)] (/usr/local/ansible-core/2.19.0/bin/python3.12)
  jinja version = 3.1.6
  pyyaml version = 6.0.2 (with libyaml v0.2.5)

 

This appears to be an issue where your Ansible controller (the server Ansible is installed on) creates an AnsiballZ Python file on the target server such as AnsiballZ_setup.py and the AnsiballZ file is attempting to import a module that is not included in the version of Python on the target server. Check out my article FreeKB - Ansible - Getting Started with the Python Interpreter for a walkthrough on how to keep the AnsiballZ file on the target server to determine if the AnsiballZ file is indeed importing a module not included in Python on the target server. In this example, the __future__ module is not included in Python version 3.6 

~]$ head /home/john.doe/.ansible/tmp/ansible-tmp-1755145396.7215042-2660408-130123854424142/AnsiballZ_setup.py
#!/usr/bin/python3.6
from __future__ import annotations

 

Running the AnsiballZ Python script on the target server may return some sort of stderr.

~]$ python /home/john.doe/.ansible/tmp/ansible-tmp-1755145396.7215042-2660408-130123854424142/AnsiballZ_setup.py
  File "/home/john.doe/.ansible/tmp/ansible-tmp-1755145396.7215042-2660408-130123854424142/AnsiballZ_setup.py", line 3
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined

 

In this example, since we were able to determine that /usr/bin/python3.6 is having some issue importing annotations from __future__ I'd next see if the pip list command includes __future__.

/usr/bin/python3.6 -m pip list

 

According to this article

ansible-core 2.17 and above only supports Python 3.7 and above

 

One solution here is to use on of the following options to configure your Ansible controller to use a specific version of Python, preferrablly the same version of Python as being used on the target server.

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter df3acc in the box below so that we can be sure you are a human.