Shared connection to server closed | How to Fix?
- arjun5792
- Oct 14, 2022
- 2 min read
Are you experiencing the Ansible error Shared connection to the server closed? You can rely on us.
Recently, one of our customers encountered an error while using Ansible to run commands on two brand-new CentOS 8 servers.
We help our clients with several Ansible queries as part of our server management services.
Let's look at how to fix the Shared connection to server closed error today.
Shared connection to server closed
When we run an Ansible command to run commands on two recently deployed CentOS 8 servers, we encounter the Ansible error.
[skynats@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”
Let's now examine the error in the Ansible module's cause and solution.
Shared connection to server closed: Reason
Our support staff discovered that the connection was lost because the remote system's shell(s) could not locate the Python interpreter.
We discovered that Python 2 is not installed on the systems after checking the remote hosts.
[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, Python 3 is already installed and is available as /usr/bin/python3 by default.
[root@server1~]# which python3
/usr/bin/python3
[root@server1~]#
[root@server2~]# which python3
/usr/bin/python3
[root@server2~]#
Quick fix for the Ansible module error
Python 3 and later are the only versions that Ansible 2.5 supports.
Additionally, Python 3 is automatically recognized and used by Ansible on many platforms that support it.
The ansible python interpreter inventory variable can be set at the group or host level to the location of a Python 3 interpreter, but if it doesn't work, we can explicitly configure a Python 3 interpreter.
Using the command line to pass the Python interpreter to Ansible
We can use the -e flag to momentarily fix the error above:
$ ansible prod_servers -e ‘ansible_python_interpreter=/usr/bin/python3’ -a “systemctl status firewalld” -u root
Ansible's Python interpreter is being set in the inventory
However, to permanently resolve the issue, we set the ansible python interpreter inventory variable in our inventory /etc/ansible/hosts:
Any text editor can be used to open the file.
$ sudo vim /etc/ansible/hosts
Each host in a group should have the following line appended to it:
ansible_python_interpreter=/usr/bin/python3
The hosts' definitions will therefore appear as follows:
[prod_servers]
192.168.10.17 ansible_python_interpreter=/usr/bin/python3
192.168.10.25 ansible_python_interpreter=/usr/bin/python3.6
Alternately, configure each host's Python interpreter to be the same.
[prod_servers]
192.168.10.17
192.168.10.25
[prod_servers:vars]
ansible_python_interpreter=/usr/bin/python3
Configuring Ansible to use the default Python interpreter
To accomplish this, we can modify the ansible python interpreter inventory variable in the main Ansible configuration file as follows:
$ sudo vim /etc/ansible/ansible.cfg
Then insert the next line in the [defaults] section.
ansible_python_interpreter=/usr/bin/python3
Close the window after saving the file.
Then try again to run the Ansible command:
$ ansible prod_servers -a “systemctl status firewalld” -u root
Comments