How to Fix “Shared connection to x.x.xx closed” Ansible Error

In this short article, we will explain how to solve the: “module_stderr“: “Shared connection to x.x.x.x closed.\r\n”, “module_stdout”: “/bin/sh: /usr/bin/python: No such file or directory\r\n”, while running Ansible commands.

The following screenshot shows the Ansible module error. We encountered this error while running an Ansible command to execute commands on two newly deployed CentOS 8 servers.

Ansible Module Error
Ansible Module Error

From the error details, the connection failed because the shell(s) in the remote system couldn’t find the Python interpreter (/usr/bin/python) as indicated by the line: “module_stdout”: “/bin/sh: /usr/bin/python: No such file or directory\r\n“.

After checking the remote hosts, we discovered that the systems don’t have Python 2 installed.

Check Python Binary
Check Python Binary

They have Python 3 installed by default and its binary is /usr/bin/python3.

Check Python3 Binary
Check Python3 Binary

According to the Ansible documentation, Ansible (2.5 and above) works with Python version 3 and above only. Also, Ansible is supposed to automatically detect and use Python 3 on many platforms that ship with it.

However, if it fails to, then you can explicitly configure a Python 3 interpreter by setting the ansible_python_interpreter inventory variable at a group or host level to the location of a Python 3 interpreter as described below.

Passing Python Interpreter to Ansible on the Command-line

To fix the above error temporarily, you can use the -e flag to pass the Python 3 interpreter to Ansible as shown.

$ ansible prod_servers  -e 'ansible_python_interpreter=/usr/bin/python3' -a "systemctl status firewalld" -u root
Set Python Interpreter via Command-line
Set Python Interpreter via Command-line

Setting Python Interpreter for Ansible in the Inventory

To fix the error permanently, set the ansible_python_interpreter inventory variable in your inventory /etc/ansible/hosts. You can open it for editing using the v/im or nano text editor as shown.

$ sudo vim /etc/ansible/hosts
OR
# vim /etc/ansible/hosts

Append the following line to each host or hosts in a group:

ansible_python_interpreter=/usr/bin/python3

So, your hosts’ definitions can look like this:

[prod_servers]
192.168.10.1			ansible_python_interpreter=/usr/bin/python3
192.168.10.20			ansible_python_interpreter=/usr/bin/python3.6
Set Python Interpreter for Ansible
Set Python Interpreter for Ansible

Alternatively, set the same Python interpreter for a group of hosts as shown.

[prod_servers]
192.168.10.1		
192.168.10.20		

[prod_servers:vars]
ansible_python_interpreter=/usr/bin/python3
Set Python Interpreter-for All Hosts in a Group
Set Python Interpreter-for All Hosts in a Group

Setting Default Python Interpreter in Ansible Configuration

To set the default Python interpreter, you can set the ansible_python_interpreter inventory variable in Ansible’s main configuration file /etc/ansible/ansible.cfg.

$ sudo vim /etc/ansible/ansible.cfg

Add the following line under the the [defaults] section.

ansible_python_interpreter=/usr/bin/python3
Set Python Interpreter in Ansible Configuration
Set Python Interpreter in Ansible Configuration

Save the file and close it.

Now try to run the Ansible command once more:

$ ansible prod_servers -a "systemctl status firewalld" -u root
Check Ansible Command
Check Ansible Command

For more information about this topic, see Python 3 support in the official Ansible documentation.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.