Ansible Error – Shared Connection Closed

Today, ansible started showing the following ERROR while pinging a host or multiple hosts. The Error looks something like following

121.141.161.181 | FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to 121.141.161.181 closed.\r\n",
    "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 127
}

This error is shown because the python module is not found by Ansible to communicate and run tasks. As I found out, there is a simple solution to this. Try and add the following line to the hosts file or put it in a common variable list so that ansible can access it while trying to connect to the host.

Python 2

if the remote host has python 2 installed/running

<host_ip> ansible_python_interpreter=/usr/bin/pyhon

Python 3

if the remote host has python3 installed/running

<host_ip> ansible_python_interpreter=/usr/bin/python3

In variables file, no need to put host_ip. Only the variable declaration will work.

Enjoy!