Need help?

Our experts have had an average response time of 11.7 minutes in August 2021 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

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 *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF