Bobcares

Shared connection to server closed – How to fix Ansible error

by | Mar 7, 2021

Stuck with the Ansible error, Shared connection to server closed? We can help you.

Recently we had a customer who came across the error while running an Ansible command to execute commands on two newly deployed CentOS 8 servers.

As part of our Server Management Services, we assist our customers with several Ansible queries.

Today, let us see how to resolve the error, Shared connection to server closed.

 

Shared connection to server closed

The Ansible error occurs while we run an Ansible command to execute commands on two newly deployed CentOS 8 servers.

[bob@server~]$ ansible prod_servers -a “systemctl status firewalld” -u root

“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”

Moving ahead, let us see the cause and resolution to this Ansible module error.

 

Cause of Shared connection to server closed

Our Support Techs found that the connection failed because the shell(s) in the remote system could not find the Python interpreter.

After checking the remote hosts, we found that the systems do not have Python 2 installed.

[root@server1~]# which python
/usr/bin/which: no python in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@server1~]#

[root@server2~]# which python
/usr/bin/which: no python in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@server2~]#

However, they have Python 3 installed by default and its binary is /usr/bin/python3.

[root@server1~]# which python3
/usr/bin/python3
[root@server1~]#

[root@server2~]# which python3
/usr/bin/python3
[root@server2~]#

 

Solution for the Ansible module error

Ansible 2.5 works with Python version 3 and above only.

In addition, Ansible automatically detects and uses Python 3 on many platforms that ship with it.

However, if it fails to, then we 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.

  • Passing Python Interpreter to Ansible on the Command-line

To fix the above error temporarily, we can use the -e flag:

$ ansible prod_servers -e ‘ansible_python_interpreter=/usr/bin/python3’ -a “systemctl status firewalld” -u root
  • Setting Python Interpreter for Ansible in the Inventory

On the other hand, to fix the error permanently, we set the ansible_python_interpreter inventory variable in our inventory /etc/ansible/hosts:

Open the file in any text editor.

$ sudo vim /etc/ansible/hosts

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

ansible_python_interpreter=/usr/bin/python3

So, the hosts’ definitions will look like this:

[prod_servers]
192.168.10.17 ansible_python_interpreter=/usr/bin/python3
192.168.10.25 ansible_python_interpreter=/usr/bin/python3.6

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

[prod_servers]
192.168.10.17
192.168.10.25

[prod_servers:vars]
ansible_python_interpreter=/usr/bin/python3
  • Setting Default Python Interpreter in Ansible Configuration

To do this, we can set the ansible_python_interpreter inventory variable in Ansible’s main configuration file:

$ sudo vim /etc/ansible/ansible.cfg

Then add the following line under the [defaults] section.

ansible_python_interpreter=/usr/bin/python3

Eventually, save the file and close it.

Then try to run the Ansible command once more:

$ ansible prod_servers -a “systemctl status firewalld” -u root

[Still, stuck with the error? We are here for you]

 

Conclusion

To conclude, the Shared connection to server closed error occur when we run an Ansible command to execute commands on two newly deployed CentOS 8 servers.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.