The following modules failed to execute: ansible.legacy.setup

If you are encountering this error message when trying to run an Ansible Playbook against a node, it in one case was that since Python 2 was by default installed, and Python 2 is becoming deprecated. Python 3 is the new supported version, the node needs to be upgraded to Python 3.

In a particular case with running Ansible and attempting to run a playbook, the following error was encountered why trying to run this test.

Testing Connectivity to Nodes

To test that Ansible is able to connect and run commands and playbooks on your nodes, you can use the following command:

ansible all -m ping

The ping  module will test if you have valid credentials for connecting to the nodes defined in your inventory file, in addition to testing if Ansible is able to run Python scripts on the remote server. A pong reply back means Ansible is ready to run commands and playbooks on that node.

[DEPRECATION WARNING]: Distribution debian 10.3 on host 192.168.1.155 should use /usr/bin/python3, but is using /usr/bin/python for
backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this
 host. See https://docs.ansible.com/ansible-core/2.11/reference_appendices/interpreter_discovery.html for more information. This feature
will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
192.168.1.155 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

So, the following was run:

python --version

And it returned this as the version for Python

Python 2.7.15+

Not ideal, but not outside of the norm. Python 2 was the standard for years, as a matter of fact, it was the standard since 2000. (Read more)

So let’s update to Python 3.x:

sudo apt-get install python3.6

And then execute the following to change the default version of Python from 2 to 3.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
echo "alias python='python3'" >> .bashrc
source .bashrc

Now it is upgraded!

python --version
Python 3.7.3