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