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.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Aaron Kili
Aaron Kili
Contributor
About the author

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

1 Comment

Leave a Reply
  1. Hello,

    I have tried all the suggestions listed in this article but am still not able to resolve the issue. Please is there any other thing you may suggest on this topic?

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.