×


Ansible error Shared connection to server closed

Are you facing Ansible error, Shared connection to server closed?

This guide will 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.
This error triggers when python 2 is not installed on the Linux System.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Ansible related queries.
In this context, we shall look into how to resolve the error, Shared connection to server closed.

Shared connection to server closed - Nature of this Ansible error ?

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

[ibmimedia@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”


What triggers Shared connection to server closed Ansible error ?

After troubleshooting, we 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, the system 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~]#


How to resolve Shared connection to server closed Ansible 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.

1. 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


2. 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 host's 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


3. 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 Ansible error Shared connection to server closed? We are here for you. ]


Conclusion

This article covers the Shared connection to server closed error which occur when we run an Ansible command to execute commands on two newly deployed CentOS 8 servers.
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.
While you can write Ansible modules in any language, most Ansible modules are written in Python, including the ones central to letting Ansible work. By default, Ansible assumes it can find a /usr/bin/python on your remote system that is either Python2, version 2.6 or higher or Python3, 3.5 or higher.

A quick fix to Ansible error Shared connection to server closed is to just add the path to python 3 in your inventory file.
It would look something like this:
ip_address ansible_python_interpreter=/usr/bin/python3
Then you could test if it works with the ping module:
ansible -m ping all

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
They have Python 3 installed by default and its binary is /usr/bin/python3.

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.